<template>
  <!-- 任务分配 -->
  <a-modal title="任务分配" :open="taskDialogVisible" @cancel="closeTaskDialog" width="70%">
    <a-form :label-col="{span: 6}" :wrapper-col="{span: 18}">
      <a-form-item label="机构">
        <a-select v-model:value="orgId" placeholder="请选择" @change="orgChange">
          <a-select-option v-for="(item,index) in orgList" :key="index" :value="item.orgId">{{ item.orgNm
          }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="督查对象类型">
        <a-checkbox :indeterminate="indeterminate" :checked="selectAll" @change="onCheckAllChange">全选</a-checkbox>
        <a-checkbox-group v-model:value="checkList" @change="checkboxChange" >
          <a-checkbox v-for="(item,index) in objList" :key="index" :disabled="item.disabled" :value="item.name">{{ item.name }}</a-checkbox>
        </a-checkbox-group>
      </a-form-item>
    </a-form>
    <div slot="footer" class="dialog-footer">
      <a-button @click="closeTaskDialog">取 消</a-button>
      <a-button type="primary" @click="addTask">确 定</a-button>
    </div>
  </a-modal>
</template>

<script>
import request from '../../utils/gw_ajax_request';
export default {
  props: ['personTaskInfo'],
  data() {
    return {
      orgList: [],
      objList: [],
      checkList: [],
      orgId: '',
      taskDialogVisible: true,
      selectAll: false, // 是否全选
      indeterminate: true,
    };
  },
  computed: {
    persId() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '';
    },
  },
  created() {},
  mounted() {
    this.getObjList();
    this.getOrgList();
  },
  methods: {
    addTask() {
      const paramObj = [];
      let params = {};
      this.objList.forEach((elem) => {
        this.checkList.forEach((ele) => {
          if (elem.name == ele) {
            paramObj.push({
              persId: this.personTaskInfo.persId,
              orgId: this.orgId,
              name: ele,
              code: elem.code,
            });
          }
        });
      });
      params = {
        inspTypeList: paramObj.length == 0 ? null : paramObj,
        delPersId: this.personTaskInfo.persId,
        delOrgId: this.orgId,
      };

      request.post(`/bis/insp/add/inspTypes`, params).then((res) => {
        if (res.success) {
          this.$message.success('绑定成功');
          this.$emit('closeTaskDialog');
        } else {
          this.$message.error(res.message);
        }
      });
    },
    closeTaskDialog() {
      this.$emit('closeTaskDialog');
    },
    getObjList() {
      request.get(`/bis/insp/listInspType`).then((res) => {
        if (res.success) {
          this.objList = res.data;
          this.objList.forEach((ele) => {
            ele['disabled'] = false;
          });
          this.getCurInfo();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    getOrgList() {
      const orgIds = this.personTaskInfo.orgIds.split(',');
      const orgNames = this.personTaskInfo.orgNames.split(',');
      orgIds.forEach((ele, index) => {
        this.orgList.push({ orgId: ele, orgNm: orgNames[index] });
      });
      this.orgId = this.orgList[0].orgId;
    },
    // 查询当前已选择的督查类
    getCurInfo() {
      this.checkList = [];
      request
        .get(`/bis/insp/list/${this.orgId}/${this.personTaskInfo.persId}`)
        .then((res) => {
          if (res.success) {
            const bindObjList = res.data;
            this.objList.forEach((elem) => {
              elem['disabled'] = false;
            });
            bindObjList.forEach((ele) => {
              this.objList.forEach((elem) => {
                if (ele.id == elem.code) {
                  this.checkList.push(elem.name);
                  // elem['disabled'] = true
                }
              });
            });
          } else {
            this.$message.error(res.message);
          }
        });
    },
    checkboxChange(checkList) {
      this.indeterminate =
        !!checkList.length && checkList.length < this.objList.length;
      this.checkAll = checkList.length === this.objList.length;
    },
    orgChange() {
      this.getCurInfo();
    },
    onCheckAllChange(e) {
      const checkedList = [];
      this.objList.forEach((item) => {
        checkedList.push(item.name);
      });
      Object.assign(this, {
        checkList: e.target.checked ? checkedList : [],
        indeterminate: false,
        selectAll: e.target.checked,
      });
      if (!e.target.checked) {
        this.getCurInfo();
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
