<template>
  <a-modal
    title="添加人员"
    :open="dialogFormVisible"
    @ok="addMens"
    @cancel="dialogFormVisible = false"
    width="70%"
  >
    <a-layout>
      <a-layout-content
        style="border:1px solid #d5d5ff;margin-left:5px;padding:0px;"
      >
        <p
          style="font-weight:bolder;background-color:#d5d5ff;padding:3px;margin: 0;"
        >
          督查人员
        </p>
        <a-form layout="inline" :model="form" style="padding: 10px;">
          <a-form-item label="人员姓名">
            <a-input
              v-model:value="form.searchText"
              placeholder="请输入人员名称"
            ></a-input>
          </a-form-item>
          <a-form-item label="机构名称">
            <a-input
              v-model:value="form.jgName"
              placeholder="请输入机构名称"
            ></a-input>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="getTableData">查询</a-button>
          </a-form-item>
        </a-form>
        <a-table
          :row-selection="{
            selectedRowKeys: bisInspAllRlationParams,
            onChange: handleSelectionChange,
          }"
          :columns="columns"
          :data-source="currentShowList"
          @change="handleCurrentChange"
          :loading="loading"
          rowKey="index"
          :pagination="{
            total,
            defaultPageSize: pageSize,
            showTotal: (total) => `共${total}条`,
          }"
          ref="menTable"
        >
        </a-table>
      </a-layout-content>
    </a-layout>
  </a-modal>
</template>

