<template>
  <div style="float:left" :class="$style.mainDiv">
    <table>
      <tr>
        <td>
          <audit-advanced-group
            :expand="unitExpand"
            :expand-str="'unitExpand'"
            :group-label="'授权单位'"
            tablestyle="''"
            @changedClick="changedClick"
            ><template>
              <SdChildTable
                ref="udata"
                v-model="unitdata"
                :label="null"
                :columns="unitcolumns"
                :modal-props="{ height: '470px' }"
                :fields="
                  [
                    {
                      caption: '序号',
                      name: 'sortNum',
                      dataType: 'sortNum',
                    },
                  ].concat(unit)
                "
                :handle-before-add="dwRelSelect"
                :flagpage="true"
              >
                <template v-slot:form="{ model, fields }">
                  <table>
                    <tr>
                      <SdFormItemTd name="unitName">
                        <template v-slot:read-and-edit="{ editable }"
                          >{{ model.unitName }}
                        </template>
                      </SdFormItemTd>
                      <SdFormItemTd name="unitCode">
                        <template v-slot:read-and-edit="{ editable }"
                          >{{ model.unitCode }}
                        </template>
                      </SdFormItemTd>
                    </tr>
                    <tr>
                      <SdFormItemTd name="empowerType" :colspan="3">
                        <template v-slot:read-and-edit="{ editable }">
                          <a-select v-model="model.empowerType">
                            <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>
                      </SdFormItemTd>
                    </tr>
                    <tr v-if="model.empowerType !== '1'">
                      <SdFormItemTd name="startDate" />
                      <SdFormItemTd name="endDate" range-after="startDate" />
                    </tr>
                    <tr v-if="model.empowerType === '1'">
                      <td class="ant-form-item-label ant-form-item-label">
                        <label title="开始日期" class="ant-form-item"> 开始日期 </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="结束日期" class="ant-form-item"> 结束日期 </label>
                      </td>
                      <td class="ant-form-item-control-wrapper">
                        <a-date-picker v-model="model.endDate" :disabled="true" />
                      </td>
                    </tr>
                  </table>
                </template> </SdChildTable
            ></template>
          </audit-advanced-group> </td
      ></tr>
    </table>

    <table>
      <tr>
        <td
          ><audit-advanced-group
            :expand="personalExpand"
            :expand-str="'personalExpand'"
            :group-label="'授权人员'"
            tablestyle="''"
            @changedClick="changedClick"
            ><template>
              <SdChildTable
                ref="pdata"
                v-model="personaldata"
                :label="null"
                :columns="personalcolumns"
                :modal-props="{ height: '470px' }"
                :fields="
                  [
                    {
                      caption: '序号',
                      name: 'sortNum',
                      dataType: 'sortNum',
                    },
                  ].concat(personal)
                "
                :handle-before-add="pRelSelect"
                :flagpage="true"
              >
                <template v-slot:form="{ model, fields }">
                  <table>
                    <tr>
                      <SdFormItemTd name="userName">
                        <template v-slot:read-and-edit="{ editable }"
                          >{{ model.userName }}
                        </template>
                      </SdFormItemTd>
                      <SdFormItemTd name="userUnit">
                        <template v-slot:read-and-edit="{ editable }"
                          >{{ model.userUnit }}
                        </template>
                      </SdFormItemTd>
                    </tr>
                    <tr>
                      <SdFormItemTd name="empowerType" :colspan="3">
                        <template v-slot:read-and-edit="{ editable }">
                          <a-select v-model="model.empowerType">
                            <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>
                      </SdFormItemTd>
                    </tr>
                    <tr v-if="model.empowerType !== '1'">
                      <SdFormItemTd name="startDate" />
                      <SdFormItemTd name="endDate" />
                    </tr>
                    <tr v-if="model.empowerType === '1'">
                      <td class="ant-form-item-label ant-form-item-label">
                        <label title="开始日期" class="ant-form-item"> 开始日期 </label>
                      </td>
                      <td class="ant-form-item-control-wrapper">
                        <a-date-picker
                          v-model="model.startDate"
                          :disabled="model.empowerType === '1'"
                        />
                      </td>
                      <td class="ant-form-item-label ant-form-item-label">
                        <label title="结束日期" class="ant-form-item"> 结束日期 </label>
                      </td>
                      <td class="ant-form-item-control-wrapper">
                        <a-date-picker
                          v-model="model.endDate"
                          :disabled="model.empowerType === '1'"
                        />
                      </td>
                    </tr>
                  </table>
                </template> </SdChildTable></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> -->
  </div>
