<template>
  <span>
    <sd-webflow
      ref="docform"
      :validate-form="validForm"
      :removed-tabs="['sdRelatedDoc']"
      :class="$style.form"
      @actionBtnClick="actionBtnClick"
    >
      <template v-slot:form="{ model, fields, FlowData }">
        <table>
          <colgroup>
            <col style="width: 15%;"/>
            <col style="width: 35%;"/>
            <col style="width: 15%;"/>
            <col style="width: 35%;"
          /></colgroup>
          <tr v-show="false">
            <!-- 标题 -->
            <sd-form-item-td name="title" :colspan="3" />
          </tr>
          <tr>
            <td colspan="4" style="border:none">
              <audit-advanced-group
                :expand="expandJ"
                :expand-str="'expandJ'"
                :group-label="'基本信息'"
                @changedClick="changedClick"
              />
            </td>
          </tr>
          <tr v-show="expandJ">
            <!-- 申请人 -->
            <sd-form-item-td name="creatorName" />
            <!-- 公司 -->
            <sd-form-item-td name="createDeptName" />
          </tr>
          <tr v-show="expandJ">
            <!-- 申请时间 -->
            <sd-form-item-td name="creationTime" />
            <!-- 授权类型 -->
            <sd-form-item-td name="empowerType">
              <template>
                <a-select v-model="model.empowerType" @change="changeType">
                  <a-select-option
                    v-for="(key, i) in fields.empowerType.attr.selectListItem"
                    :key="i"
                    :value="key.value"
                  >
                    {{ key.label }}
                  </a-select-option>
                </a-select>
              </template>
            </sd-form-item-td>
          </tr>

          <tr v-if="(model.empowerType === '1' || model.empowerType === '2') && expandJ">
            <td class="ant-form-item-label ant-form-item-label">
              <label title="开始日期"> 开始日期 </label>
            </td>
            <td class="ant-form-item-control-wrapper">
              <a-date-picker v-model="model.startDate" :disabled="true" />
            </td>
            <td class="ant-form-item-label ant-form-item-label">
              <label title="结束日期"> 结束日期 </label>
            </td>
            <td class="ant-form-item-control-wrapper">
              <a-date-picker v-model="model.endDate" :disabled="true" />
            </td>
          </tr>
          <tr v-if="model.empowerType !== '1' && model.empowerType !== '2' && expandJ">
            <!-- 授权开始时间 -->
            <!-- <sd-form-item-td name="startDate" /> -->
            <!-- 授权截止时间 -->
            <!-- <sd-form-item-td name="endDate" /> -->
            <td class="ant-form-item-label ant-form-item-label">
              <label title="开始日期" class="ant-form-item-required"> 开始日期 </label>
            </td>
            <td class="ant-form-item-control-wrapper">
              <a-form-model-item :label="null" :rules="rules" prop="startDate">
                <a-date-picker v-model="model.startDate" />
              </a-form-model-item>
            </td>
            <td class="ant-form-item-label ant-form-item-label">
              <label title="结束日期" class="ant-form-item-required"> 结束日期 </label>
            </td>
            <td class="ant-form-item-control-wrapper">
              <a-form-model-item :label="null" :rules="rules" prop="endDate">
                <a-date-picker v-model="model.endDate" />
              </a-form-model-item>
            </td>
          </tr>
          <tr v-show="expandJ">
            <!-- 申请原因 -->
            <sd-form-item-td name="empowerReason" :colspan="3">
              <a-textarea v-model="model.empowerReason" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr
            ><td
              v-if="getmaintaindata(model, fields)"
              :colspan="4"
              style="line-height:40px;border:none1;"
              :class="$style.maintaintable"
            >
              <audit-advanced-group
                :expand="expandModel"
                :expand-str="'expandY'"
                :group-label="'模型列表'"
                @changedClick="changedClick"
                ><template>
                  <!-- </td></tr><tr><td v-if="getmaintaindata(model,fields)" :colspan="4" :class="$style.maintaintable"  style="border:none"> -->
                  <div :class="$style.wrapper">
                    <div v-show="expandModel && maintaineditable" :class="$style.btnselectmaintain">
                      <a-button type="link" :class="$style.batchselect" @click="mxRelSelect">
                        <a-icon type="check-circle" :theme="'filled'" />
                        选择
                      </a-button>
                      <a-button
                        type="link"
                        :disabled="selectedRowKeys.length === 0"
                        style="z-index:10"
                        @click="removemx(selectedRowKeys)"
                      >
                        <a-icon type="minus-circle" :theme="'filled'" />
                        删除
                      </a-button>
                    </div>
                  </div>
                  <sd-table
                    v-show="expandModel"
                    ref="maintaintable"
                    :row-key="(record, index) => index"
                    :loading="false"
                    class="maintaintable"
                    :columns="modelChildMXColumns"
                    :data-source="maintaindata"
                    :pagination="false"
                    :bordered="true"
                    :row-selection="
                      maintaineditable
                        ? {
                            getCheckboxProps(record) {
                              return {
                                props: { disabled: !showSelection(record) },
                              }
                            },
                            selectedRowKeys: selectedRowKeys,
                            onChange: onSelectChange,
                          }
                        : null
                    "/></template
              ></audit-advanced-group>
            </td>
          </tr>

          <tr
            ><td :colspan="4" style="border:none1">
              <sd-form-item name="iamLicenseUnitEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="expandW"
                    :expand-str="'expandW'"
                    :group-label="'申请单位'"
                    @changedClick="changedClick"
                    ><template>
                      <template>
                        <div :class="$style.wrapper">
                          <div v-show="expandW && editable" :class="$style.btnselect">
                            <a-button type="link" :class="$style.batchselect" @click="dwRelSelect">
                              <a-icon type="check-circle" :theme="'filled'" />
                              选择
                            </a-button>
                          </div>
                        </div>
                      </template>
                      <xm-child-table
                        v-model="model.iamLicenseUnitEntitys"
                        :read-only="!editable"
                        label=""
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'sortNum',
                            },
                          ].concat(Array.from(fields.iamLicenseUnitEntitys.attr.dync))
                        "
                        :columns="childDWColumns"
                        :width="1200"
                        :addbuttonvisiable="false"
                        @change="saveForm"
                      >
                        <template v-slot:unitName="{ field, text, index, value }">
                          {{ text }}
                        </template>
                        <template v-slot:unitCode="{ field, text, index, value }">
                          {{ text }}
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item></td
            >
          </tr>
          <tr
            ><td :colspan="4" style="border:none1">
              <sd-form-item name="iamLicenseUnitEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="expandPeople"
                    :expand-str="'expandPeople'"
                    :group-label="'申请用户'"
                    @changedClick="changedClick"
                    ><template>
                      <template>
                        <div :class="$style.wrapper">
                          <div v-show="expandPeople && editable" :class="$style.btnselect">
                            <a-button
                              type="link"
                              :class="$style.batchselect"
                              @click="peopleRelSelect"
                            >
                              <a-icon type="check-circle" :theme="'filled'" />
                              选择
                            </a-button>
                          </div>
                        </div>
                      </template>
                      <xm-child-table
                        v-model="model.iamLicensePeopleEntitys"
                        :read-only="!editable"
                        label=""
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'sortNum',
                            },
                          ].concat(Array.from(fields.iamLicensePeopleEntitys.attr.dync))
                        "
                        :columns="peopleColumns"
                        :width="1200"
                        :addbuttonvisiable="false"
                        @change="saveForm"
                      >
                        <template v-slot:userName="{ field, text, index, value }">
                          {{ text }}
                        </template>
                        <template v-slot:userUnit="{ field, text, index, value }">
                          {{ text }}
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item></td
            >
          </tr>
          <tr
            ><td :colspan="4" style="border:none1">
              <sd-form-item name="iamLicenseUnitEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="expandProject"
                    :expand-str="'expandProject'"
                    :group-label="'申请项目'"
                    @changedClick="changedClick"
                    ><template>
                      <template>
                        <div :class="$style.wrapper">
                          <div v-show="expandProject && editable" :class="$style.btnselect">
                            <a-button
                              type="link"
                              :class="$style.batchselect"
                              @click="projectRelSelect"
                            >
                              <a-icon type="check-circle" :theme="'filled'" />
                              选择
                            </a-button>
                          </div>
                        </div>
                      </template>
                      <xm-child-table
                        v-model="model.iamLicenseProjectEntitys"
                        :read-only="!editable"
                        label=""
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'sortNum',
                            },
                          ].concat(Array.from(fields.iamLicenseProjectEntitys.attr.dync))
                        "
                        :columns="projectColumns"
                        :width="1200"
                        :addbuttonvisiable="false"
                        @change="saveForm"
                      >
                        <template v-slot:projectName="{ field, text, index, value }">
                          {{ text }}
                        </template>
                        <template v-slot:projectUnit="{ field, text, index, value }">
                          {{ text }}
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item></td
            >
          </tr>
          <tr
            ><td :colspan="4" style="border:none1">
              <sd-form-item name="iamLicenseUnitEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="expandRole"
                    :expand-str="'expandRole'"
                    :group-label="'申请角色'"
                    @changedClick="changedClick"
                    ><template>
                      <template>
                        <div :class="$style.wrapper">
                          <div v-show="expandRole && editable" :class="$style.btnselect">
                            <a-button
                              type="link"
                              :class="$style.batchselect"
                              @click="roleRelSelect"
                            >
                              <a-icon type="check-circle" :theme="'filled'" />
                              选择
                            </a-button>
                          </div>
                        </div>
                      </template>
                      <xm-child-table
                        v-model="model.iamLicenseRoleEntitys"
                        :read-only="!editable"
                        label=""
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'sortNum',
                            },
                          ].concat(Array.from(fields.iamLicenseRoleEntitys.attr.dync))
                        "
                        :columns="roleColumns"
                        :width="1200"
                        :addbuttonvisiable="false"
                        @change="saveForm"
                      >
                        <template v-slot:roleName="{ field, text, index, value }">
                          {{ text }}
                        </template>
                        <template v-slot:roleId="{ field, text, index, value }">
                          {{ text }}
                        </template>
                        <template v-slot:code="{ field, text, index, value }">
                          {{ text }}
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item></td
            >
          </tr>
          <tr v-show="false">
            <sd-form-item-td name="iamLicensePeopleEntitys" :label="null" :colspan="3">
            </sd-form-item-td>
          </tr>
        </table>
      </template>
    </sd-webflow>

    <AuditSelectMaintain :visible="visibleY" @listMxSelected="listMxSelected">
    </AuditSelectMaintain>
    <auditClassTable :visible="flTableShow" @listMxSelected="classListMxSelected">
    </auditClassTable>
    <div v-show="false">
      <sd-group-picker
        ref="dwRelSelect"
        v-model="mainSendUnit"
        :single="false"
        :read-only="false"
        @change="change"
      />
    </div>

    <roleModal v-model="roleShow" @selectRoleList="changeRole" />
    <projectModal v-model="projectShow" @selectProject="changeProject" />
    <div v-show="false">
      <sd-user-picker
        ref="peopleRelSelect"
        v-model="mainSendPersonal"
        :single="false"
        :read-only="false"
        @change="changePeople"
      />
    </div>
    <div v-show="false">
      <SdGroupPicker
        ref="cRelSelect"
        v-model="companyUnit"
        :single="false"
        :read-only="false"
        @change="changecompany"
      />
    </div>
  </span>
