<template>
  <section class="app-container">
    <el-row>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="addArea">添加</el-button>
      <el-button type="danger" icon="el-icon-delete" size="mini" @click="delArea">批量删除</el-button>
    </el-row>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="大区ID">
      </el-table-column>
      <el-table-column prop="areaName" label="大区名称">
      </el-table-column>
      <el-table-column prop="ordinal" label="序数">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="view(scope.row)">查看省份</el-button>

          <!-- <el-button size="mini" type="text" icon="el-icon-view" @click="view(scope.row)">查看省份</el-button> -->
          <el-button type="primary" icon="el-icon-edit" @click="editArea(scope.row)">修改</el-button>
          <el-button type="primary" icon="el-icon-delete" @click="delArea(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog title="省份列表" :visible.sync="open" width="800px" append-to-body>
      <el-row>
        <el-col :span="8">
          <span v-if="!areaData.length">该大区下暂无省份</span>
          <el-row>
            <el-tag class="mb5 mr5" v-for="tag in areaData" :key="tag.code" closable @close="handleDelAreaProvince(tag)">
              {{tag.cityName}}
            </el-tag>
          </el-row>
        </el-col>
        <el-col :span="16">
          <!-- <span>待选列表</span> -->
          <!-- //tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase())) -->
          <el-table :data="provinceTableData" height="250" style="width: 100%" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55">
            </el-table-column> -->
            <el-table-column prop="cityName" label="省份名称">
            </el-table-column>
            <el-table-column prop="code" label="地区编码">
            </el-table-column>
            <el-table-column align="center" class-name="small-padding fixed-width">
              <template slot="header" slot-scope="scope">
                <el-input v-model="provinceSearch" size="mini" clearable="" placeholder="输入省份名搜索" />
              </template>
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-plus" @click="addAreaProvince(scope.row)">添加省份</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- <el-table :data="areaProvince" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="id" label="大区ID">
        </el-table-column>
        <el-table-column prop="areaName" label="大区名称">
        </el-table-column>
        <el-table-column prop="ordinal" label="序数">
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
          </template>
        </el-table-column>
      </el-table> -->
      <!-- <el-checkbox-group v-model="areaData">
        <el-checkbox v-for="data in provinceData" :key="data.code" :label="data.code" :value="data.code">{{data.cityName}}</el-checkbox>
      </el-checkbox-group> -->
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div> -->
    </el-dialog>

    <!-- 大区弹框 -->
    <el-dialog :title="title" :visible.sync="areaOpen" width="500px" append-to-body>
      <el-form ref="areaForm" :model="areaForm" :rules="areaRules" label-width="100px">
        <el-form-item label="大区名称" prop="areaName">
          <el-input v-model="areaForm.areaName" placeholder="请输入大区名称"></el-input>
        </el-form-item>
        <el-form-item label="序数" prop="ordinal">
          <el-input v-model="areaForm.ordinal" placeholder="请输入序数"></el-input>
        </el-form-item>
        <el-form-item label="运维角色" prop="roleId">
          <el-select v-model="areaForm.roleId" placeholder="请选择">
            <el-option v-for="item in roleSelectList" :key="item.id" :label="item.roleName" :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import {roleSelect} from '@/api/system/role'
import { list, areaSelect, getProvinceByArea, getProvinceByParent, province, save, saveProvince, del, delAreaProvince } from "@/api/hardware/area";
export default {
  name: 'AreaList',
  data () {
    return {
      roleSelectList:[],
      ids: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      areaOpen: false,
      // 表格数据
      tableData: [],
      areaData: [], // 大区省份
      provinceData: [], // 所有省份
      provinceSearch: '',
      // 表单参数
      areaForm: {},
      row: {}, // 大区
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      areaRules: {
        areaName: [
          { required: true, message: "大区名称不能为空", trigger: "blur" },
        ],
        ordinal: [
          { required: true, message: "序数不能为空", trigger: "blur" },
        ],
        roleId:[
          {required: true, message: '请选择运维角色', trigger: 'change'}
        ]
      },
    }
  },
  computed: {
    provinceTableData () {
      return this.provinceData.filter(data => data.cityName.includes(this.provinceSearch) && -1 == this.areaData.findIndex(v => v.code == data.code))
    }
  },
  created () {
    province().then(res => {
      if (res.code == 200) {
        this.provinceData = res.rows;
      } else {
        this.$message.error(msg);
      }
    })
    this.getList()
  },
  methods: {
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    // 表单重置
    reset () {
      this.areaForm = {
        id: "",
        areaName: "",
        ordinal: "",
        roleId:''
      };
      this.resetForm("areaForm");
    },

    // 取消按钮
    cancel () {
      this.areaOpen = false;
      this.reset();
    },

    // 获取大区列表
    getList () {
      list(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },

    // 添加大区
    addArea () {
      this.reset();
      this.areaOpen = true;
      this.title = "添加大区";
      this.getUser()
    },

    // 获取大区角色
    async getUser(){
      const {code,msg,select} = await roleSelect()
      if(code!==200) return this.msgError('获取运营角色失败')
      this.roleSelectList=select
    },

    // 修改大区
    editArea (row) {
      this.areaOpen = true;
      this.areaForm = Object.assign({}, row);
      this.title = "修改大区";
    },

    // 提交
    submitForm () {
      this.$refs.areaForm.validate(valid=>{
      if (!valid) return this.msgError('请输入必填项信息')
      save(this.areaForm).then(res => {
        if (200 == res.code) {
          this.areaOpen = false;
          this.msgSuccess("添加大区成功");
          this.getList();
        } else {
          this.msgError(res.msg);
        }
      })
    })
    },

    async view (row) {
      this.row = row;
      await this.getProvinceByArea(row.id)
      this.open = true;
    },
    async getProvinceByArea (id) {
      return getProvinceByArea(id).then(response => {
        // this.areaData = response.data.map(v => v.code);
        this.areaData = response.data;
      });
    },
    addAreaProvince ({ code }) {
      saveProvince({
        ...this.row,
        "provinceIds": [code]
      }).then(res => {
        this.getProvinceByArea(this.row.id)
        this.$forceUpdate();
      })
    },
    handleDelAreaProvince ({ id }) {
      delAreaProvince(id).then(res => {
        this.getProvinceByArea(this.row.id)
      })
    },
    submit () {
      saveProvince({
        ...this.row,
        "provinceIds": this.areaData
      }).then(res => {
        this.open = false;
        console.log(res)
      })
    },

    /** 删除按钮操作 */
    delArea (row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(ids);
        })
        .then((res) => {
          if (200 == res.code) {
            this.getList();
            this.msgSuccess("删除成功");
          } else {
            this.msgError(res.msg);
          }
        })
        .catch((res) => { });
    },
  }
}
</script>

<style lang="scss" scoped>
.el-checkbox {
  width: 50px;
}
</style>