</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 SdChildTable from '@/common/components/sd-child-table.vue'
import SdFormItemTd from '@/common/components/sd-form-item-td.vue'
import SdGroupPicker from '@/common/components/sd-group-picker.vue'
import SdUserPicker from '@/common/components/sd-user-picker.vue'
// import auditMaintainService from './audit-maintain-service'
// import components from './_import-components/audit-datasq-form-import'

export default {
  name: 'AuditDataFromsq',
  metaInfo: {
    title: '数据授权',
  },
  components: {
    // ...components,
    auditAdvancedGroup,
    SdChildTable,
    SdFormItemTd,
  },
  mixins: [auditAdvancedGroupMixins],
  props: {
    // 默认选中节点
    // key: {
    //   type: Number,
    //   default: 0,
    // },
    recordId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      mainSendUnit: [],
      mainSendPersonal: [],
      selectedRowKeys: [],
      selectid: '',
      maintaindata: [],
      unitdata: [],
      personaldata: [],
      maintainExpand: true,
      unitExpand: true,
      personalExpand: true,
      personal: [],
      unit: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '模型名称',
          dataIndex: 'modelName',
        },
        {
          title: '模型编号',
          dataIndex: 'modelCode',
        },
        {
          title: '模型描述',
          dataIndex: 'modelDesc',
        },
      ],
      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,
        },
      ],
      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,
        },
      ],
    }
  },
  watch: {
    recordId() {
      // this.getmodelsqdata(this.recordId)
    },
  },
  created() {
    this.getfields()
  },
  methods: {
    panelEndYearChange(value) {},
    savetableh(index, field, data) {
      this.$refs[data].savetableh(index, field)
    },
    changeunit(values) {
      let iamAuditDwMxs = []
      iamAuditDwMxs = this.unitdata
      values.forEach((element) => {
        const index = iamAuditDwMxs.findIndex((i) => i.unitCode === element.code)
        if (index === -1) {
          const mmc = {}
          mmc.unitName = element.name
          mmc.unitCode = element.code
          mmc.unitId = element.code
          iamAuditDwMxs.push(mmc)
        }
      })
      this.unitdata = iamAuditDwMxs
    },
    changep(values) {
      let iamAuditPMxs = []
      iamAuditPMxs = this.personaldata
      // 过滤表格中已有的人员
      values.forEach((element) => {
        const index = iamAuditPMxs.findIndex((i) => i.userId === element.code)
        if (index === -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)
      return true
    },
    pRelSelect() {
      // 选择申请单位
      setTimeout(() => {
        this.$refs.pRelSelect.openPicker()
      }, 0)
      return true
    },

    saveForm() {
      const udata = []
      const pdata = []
      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()
          }
        }
        udata.push({
          docType: 'date',
          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()
          }
        }
        pdata.push({
          docType: 'date',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          userId: item.userId,
          userUnit: item.userUnit,
        })
      })

      const params = {
        iamLicenseUnitEntitys: udata,
        iamLicensePeopleEntitys: pdata,
      }
      // 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
      })
    },
  },
}
</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;
  }
}
.main-div {
  :global(.caption_sd-child-table_common) {
    top: -10px;
    z-index: 99;
  }
}
</style>