</template>

<script>
import { Modal, message } from 'ant-design-vue'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import auditMaintainService from './audit-maintain-service'
import AuditSelectMaintain from './audit-select-maintain.vue'
import roleModal from '../dataUphold/role-modal.vue'
import projectModal from '../dataUphold/project-modal.vue'
import components from './_import-components/audit-sqapply-from-import'
import auditClassTable from '../dataUphold/audit-class-table.vue'
import axios from '@/common/services/axios-instance'
import moment from 'moment'
export default {
  name: 'AuditSqapplyFrom',
  metaInfo: {
    title: '授权申请',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    AuditSelectMaintain,
    roleModal,
    projectModal,
    auditClassTable,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      mainSendUnit: [],
      mainSendPersonal: [],
      mainSendProject: [],
      // 控制是否展开
      expandY: true,
      expandW: true,
      expandJ: true,
      expandPeople: true,
      expandRole: true,
      expandProject: true,
      expandCompany: true,
      visibleY: false,
      maintaineditable: false,
      expandModel: true,

      modelChildMXColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '模型名称',
          dataIndex: 'modelName',
        },
        {
          title: '模型编号',
          dataIndex: 'modelCode',
        },
        {
          title: '风险描述',
          dataIndex: 'rick',
        },
      ],
      companyColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'maintainId', sdHidden: true },

        {
          title: '公司名称',
          dataIndex: 'unitName',
        },
        {
          title: '公司编号',
          dataIndex: 'unitCode',
        },
        {
          dataIndex: 'unitId',
          sdHidden: true,
        },
      ],
      childMXColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '分类名称',
          dataIndex: 'categoryName',
        },
        {
          title: '分类编号',
          dataIndex: 'categoryId',
        },
      ],
      childDWColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'maintainId', sdHidden: true },
        {
          title: '单位名称',
          dataIndex: 'unitName',
        },
        {
          title: '单位编号',
          dataIndex: 'unitCode',
        },
        {
          title: '单位Id',
          dataIndex: 'unitId',
          sdHidden: true,
        },
      ],
      peopleColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'maintainId', sdHidden: true },

        {
          title: '用户名称',
          dataIndex: 'userName',
        },
        // 所属单位
        {
          title: '所属单位',
          dataIndex: 'userUnit',
        },
        // userId
        {
          title: '用户Id',
          dataIndex: 'userId',
          sdHidden: true,
        },
      ],
      projectColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'maintainId', sdHidden: true },

        {
          title: '项目名称',
          dataIndex: 'projectName',
        },
        // 所属单位
        {
          title: '所属单位',
          dataIndex: 'projectUnit',
        },
        // userId
        {
          title: '项目Id',
          dataIndex: 'projectId',
          sdHidden: true,
        },
      ],
      roleColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'maintainId', sdHidden: true },

        {
          title: '角色名称',
          dataIndex: 'roleName',
        },
        // 角色编号
        {
          title: '角色编号',
          dataIndex: 'code',
        },
        {
          dataIndex: 'roleId',
          sdHidden: true,
        },
      ],
      maintaindata: [],
      selectedRowKeys: [],
      flag: true,
      modelFlag: true,
      classtable: false,
      rules: [
        {
          required: true,
          trigger: ['change', 'blur'],
        },
      ],
      // 控制是否弹出提示框
      roleShow: false,
      projectShow: false,
      peopleShow: false,
      CompanyShow: false,
      // flTableShow 是否显示分类表格
      flTableShow: false,
      // 分类列表
      classdata: [],
      // 人员
      personaldata: [],
      personal: [],

      projectData: [],
      projectTable: [],

      companyData: [],
      companyUnit: [],
      roleData: [],
      roleTable: [],
      docType: 'model_apply',
    }
  },
  watch: {
    classdata() {
      this.saveForm()
    },
  },
  mounted() {},
  methods: {
    changeType() {
      const empowerType = this.$refs.docform.getFieldValue('empowerType')
      if (empowerType === '1') {
        this.$refs.docform.setFieldValue('startDate', '')
        this.$refs.docform.setFieldValue('endDate', '')
      }
      if (empowerType === '2') {
        // 设置开始时间 为当前时间
        // 结束时间为当年最后一天
        // 使用moment格式
        const start = moment()
        const end = moment().endOf('year')
        this.$refs.docform.setFieldValue('startDate', start)
        this.$refs.docform.setFieldValue('endDate', end)
      }
      this.saveForm()
    },
    getmodelsqdata(model) {
      this.mainSendUnit = []
    },
    saveForm() {
      const save = new Promise((resolve) => {
        this.$refs.docform.setFieldValue('docType', this.docType)

        let udata = []
        if (this.$refs.docform.getFieldValue('iamLicenseUnitEntitys') !== undefined) {
          udata = this.$refs.docform.getFieldValue('iamLicenseUnitEntitys')
        }
        // 指定公司
        let cdata = []
        if (this.$refs.docform.getFieldValue('iamAppointUnitEntitys') !== undefined) {
          cdata = cdata.concat(this.$refs.docform.getFieldValue('iamAppointUnitEntitys'))
        }
        // 人员
        let pdata = []
        if (this.$refs.docform.getFieldValue('iamLicensePeopleEntitys') !== undefined) {
          pdata = pdata.concat(this.$refs.docform.getFieldValue('iamLicensePeopleEntitys'))
        }
        // 角色
        let rdata = []
        if (this.$refs.docform.getFieldValue('iamLicenseRoleEntitys') !== undefined) {
          rdata = rdata.concat(this.$refs.docform.getFieldValue('iamLicenseRoleEntitys'))
        }
        // 项目
        let pjdata = []
        if (this.$refs.docform.getFieldValue('iamLicenseProjectEntitys') !== undefined) {
          pjdata = pjdata.concat(this.$refs.docform.getFieldValue('iamLicenseProjectEntitys'))
        }
        if (this.maintaindata.length > 0) {
          const empowerType = this.$refs.docform.getFieldValue('empowerType')
          const creatorName = this.$refs.docform.getFieldValue('creatorId')
          const createDeptName = this.$refs.docform.getFieldValue('createDeptName')

          let sl = null
          let el = null
          if (empowerType === '0') {
            if (this.$refs.docform.getFieldValue('startDate') !== undefined) {
              const s = new Date(this.$refs.docform.getFieldValue('startDate'))
              const e = new Date(this.$refs.docform.getFieldValue('endDate'))
              sl = s.getTime()
              el = e.getTime()
              // 如果开始时间大于结束时间则提示开始时间不能大于结束时间
              if (sl > el) {
                message.warning({
                  title: '提示',
                  content: '开始时间不能大于结束时间!',
                })
                return
              }
            }
          }
          let idListKeys = []

          idListKeys = this.maintaindata.map((item) => item?.id) || []
          const { modelName, modelCode } = this.maintaindata[0]
          this.$refs.docform.setFieldValue('modelName', modelName)
          this.$refs.docform.setFieldValue('modelCode', modelCode)

          let maintainId = ''
          if (idListKeys.length > 0) {
            maintainId = idListKeys.join(',')
          }
          udata.forEach((item) => {
            item.docType = this.docType
            item.maintainId = maintainId
            item.empowerType = empowerType
            item.startDate = sl
            item.endDate = el
          })
          this.$refs.docform.setFieldValue('iamLicenseUnitEntitys', udata)
          // 处理指定公司数据
          cdata.forEach((item) => {
            item.docType = this.docType
            item.maintainId = maintainId
            item.empowerType = empowerType
            item.startDate = sl
            item.endDate = el
          })
          this.$refs.docform.setFieldValue('iamAppointUnitEntitys', cdata)
          // 处理角色数据
          rdata.forEach((item) => {
            item.docType = this.docType
            item.maintainId = maintainId
            item.empowerType = empowerType
            item.startDate = sl
            item.endDate = el
          })
          this.$refs.docform.setFieldValue('iamLicenseRoleEntitys', rdata)
          // 处理项目数据
          pjdata.forEach((item) => {
            item.docType = this.docType
            item.maintainId = maintainId
            item.empowerType = empowerType
            item.startDate = sl
            item.endDate = el
          })
          this.$refs.docform.setFieldValue('iamLicenseProjectEntitys', pjdata)
          // 处理人员数据
          pdata.forEach((item) => {
            item.docType = this.docType
            item.maintainId = maintainId
            item.empowerType = empowerType
            item.startDate = sl
            item.endDate = el
          })
          this.$refs.docform.setFieldValue('iamLicensePeopleEntitys', pdata)
        }
        resolve(true)
      })
      return save
    },
    validForm() {
      if (this.maintaindata.length === 0) {
        Modal.warning({
          title: '提示',
          content: '请选择需要授权的模型!',
        })
        return Promise.resolve(false)
      } else {
        const udata = this.$refs.docform.getFieldValue('iamLicenseUnitEntitys')
        const pdata = this.$refs.docform.getFieldValue('iamLicensePeopleEntitys')
        const pjdata = this.$refs.docform.getFieldValue('iamLicenseProjectEntitys')
        const rdata = this.$refs.docform.getFieldValue('iamLicenseRoleEntitys')
        if (udata.length === 0 && pdata.length === 0 && pjdata.length === 0 && rdata.length === 0) {
          Modal.warning({
            title: '提示',
            content: '请至少选择一个授权项!',
          })
          return Promise.resolve(false)
        }
        this.$refs.docform.setFieldValue(
          'title',
          this.$refs.docform.getFieldValue('creatorName') + '模型授权申请'
        )
        return Promise.resolve(true)
        // }
      }
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'save' || button.fakeId === 'workflow-push') {
        // 保存前先校验计划是否已经存在
        evt.waitUntil(
          new Promise((resolve, reject) => {
            this.saveForm().then((res) => {
              if (!res) {
                evt.preventDefault()
              }
              resolve()
            })
          })
        )
      }
    },
    removemx(keys) {
      // 用的序号作key，从大往小删，防止串了
      const removeKeys = keys.sort((a, b) => b - a)
      removeKeys.forEach((key) => {
        this.maintaindata.splice(key, 1)
      })
      this.selectedRowKeys = []
    },
    getmaintaindata(model, fields) {
      if (this.modelFlag) {
        this.maintaineditable = !fields.iamLicenseUnitEntitys.readonly
        const id = model.id
        if (id) {
          auditMaintainService.getmaintainsqdata(id).then((data) => {
            this.maintaindata = data
            this.getmodelsqdata(model)
          })
        }
        this.modelFlag = false
      }
      return true
    },
    getClassdata(model, fields) {
      if (this.flag) {
        this.classtable = !fields.iamLicenseUnitEntitys.readonly
        const id = model.id
        if (id) {
          auditMaintainService.getclassdata(id).then((data) => {
            if (data[0]) {
              this.classdata = data
            }
            this.getmodelsqdata(model)
          })
        }
        this.flag = false
      }
      return true
    },
    showSelection() {
      return true
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 单位
    change(values) {
      let iamAuditDwMxs = []
      if (this.$refs.docform.getFieldValue('iamLicenseUnitEntitys')) {
        iamAuditDwMxs = this.$refs.docform.getFieldValue('iamLicenseUnitEntitys')
      }
      if (values.length < iamAuditDwMxs.length) {
        iamAuditDwMxs = []
      }
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.unitCode === 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.$refs.docform.setFieldValue('iamLicenseUnitEntitys', iamAuditDwMxs)
      this.saveForm()
    },
    // 公司
    changecompany(values) {
      let iamAuditDwMxs = []
      // 公司
      if (this.$refs.docform.getFieldValue('iamAppointUnitEntitys')) {
        iamAuditDwMxs = this.$refs.docform.getFieldValue('iamAppointUnitEntitys')
      }
      if (values.length < iamAuditDwMxs.length) {
        iamAuditDwMxs = []
      }
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.unitCode === element.code) === -1) {
          const mmc = {}
          mmc.unitName = element.name
          mmc.unitCode = element.code + ''
          mmc.unitId = element.code
          iamAuditDwMxs.push(mmc)
        }
      })
      this.$refs.docform.setFieldValue('iamAppointUnitEntitys', iamAuditDwMxs)
      this.saveForm()
    },
    // 用户
    // iamLicensePeopleEntitys
    changePeople(values) {
      let iamAuditDwMxs = []
      if (this.$refs.docform.getFieldValue('iamLicensePeopleEntitys')) {
        iamAuditDwMxs = this.$refs.docform.getFieldValue('iamLicensePeopleEntitys')
      }
      // 如果values长度小于iamAuditDwMxs长度，说明有删除
      if (values.length < iamAuditDwMxs.length) {
        iamAuditDwMxs = []
      }
      values.forEach((element) => {
        if (iamAuditDwMxs.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
            iamAuditDwMxs.push(mmc)
            this.$refs.docform.setFieldValue('iamLicensePeopleEntitys', iamAuditDwMxs)
          })
        }
      })
      this.saveForm()
    },
    // 角色
    changeRole(values) {
      let iamAuditDwMxs = []
      if (this.$refs.docform.getFieldValue('iamLicenseRoleEntitys')) {
        iamAuditDwMxs = this.$refs.docform.getFieldValue('iamLicenseRoleEntitys')
      }
      if (values.length < iamAuditDwMxs.length) {
        iamAuditDwMxs = []
      }
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.roleId === element.id) === -1) {
          const mmc = {}
          mmc.roleName = element.name
          mmc.roleId = element.id
          mmc.code = element.code
          iamAuditDwMxs.push(mmc)
        }
      })
      this.$refs.docform.setFieldValue('iamLicenseRoleEntitys', iamAuditDwMxs)
      this.saveForm()
    },
    // 项目
    changeProject(values) {
      let iamAuditDwMxs = []
      // iamAuditDwMxs = this.projectData
      if (this.$refs.docform.getFieldValue('iamLicenseProjectEntitys')) {
        iamAuditDwMxs = this.$refs.docform.getFieldValue('iamLicenseProjectEntitys')
      }
      if (values.length < iamAuditDwMxs.length) {
        iamAuditDwMxs = []
      }
      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.$refs.docform.setFieldValue('iamLicenseProjectEntitys', iamAuditDwMxs)
      this.saveForm()
    },

    // 选择模型列表
    mxRelSelect() {
      // 选择模型列表
      this.visibleY = true
    },
    flRelSelect() {
      this.flTableShow = true
    },
    peopleRelSelect() {
      setTimeout(() => {
        this.$refs.peopleRelSelect.openPicker()
      }, 0)
    },
    projectRelSelect() {
      this.projectShow = true
    },
    roleRelSelect() {
      this.roleShow = true
    },
    companylSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.cRelSelect.openPicker()
      }, 0)
    },
    // 选择申请单位
    dwRelSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.dwRelSelect.openPicker()
      }, 0)
    },

    listMxSelected(keys, keyinfos) {
      const iamAuditMxs = []
      // iamAuditMxs = this.maintaindata

      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.modelName = element.modelName
        mmc.modelCode = element.modelCode
        mmc.modelDesc = element.modelDesc
        mmc.rick = element.rick
        mmc.id = element.id
        iamAuditMxs.push(mmc)
      })
      this.maintaindata = iamAuditMxs
      this.saveForm()

      // 处理人员数据-仅增加新增的
      // pdata.forEach((item) => {
      //   item.docType = 'apply'
      //   item.maintainId = maintainId
      // })
    },
    classListMxSelected(keys, keyinfos) {
      const iamAuditMxs = []
      keyinfos.map((item) => {
        // 判断当前模型ID是否存在存在则不添加
        const m = iamAuditMxs.find((p) => p.id === item.id)
        if (!m) {
          const mmc = {}
          mmc.id = item.id
          mmc.categoryName = item.categoryName
          mmc.categoryId = item.categoryId
          iamAuditMxs.push(mmc)
        }
      })
      this.classdata = iamAuditMxs
      this.saveForm()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';

.btnselect {
  position: relative;
  top: 2px;
  right: 80px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
.btnselectmaintain {
  position: relative;
  float: right;
  .batchselect {
    z-index: 100;
    margin-right: -10px;
    margin-left: 10px;
  }
}
.maintaintable {
  :global(.ant-table-thead > tr > th) {
    border-right: none;
  }
  :global(.ant-table-bordered.ant-table-empty .ant-table-placeholder) {
    border: none;
  }
  :global(.ant-table-body table) {
    //  border-left:none;
    border: none;
  }
}
</style>
