<template>
  <span>
    <audit-form-top-banner :handel-save-form="saveForm" @handelSaveForm="saveForm">
      <div class="title_sd-detail-form_common">模型授权</div>
      <table
        ><tr
          ><td style="border:none">
            <audit-advanced-group
              :expand="maintainExpand"
              :expand-str="'maintainExpand'"
              :group-label="'模型列表'"
              tablestyle="''"
              @changedClick="changedClick"
            ></audit-advanced-group> </td></tr
      ></table>

      <sd-table
        v-if="maintainExpand && maintaindata.length > 0"
        ref="maintaintable"
        :row-key="(record, index) => index"
        :loading="false"
        class="maintaintable"
        :columns="columns"
        :data-source="maintaindata"
        :pagination="false"
        :bordered="true"
        :row-selection="{
          getCheckboxProps(record) {
            return {
              props: { disabled: !showSelection(record) },
            }
          },
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      />
      <table>
        <tr v-if="unit.length > 1">
          <td>
            <audit-advanced-group
              :expand="unitExpand"
              :expand-str="'unitExpand'"
              :group-label="'授权单位'"
              tablestyle="''"
              @changedClick="changedClick"
              ><template>
                <template>
                  <div :class="$style.wrapper">
                    <div v-show="unitExpand" :class="$style.btnselect">
                      <a-button type="link" :class="$style.batchselect" @click="dwRelSelect">
                        <a-icon type="check-circle" :theme="'filled'" />
                        添加
                      </a-button>
                    </div>
                  </div>
                </template>
                <XmChildTable
                  ref="udata"
                  v-model="unitdata"
                  :label="null"
                  :columns="unitcolumns"
                  :fields="
                    [
                      {
                        caption: '序号',
                        name: 'sortNum',
                        dataType: 'sortNum',
                      },
                    ].concat(unit)
                  "
                  :addbuttonvisiable="false"
                >
                  <template v-slot:unitName="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:unitCode="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:empowerType="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'udata_empowerType_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="type"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:startDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'udata_startDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="stime"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:endDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'udata_endDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="etime"
                      @change="setchange"
                    />
                  </template> </XmChildTable
              ></template>
            </audit-advanced-group> </td
        ></tr>
      </table>

      <table>
        <tr v-if="personal.length > 1">
          <td
            ><audit-advanced-group
              :expand="personalExpand"
              :expand-str="'personalExpand'"
              :group-label="'授权人员'"
              tablestyle="''"
              @changedClick="changedClick"
              ><template>
                <template>
                  <div :class="$style.wrapper">
                    <div v-show="personalExpand" :class="$style.btnselect">
                      <a-button type="link" :class="$style.batchselect" @click="pRelSelect">
                        <a-icon type="check-circle" :theme="'filled'" />
                        添加
                      </a-button>
                    </div>
                  </div>
                </template>
                <XmChildTable
                  ref="pdata"
                  v-model="personaldata"
                  :label="null"
                  :columns="personalcolumns"
                  :fields="
                    [
                      {
                        caption: '序号',
                        name: 'sortNum',
                        dataType: 'sortNum',
                      },
                    ].concat(personal)
                  "
                  :addbuttonvisiable="false"
                >
                  <template v-slot:userName="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:userUnit="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:empowerType="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'pdata_empowerType_' + index"
                      :data="'pdata'"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="type"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:startDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'pdata_startDate_' + index"
                      :data="'pdata'"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="stime"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:endDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'pdata_endDate_' + index"
                      :data="'pdata'"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      lx="etime"
                      @change="setchange"
                    />
                  </template> </XmChildTable></template
            ></audit-advanced-group> </td
        ></tr>
      </table>

      <!-- 项目 -->

      <table>
        <tr v-if="projectTable.length > 1">
          <td
            ><audit-advanced-group
              :expand="projectExpand"
              :expand-str="'projectExpand'"
              :group-label="'项目授权'"
              tablestyle="''"
              @changedClick="changedClick"
              ><template>
                <template>
                  <div :class="$style.wrapper">
                    <div v-show="projectExpand" :class="$style.btnselect">
                      <a-button type="link" :class="$style.batchselect" @click="projectShow = true">
                        <a-icon type="check-circle" :theme="'filled'" />
                        添加
                      </a-button>
                    </div>
                  </div>
                </template>
                <XmChildTable
                  ref="prodata"
                  v-model="projectData"
                  :label="null"
                  :columns="projectColums"
                  :fields="
                    [
                      {
                        caption: '序号',
                        name: 'sortNum',
                        dataType: 'sortNum',
                      },
                    ].concat(projectTable)
                  "
                  :addbuttonvisiable="false"
                >
                  <template v-slot:projectName="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:projectUnit="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:empowerType="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'prodata_empowerType_' + index"
                      :value="value[index]"
                      :index="index"
                      :data="'prodata'"
                      :field="field"
                      lx="type"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:startDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'prodata_startDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'prodata'"
                      lx="stime"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:endDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'prodata_endDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'prodata'"
                      lx="etime"
                      @change="setchange"
                    />
                  </template>
                </XmChildTable> </template
            ></audit-advanced-group> </td
        ></tr>
      </table>

      <!-- 角色 -->
      <table>
        <tr v-if="roleTable.length > 1">
          <td
            ><audit-advanced-group
              :expand="roleExpand"
              :expand-str="'roleExpand'"
              :group-label="'授权角色'"
              tablestyle="''"
              @changedClick="changedClick"
              ><template>
                <template>
                  <div :class="$style.wrapper">
                    <div v-show="roleExpand" :class="$style.btnselect">
                      <a-button type="link" :class="$style.batchselect" @click="roleShow = true">
                        <a-icon type="check-circle" :theme="'filled'" />
                        添加
                      </a-button>
                    </div>
                  </div>
                </template>
                <XmChildTable
                  ref="rdata"
                  v-model="roleData"
                  :label="null"
                  :columns="roleColumns"
                  :fields="
                    [
                      {
                        caption: '序号',
                        name: 'sortNum',
                        dataType: 'sortNum',
                      },
                    ].concat(roleTable)
                  "
                  :addbuttonvisiable="false"
                >
                  <template v-slot:roleName="{ field, text, index, value }">
                    {{ text }}
                  </template>
                  <template v-slot:empowerType="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'rdata_empowerType_' + index"
                      :value="value[index]"
                      :index="index"
                      :data="'rdata'"
                      :field="field"
                      lx="type"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:startDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'rdata_startDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'rdata'"
                      lx="stime"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:endDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'rdata_endDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'rdata'"
                      lx="etime"
                      @change="setchange"
                    />
                  </template>
                </XmChildTable> </template
            ></audit-advanced-group> </td
        ></tr>
      </table>

      <div v-show="false">
        <sd-group-picker
          ref="dwRelSelect"
          v-model="mainSendUnit"
          :single="false"
          :read-only="false"
          @change="changeunit"
        />
      </div>

      <div v-show="false">
        <sd-user-picker
          ref="pRelSelect"
          v-model="mainSendPersonal"
          :single="false"
          :read-only="false"
          @change="changep"
        />
      </div>
    </audit-form-top-banner>
    <roleModal v-model="roleShow" @selectRoleList="changeRole" />
    <projectModal v-model="projectShow" @selectProject="changeProject" />
  </span>