<script>
import { addDczOthersMens, getArenGrpMen } from '../../api/duChaPlan.js';
import request from '../../utils/gw_ajax_request.js';
import { dateFormat } from '../../utils/gw_date.js';
export default {
  props: ['objType', 'dczId', 'dczName', 'jiGouId', 'addPerType', 'psn'],
  data() {
    return {
      dialogFormVisible: false,
      addOnce: false,
      total: 0,
      addArr: '',
      pageSize: 10,
      pageIndex: 1,
      form: {
        searchText: '',
        jgName: '',
      },
      currentShowList: [],
      multipleSelection: [],
      multipleSelectionAll: [],
      idKey: 'guid',
      searchProvTreeListCode: '',
      rememberselectAllIds: [],
      dcGroupId: '',
      loading: false,
      bisInspAllRlationParams: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          ellipsis: true,
          width: '60px',
          customCell: () => {
            return {
              style: {
                'min-width': '60px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '60px',
              },
            };
          },
        },
        {
          title: '人员姓名',
          dataIndex: 'persName',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '100px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '100px',
              },
            };
          },
        },
        {
          title: '性别',
          dataIndex: 'sex1',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '100px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '100px',
              },
            };
          },
        },
        {
          title: '手机号码',
          dataIndex: 'mobilenumb',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
        {
          title: '机构名称',
          dataIndex: 'orgName',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
        {
          title: '所属部门',
          dataIndex: 'dpnm',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
        {
          title: '职务级别',
          dataIndex: 'admDuty',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
        {
          title: '职称',
          dataIndex: 'dppost',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
        {
          title: '专业',
          dataIndex: 'specialty',
          align: 'center',
          ellipsis: true,
          customCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
          customHeaderCell: () => {
            return {
              style: {
                'min-width': '80px',
              },
            };
          },
        },
      ],
    };
  },
  mounted() {
    if (this.objType !== '8') {
      this.columns = this.columns
        .filter((item) => item.dataIndex !== 'dppost')
        .filter((item) => item.dataIndex !== 'specialty');
    }
  },
  methods: {
    showDialog() {
      this.dialogFormVisible = true;
      this.getTableData();
      this.addArr = '';
    },
    addMens() {
      //debugger
      this.changePageCoreRecordData();
      var mohurd = '';
      if (this.addOnce) {
        this.addOnce = false;
        return;
      }
      this.addOnce = true;
      const length = this.multipleSelection.length;
      for (let i = 0; i < length; i++) {
        this.addArr += this.multipleSelection[i].guid + ',';
        mohurd += this.multipleSelection[i].mohurd + ',';
      }

      if (this.addPerType) {
        const newpersAllDtoList = [];
        this.multipleSelection.forEach((element) => {
          newpersAllDtoList.push({
            guid: element.guid,
            persName: element.persName,
            persType: this.addPerType,
          });
        });
        request
          .post('/supervision/plan/insertSupervisionObj', {
            id: this.dcGroupId,
            persAllAreaDtoList: [],
            persAllDtoList: newpersAllDtoList,
            persAllObjDtoList: [],
            // "pid": _self.tableCurrentRow.pid
          })
          .then((response) => {
            this.$message.success('添加专家成功');
            this.$emit('addSuccess');
            this.dialogFormVisible = false;
            this.bisInspAllRlationParams = [];
          });
      } else {
        if (this.addArr) {
          try {
            sessionStorage.setItem('Authorization', '1');
            addDczOthersMens(
              this.addArr,
              this.dcGroupId,
              mohurd,
              this.psn,
            ).then((response) => {
              if (response.success) {
                this.addOnce = false;
                // this.getTableData();
                this.$message.success('添加成功');
                this.$emit('addSuccess');
                this.dialogFormVisible = false;
                this.bisInspAllRlationParams = [];
              } else {
                this.$message.error(response.message);
              }
            });
          } catch (e) {
            this.$message.error('添加失败，请稍后再试');
          }
        } else {
          this.$message.error('请选择要添加的人员');
        }
      }
    },
    getTableData() {
      // 获取非当前督查需人员列表
      if (!this.dczId) {
        this.dcGroupId = 'jwW2Wihf5E9X3UC9t45fmSHyffUs2rzI';
      } else {
        this.dcGroupId = this.dczId;
      }
      if (!this.jiGouId) {
        this.jiGouId = '4355A1641209412DA5534DE05A941BCC';
      }
      this.loading = true;
      getArenGrpMen(
        this.dcGroupId.substring(3, 6),
        this.dcGroupId,
        this.pageSize,
        this.pageIndex,
        this.form.searchText,
        this.searchProvTreeListCode,
        this.form.jgName,
      ).then((response) => {
        var arr = response.data.list;
        arr.forEach((arrItem, index) => {
          arrItem['index'] = this.pageSize * (this.pageIndex - 1) + index + 1;
          arrItem['sex1'] =
            arrItem.sex == '1' ? '男' : arrItem.sex == '2' ? '女' : '';
          arrItem['modifyTime1'] =
            arrItem.modifyTime == null
              ? ''
              : dateFormat(arrItem.modifyTime, 'yyyy-MM-DD');
        });
        this.currentShowList = response.data.list;
        this.loading = false;
        this.total = response.data.total;
      });
    },
    handleSelectionChange(val, data) {
      this.bisInspAllRlationParams = val;
      this.multipleSelection = data;
      if (!this.multipleSelection || this.multipleSelection.length <= 0) {
        return;
      }
      // 标识当前行的唯一键的名称
      const idKey = this.idKey;
      const selectAllIds = [];
      this.multipleSelection.forEach((row) => {
        selectAllIds.push(row[idKey]);
      });
      this.rememberselectAllIds =
        this.rememberselectAllIds.concat(selectAllIds);
    },
    handleCurrentChange(item) {
      this.pageIndex = item.current;
      this.getTableData();
    },
    // 记忆选择核心方法
    changePageCoreRecordData() {
      // 标识当前行的唯一键的名称
      const idKey = this.idKey;
      // 如果总记忆中还没有选择的数据，那么就直接取当前页选中的数据，不需要后面一系列计算
      if (this.multipleSelectionAll.length <= 0) {
        this.multipleSelectionAll = this.multipleSelection;
        return;
      }
      // 总选择里面的key集合
      const selectAllIds = [];
      this.multipleSelectionAll.forEach((row) => {
        selectAllIds.push(row[idKey]);
      });
      const selectIds = [];
      // 获取当前页选中的id
      this.multipleSelection.forEach((row) => {
        selectIds.push(row[idKey]);
        // 如果总选择里面不包含当前页选中的数据，那么就加入到总选择集合里
        if (selectAllIds.indexOf(row[idKey]) < 0) {
          this.multipleSelectionAll.push(row);
        }
      });
      const noSelectIds = [];
      // 得到当前页没有选中的id
      this.currentShowList.forEach((row) => {
        if (selectIds.indexOf(row[idKey]) < 0) {
          noSelectIds.push(row[idKey]);
        }
      });
      noSelectIds.forEach((id) => {
        if (selectAllIds.indexOf(id) >= 0) {
          for (let i = 0; i < this.multipleSelectionAll.length; i++) {
            if (this.multipleSelectionAll[i][idKey] == id) {
              // 如果总选择中有未被选中的，那么就删除这条
              this.multipleSelectionAll.splice(i, 1);
              break;
            }
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
:deep(.ant-table-pagination) {
  float: left;
}
:deep(.ant-table-thead > tr > th),
:deep(.ant-table-tbody > tr > td) {
  padding: 0;
  height: 36px;
}
</style>