</template>

<script>
import { Modal, Message } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import axios from '@/common/services/axios-instance'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import auditWarrantInput from './audit-warrant-input.vue'
import auditMaintainService from './audit-maintain-service'
import components from './_import-components/audit-warrant-from-import'
import XmChildTable from '../../components/xm-child-table.vue'
import roleModal from '../dataUphold/role-modal.vue'
import projectModal from '../dataUphold/project-modal.vue'
import moment from 'moment'
export default {
  name: 'AuditWarrantFrom',
  metaInfo: {
    title: '模型授权',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    auditWarrantInput,
    XmChildTable,
    roleModal,
    projectModal,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      mainSendUnit: [],
      mainSendPersonal: [],
      mainSendProject: [],
      selectedRowKeys: [],
      selectid: '',
      maintaindata: [],
      unitdata: [],
      personaldata: [],
      empowerProjectData: [],

      maintainExpand: true,
      unitExpand: true,
      personalExpand: true,
      roleExpand: true,
      projectExpand: true,
      personal: [],
      unit: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          key: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '模型名称',
          dataIndex: 'modelName',
          key: 'modelName',
          width: '30%',
        },
        {
          title: '模型编号',
          dataIndex: 'modelCode',
          key: 'modelCode',
          width: '30%',
        },
        {
          title: '模型描述',
          dataIndex: 'modelDesc',
          key: 'modelDesc',
          width: '30%',
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      unitcolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },

        {
          title: '单位编号',
          dataIndex: 'unitCode',
          sdHidden: false,
        },
        {
          title: '单位名称',
          dataIndex: 'unitName',
          sdHidden: false,
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: false,
        },
        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: false,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: false,
        },
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'unitOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'unitId',
          sdHidden: true,
        },
        {
          dataIndex: 'selectOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'belongedOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'docType',
          sdHidden: true,
        },
        {
          dataIndex: 'maintainId',
          sdHidden: true,
        },
        {
          dataIndex: 'applyId',
          sdHidden: true,
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      personalcolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '用户名',
          dataIndex: 'userName',
          sdHidden: false,
        },
        {
          title: '所属单位',
          dataIndex: 'userUnit',
          sdHidden: false,
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: false,
        },
        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: false,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: false,
        },
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'userId',
          sdHidden: true,
        },
        {
          dataIndex: 'userOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'selectOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'belongedOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'docType',
          sdHidden: true,
        },
        {
          dataIndex: 'maintainId',
          sdHidden: true,
        },
        {
          dataIndex: 'applyId',
          sdHidden: true,
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      roleColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: false,
        },
        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: false,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: false,
        },
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'roleId',
          sdHidden: true,
        },
        {
          dataIndex: 'selectOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'belongedOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'docType',
          sdHidden: true,
        },
        {
          dataIndex: 'maintainId',
          sdHidden: true,
        },
        {
          dataIndex: 'applyId',
          sdHidden: true,
        },
        {
          dataIndex: 'roleOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      projectColums: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: false,
        },

        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: false,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: false,
        },
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'roleId',
          sdHidden: true,
        },
        {
          dataIndex: 'selectOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'belongedOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'docType',
          sdHidden: true,
        },
        {
          dataIndex: 'maintainId',
          sdHidden: true,
        },
        {
          dataIndex: 'applyId',
          sdHidden: true,
        },
        {
          dataIndex: 'roleOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'projectOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'projectId',
          sdHidden: true,
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      roleData: [],
      roleTable: [],
      roleShow: false,
      projectShow: false,
      projectData: [],
      projectTable: [],
      companyExpand: true,
      companyData: [],
      companycolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '公司名称',
          dataIndex: 'unitName',
          sdHidden: false,
        },
        {
          title: '公司编号',
          dataIndex: 'unitCode',
          sdHidden: false,
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: true,
        },
        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: true,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: true,
        },
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          dataIndex: 'unitOpt',
          sdHidden: true,
        },
        {
          dataIndex: 'unitId',
          sdHidden: true,
        },
        {
          dataIndex: 'selectOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'belongedOrgId',
          sdHidden: true,
        },
        {
          dataIndex: 'docType',
          sdHidden: true,
        },
        {
          dataIndex: 'maintainId',
          sdHidden: true,
        },
        {
          dataIndex: 'applyId',
          sdHidden: true,
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
    }
  },
  created() {
    if (this.$route.query.selectid) {
      this.selectid = this.$route.query.selectid
      this.getmxselect(this.selectid)
      // 获取人员和单位 field
      this.getfields()
    }
  },
  methods: {
    setchange(value, field, index, lx, data) {
      this.$refs[data].$refs[field.name + '_' + index][0].setFieldValue(field.name, value)
      if (data === 'udata') {
        this.unitdata[index][field.name] = value
      } else if (data === 'pdata') {
        this.personaldata[index][field.name] = value
      } else if (data === 'rdata') {
        this.roleData[index][field.name] = value
      } else if (data === 'prodata') {
        this.projectData[index][field.name] = value
      }

      if (lx === 'type') {
        // 设置 时间两个条件
        if (value === '1') {
          this.$refs[data + '_startDate_' + index].disabled = true
          this.$refs[data + '_endDate_' + index].disabled = true
          this.$refs[data + '_endDate_' + index].rules = [
            { required: false, trigger: ['change', 'blur'] },
          ]
          this.$refs[data + '_startDate_' + index].rules = [
            { required: false, trigger: ['change', 'blur'] },
          ]
        } else {
          this.$refs[data + '_startDate_' + index].disabled = false
          this.$refs[data + '_endDate_' + index].disabled = false
          this.$refs[data + '_endDate_' + index].rules = [
            { required: true, trigger: ['change', 'blur'] },
          ]
          this.$refs[data + '_startDate_' + index].rules = [
            { required: true, trigger: ['change', 'blur'] },
          ]
        }
        if (value === '2') {
          this.$refs[data + '_startDate_' + index].disabled = true
          this.$refs[data + '_endDate_' + index].disabled = true
          this.$refs[data + '_endDate_' + index].rules = [
            { required: false, trigger: ['change', 'blur'] },
          ]

          // this.$refs[data + '_endDate_' + index].model.startDate = moment().endOf
          this.$refs[data + '_startDate_' + index].model.startDate = moment()
          this.$refs[data + '_endDate_' + index].model.endDate = moment().endOf('year')
          this.$refs[data + '_startDate_' + index].startDate = moment()
          this.$refs[data + '_endDate_' + index].endDate = moment().endOf('year')
        }
      } else if (lx === 'stime') {
        // 设置 开始时间
        this.$refs[data + '_endDate_' + index].time = new Date(value).getTime()
      } else {
        this.$refs[data + '_startDate_' + index].time = new Date(value).getTime()
      }
    },
    savetableh(index, field, data) {
      this.$refs[data].savetableh(index, field)
    },
    changeunit(values) {
      let iamAuditDwMxs = []
      iamAuditDwMxs = this.unitdata
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.unitId === element.code) === -1) {
          const mmc = {}
          mmc.unitName = element.name
          // mmc.unitCode = element.props.code
          mmc.unitCode = element.code
          mmc.unitId = element.code
          iamAuditDwMxs.push(mmc)
        }
      })
      this.unitdata = iamAuditDwMxs
    },
    changep(values) {
      let iamAuditPMxs = []
      iamAuditPMxs = this.personaldata
      values.forEach((element) => {
        if (iamAuditPMxs.findIndex((dw) => dw.userId === element.code) === -1) {
          const mmc = {}
          mmc.userName = element.name
          mmc.userId = element.code
          axios({
            url: 'api/xcoa-mobile/v1/iammodelmaintain/findGroupByAccount?account=' + element.code,
            method: 'get',
          }).then((res) => {
            mmc.userUnit = res.data.name
            iamAuditPMxs.push(mmc)
          })
        }
      })
      this.personaldata = iamAuditPMxs
    },

    // 选择申请单位
    dwRelSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.dwRelSelect.openPicker()
      }, 0)
    },
    pRelSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.pRelSelect.openPicker()
      }, 0)
    },
    roleSelect() {
      // 选择申请角色
      setTimeout(() => {
        this.$refs.roleSelect.openPicker()
      }, 0)
    },
    companylSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.cRelSelect.openPicker()
      }, 0)
    },
    saveForm() {
      const udata = []
      const pdata = []
      const rdata = []
      const prodata = []
      const cdata = []
      this.unitdata.forEach((item) => {
        let sl = null
        let el = null
        if (item.empowerType === '0') {
          if (item.startDate !== undefined) {
            const s = new Date(item.startDate)
            const e = new Date(item.endDate)
            sl = s.getTime()
            el = e.getTime()
          }
        }
        if (item.empowerType === '2') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().endOf('year')).getTime()
        }
        udata.push({
          docType: 'model',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          unitCode: item.unitCode,
          unitName: item.unitName,
        })
      })
      this.personaldata.forEach((item) => {
        let sl = null
        let el = null
        if (item.empowerType === '0') {
          if (item.startDate !== undefined) {
            const s = new Date(item.startDate)
            const e = new Date(item.endDate)
            sl = s.getTime()
            el = e.getTime()
          }
        }
        if (item.empowerType === '2') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().endOf('year')).getTime()
        }
        pdata.push({
          docType: 'model',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          userId: item.userId,
          userUnit: item.userUnit,
          userName: item.userName,
        })
      })
      this.roleData.forEach((item) => {
        let sl = null
        let el = null
        if (item.empowerType === '0') {
          if (item.startDate !== undefined) {
            const s = new Date(item.startDate)
            const e = new Date(item.endDate)
            sl = s.getTime()
            el = e.getTime()
          }
        }
        if (item.empowerType === '2') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().endOf('year')).getTime()
        }
        rdata.push({
          docType: 'model',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          roleId: item.roleId,
          roleName: item.roleName,
        })
      })
      this.projectData.forEach((item) => {
        let sl = null
        let el = null
        if (item.empowerType === '0') {
          if (item.startDate !== undefined) {
            const s = new Date(item.startDate)
            const e = new Date(item.endDate)
            sl = s.getTime()
            el = e.getTime()
          }
        }
        if (item.empowerType === '2') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().endOf('year')).getTime()
        }
        prodata.push({
          docType: 'model',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          projectId: item.projectId,
          projectName: item.projectName,
        })
      })

      this.$refs.udata.valid().then((res) => {
        if (res) {
          this.$refs.pdata.valid().then((res) => {
            if (res) {
              this.$refs.prodata.valid().then((res) => {
                if (res) {
                  this.$refs.rdata.valid().then((res) => {
                    if (res) {
                      if (this.selectedRowKeys.length === 0) {
                        Modal.warning({
                          title: '提示',
                          content: '请勾选需要授权的模型!',
                        })
                        return false
                      }
                      // 如果 授权的都没有数据则提示至少又一个授权项
                      if (
                        udata.length === 0 &&
                        pdata.length === 0 &&
                        prodata.length === 0 &&
                        rdata.length === 0
                      ) {
                        Modal.warning({
                          title: '提示',
                          content: '请至少选择一个授权项!',
                        })
                        return false
                      }
                      const selectedRow = []
                      this.selectedRowKeys.forEach((i) => selectedRow.push(this.maintaindata[i]))
                      const params = {
                        iamModelMaintainEntitys: selectedRow,
                        iamLicenseUnitEntitys: udata,
                        iamLicensePeopleEntitys: pdata,
                        iamLicenseProjectEntitys: prodata,
                        iamLicenseRoleEntitys: rdata,
                      }

                      auditMaintainService.saveIamAuditMaintain(params).then((res) => {
                        if (res) {
                          Message.success('保存成功', 1).then((res) => {
                            this.close(true)
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          })
        }
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    showSelection() {
      return true
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    getfields() {
      axios({
        url: 'api/xcoa-mobile/v1/page/wp/audit/maintain/iamLicensePeople',
        method: 'get',
      }).then((res) => {
        this.personal = res.data.pageFormData.pageFieldInfos
      })
      axios({
        url: 'api/xcoa-mobile/v1/page/wp/audit/maintain/iamLicenseUnit',
        method: 'get',
      }).then((res) => {
        this.unit = res.data.pageFormData.pageFieldInfos
        this.unit.find((item) => item.name === 'empowerType').attr.mandatory = 'false'
      })

      axios({
        url: 'api/xcoa-mobile/v1/page/wp/audit/maintain/iamLicenseRole',
        method: 'get',
      }).then((res) => {
        this.roleTable = res.data.pageFormData.pageFieldInfos
        this.roleTable.find((item) => item.name === 'empowerType').attr.mandatory = 'false'
      })
      axios({
        url: 'api/xcoa-mobile/v1/page/wp/audit/maintain/iamLicenseProject',
        method: 'get',
      }).then((res) => {
        this.projectTable = res.data.pageFormData.pageFieldInfos
        this.projectTable.find((item) => item.name === 'empowerType').attr.mandatory = 'false'
      })
    },
    getmxselect(ids) {
      const arrid = ids.split(',')
      arrid.forEach((id) => {
        auditMaintainService.findIamAuditMaintain(id).then((data) => {
          this.maintaindata.push(data)
        })
      })
    },
    changeRole(values) {
      let iamAuditDwMxs = []
      iamAuditDwMxs = this.roleData
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.roleId === element.id) === -1) {
          const mmc = {}
          mmc.roleName = element.name
          mmc.roleId = element.id
          iamAuditDwMxs.push(mmc)
        }
      })
      this.roleData = iamAuditDwMxs
    },
    changeProject(values) {
      let iamAuditDwMxs = []
      iamAuditDwMxs = this.projectData
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.projectId === element.id) === -1) {
          const mmc = {}
          mmc.projectName = element.projectTitle
          mmc.projectId = element.id
          mmc.projectUnit = element.leadUnitName
          iamAuditDwMxs.push(mmc)
        }
      })
      this.projectData = iamAuditDwMxs
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.btnselect {
  position: relative;
  top: 4px;
  right: 80px;
  float: right;
  padding: 4px 0;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
:global(.maintaintable) {
  :global(.ant-table-tbody > tr > td) {
    border-right: none;
    border-left: none;
  }
  :global(.ant-table-thead > tr > th) {
    border-right: none;
    border-left: none;
  }
  :global(table) {
    border: none !important;
  }
  :global(.ant-table-placeholder) {
    border: none !important;
  }
}
</style>
