<template>
  <span>
    <audit-form-top-banner :handel-save-form="saveForm" @handelSaveForm="saveForm">
      <div class="title_sd-detail-form_common">数据分类授权</div>

      <table>
        <tr>
          <td>
            <audit-advanced-group
              :expand="classExpand"
              :expand-str="'classExpand'"
              :group-label="'分类列表'"
              tablestyle="''"
              @changedClick="changedClick"
            >
            </audit-advanced-group>
            <!-- <SdTable
              v-if="classExpand"
              ref="classtable"
              :loading="false"
              :columns="columns"
              :data-source="classdata"
              :pagination="false"
              :row-key="(record, index) => index"
            /> -->
            <template>
              <div :class="$style.wrapper">
                <div :class="$style.btnselect">
                  <a-button type="link" :class="$style.batchselect" @click="flRelSelect">
                    <a-icon type="check-circle" :theme="'filled'" />
                    添加
                  </a-button>
                </div>
              </div>
            </template>
            <template>
              <XmChildTable
                ref="classtable"
                v-model="classdata"
                :label="null"
                :columns="columns"
                :fields="classifyFileds"
                :addbuttonvisiable="false"
                @deleted="deletedClassList"
              >
                <template v-slot:categoryName="{ field, text, index, value }">
                  {{ text }}
                </template>
                <template v-slot:categoryId="{ field, text, index, value }">
                  {{ text }}
                </template>
                <template v-slot:categoryDesc="{ field, text, index, value }">
                  {{ text }}
                </template>
              </XmChildTable>
            </template>
          </td>
        </tr>
      </table>

      <table>
        <tr>
          <td>
            <audit-advanced-group
              :expand="selectExpand"
              :expand-str="'selectExpand'"
              :group-label="'数据表'"
              tablestyle="''"
              @changedClick="changedClick"
            >
            </audit-advanced-group>
            <!-- <SdTable
              v-if="selectExpand"
              :loading="false"
              :columns="selectColumns"
              :data-source="selectData"
              :pagination="false"
              :row-key="(record, index) => index"
              :row-selection="{
                selectedRowKeys: selectedRowKeys,
                onChange: onSelectChange,
              }"
            /> -->
            <template>
              <div :class="$style.wrapper">
                <div :class="$style.btnselect">
                  <a-button type="link" :class="$style.batchselect" @click="showSelectTable = true">
                    <a-icon type="check-circle" :theme="'filled'" />
                    添加
                  </a-button>
                </div>
              </div>
            </template>
            <template>
              <XmChildTable
                ref="selecttable"
                v-model="selectData"
                :label="null"
                :columns="selectColumns"
                :fields="selectColumnsFileds"
                :addbuttonvisiable="false"
              >
                <template v-slot:tableNameCN="{ field, text, index, value }">
                  {{ text }}
                </template>
                <template v-slot:tableNameEN="{ field, text, index, value }">
                  {{ text }}
                </template>
                <template v-slot:classifyName="{ field, text, index, value }">
                  {{ text }}
                </template>
              </XmChildTable>
            </template>
          </td>
        </tr>
      </table>

      <table>
        <tr v-if="unit.length > 1">
          <td>
            <audit-advanced-group
              :expand="companyExpand"
              :expand-str="'companyExpand'"
              :group-label="'指定公司'"
              tablestyle="''"
              @changedClick="changedClick"
              ><template>
                <template>
                  <div :class="$style.wrapper">
                    <div v-show="companyExpand" :class="$style.btnselect">
                      <a-button type="link" :class="$style.batchselect" @click="companylSelect">
                        <a-icon type="check-circle" :theme="'filled'" />
                        添加
                      </a-button>
                    </div>
                  </div>
                </template>
                <XmChildTable
                  ref="cdata"
                  v-model="companyData"
                  :label="null"
                  :columns="companycolumns"
                  :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="'cdata_empowerType_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'cdata'"
                      lx="type"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:startDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'cdata_startDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'cdata'"
                      lx="stime"
                      @change="setchange"
                    />
                  </template>
                  <template v-slot:endDate="{ field, text, index, value }">
                    <audit-warrant-input
                      :ref="'cdata_endDate_' + index"
                      :value="value[index]"
                      :index="index"
                      :field="field"
                      :data="'cdata'"
                      lx="etime"
                      @change="setchange"
                    />
                  </template>
                </XmChildTable>
              </template>
            </audit-advanced-group>
          </td>
        </tr>
      </table>

      <!-- <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:userId="{ 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-p
                      :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-p
                      :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-p
                      :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">
        <SdGroupPicker
          ref="dwRelSelect"
          v-model="mainSendUnit"
          :root-node="rootNodeData"
          :single="false"
          :read-only="false"
          @change="changeunit"
        />
      </div>

      <div v-show="false">
        <SdGroupPicker
          ref="cRelSelect"
          v-model="companyUnit"
          :root-node="rootNodeData"
          :single="false"
          :read-only="false"
          @change="changecompany"
        />
      </div>

      <div v-show="false">
        <SdUserPicker
          ref="pRelSelect"
          v-model="mainSendPersonal"
          :root-node="rootNodeData"
          :single="false"
          :read-only="false"
          @change="changep"
        />
      </div>
    </audit-form-top-banner>
    <roleModal v-model="roleShow" @selectRoleList="changeRole" />
    <projectModal v-model="projectShow" @selectProject="changeProject" />
    <DataClassPicker :visible="flTableShow" @listMxSelected="indexlistMxSelected"></DataClassPicker>
    <DataTablePicker
      :visible="showSelectTable"
      :class-data="classdata"
      @listMxSelected="selectMxSelected"
    ></DataTablePicker>
  </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 '../dataUphold/audit-warrant-input.vue'
import auditWarrantInputP from './audit-warrant-input.vue'
import dataMarketService from './data-market'
import SdGroupPicker from '@/common/components/sd-group-picker.vue'
// import SdTable from '@/common/components/sd-table.vue'
import SdUserPicker from '@/common/components/sd-user-picker.vue'
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'
import DataClassPicker from './data-class-picker.vue'
import DataTablePicker from './data-table-picker.vue'
import DataService from '../dataUphold/data-config'
import { getUserInfo } from '@/common/store-mixin'

export default {
  name: 'DataRuleApply',
  metaInfo: {
    title: '数据分类授权',
  },
  components: {
    SdGroupPicker,
    // SdTable,
    SdUserPicker,
    XmChildTable,
    auditFormTopBanner,
    auditAdvancedGroup,
    auditWarrantInput,
    roleModal,
    projectModal,
    DataClassPicker,
    DataTablePicker,
    auditWarrantInputP,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      mainSendUnit: [],
      companyUnit: [],
      mainSendPersonal: [],
      mainSendRole: [],
      selectedRowKeys: [],
      classdata: [],
      selectExpand: true,
      selectData: [],
      unitdata: [],
      personaldata: [],
      classExpand: true,
      unitExpand: true,
      personalExpand: true,
      roleExpand: true,
      projectExpand: true,
      personal: [],
      unit: [],
      indexTableShow: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          key: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '分类名称',
          dataIndex: 'categoryName',
          key: 'categoryName',
        },
        {
          title: '分类编号',
          dataIndex: 'categoryId',
          key: 'categoryId',
        },
        {
          title: '分类描述',
          dataIndex: 'categoryDesc',
          key: 'categoryDesc',
        },
      ],
      // 分类字段 分类名称 编号 描述
      classifyFileds: [
        {
          caption: '序号',
          name: 'sortNum',
          dataType: 'sortNum',
        },
        {
          // 分类名称
          caption: '分类名称',
          name: 'categoryName',
          dataType: 'string',
        },
        {
          // 分类编号
          caption: '分类编号',
          name: 'categoryId',
          dataType: 'string',
        },
        {
          // 分类描述
          caption: '分类描述',
          name: 'categoryDesc',
          dataType: 'string',
        },
      ],
      showSelectTable: false,
      selectColumnsFileds: [
        {
          caption: '序号',
          name: 'sortNum',
          dataType: 'sortNum',
        },
        {
          // 表中文名称
          caption: '表中文名称',
          name: 'tableNameCN',
          dataType: 'string',
        },
        {
          // 表英文名称
          caption: '表英文名称',
          name: 'tableNameEN',
          dataType: 'string',
        },
        {
          // 所属分类
          caption: '所属分类',
          name: 'classifyName',
          dataType: 'string',
        },
      ],

      selectColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          key: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '表中文名称',
          dataIndex: 'tableNameCN',
        },
        {
          title: '表英文名称',
          dataIndex: 'tableNameEN',
        },
        {
          title: '所属分类',
          dataIndex: 'classifyName',
        },
      ],
      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: 'userId',
          sdHidden: false,
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
          sdHidden: false,
        },
        {
          title: '开始日期',
          dataIndex: 'startDate',
          sdHidden: false,
        },
        {
          title: '结束日期',
          dataIndex: 'endDate',
          sdHidden: false,
        },
        {
          dataIndex: 'id',
          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,
        },
      ],
      classIndexFileds: [
        {
          caption: '序号',
          name: 'sortNum',
          dataType: 'sortNum',
        },
        {
          caption: '业务名称',
          name: 'indexName',
          dataType: 'string',
        },
        {
          caption: '业务编号',
          name: 'id',
          dataType: 'string',
        },
        {
          caption: '系统来源名称',
          name: 'indexRemark',
          dataType: 'string',
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      classIndexData: [],
      classShow: true,
      flTableShow: false,
      indexColumns: [
        // 序号 ,业务名称 业务编号
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '业务名称',
          dataIndex: 'indexName',
        },
        {
          title: '业务编号',
          dataIndex: 'id',
        },
        {
          title: '系统来源名称',
          dataIndex: 'indexRemark',
        },
        {
          dataIndex: 'indexId',
          sdHidden: true,
        },
      ],
      rootNodeData: null,
    }
  },
  created() {
    if (this.$route.query.classid) {
      const classid = this.$route.query.classid
      this.getClassList(classid)
    }
    if (this.$route.query.selectid) {
      const selectid = this.$route.query.selectid
      this.getOutside(selectid)
    }

    // 获取人员和单位 field
    this.getfields()
  },
  mounted() {
    const userInfo = getUserInfo()
    DataService.getCompany(userInfo.account).then((res) => {
      this.rootNodeData = {
        code: res.data.id,
        name: res.data.name,
      }
      const roles = this.$store.state.sd.common.userInfo.default.roles
      const isExport = roles.map((val) => val.code).includes('G-1_DATA_AUDIT_RULE')
      if (isExport) {
        this.rootNodeData = {
          code: '28951',
          name: '国家电力投资集团有限公司',
        }
      }
    })
  },
  methods: {
    setchange(value, field, index, lx, data) {
      this.$refs[data].$refs[field.name + '_' + index][0].setFieldValue(field.name, value)
      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 + '_startDate_' + 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')
        }
        // { label: '近一个月', value: '3' },
        // { label: '近三个月', value: '4' },
        // { label: '近六个月', value: '5' },
        if (value === '3') {
          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'] },
          ]
          this.$refs[data + '_startDate_' + index].model.startDate = moment()
          // 加一个月
          this.$refs[data + '_endDate_' + index].model.endDate = moment().add(1, 'months')
        }
        if (value === '4') {
          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'] },
          ]
          this.$refs[data + '_startDate_' + index].model.startDate = moment()
          this.$refs[data + '_endDate_' + index].model.endDate = moment().add(3, 'months')
        }
        if (value === '5') {
          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'] },
          ]
          this.$refs[data + '_startDate_' + index].model.startDate = moment()
          this.$refs[data + '_endDate_' + index].model.endDate = moment().add(6, 'months')
        }
      } 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()
      }
    },
    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.code
          mmc.unitId = element.id
          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() {
      // if (this.selectedRowKeys.length === 0) {
      //   Message.error('请选择数据项')
      //   return false
      // }
      if (this.classdata.length === 0) {
        Message.error('请至少选择一个分类')
        return
      }

      // const tableArr = this.selectData.filter((item, index) => {
      //   return this.selectedRowKeys.includes(index)
      // })
      const tableArr = this.selectData
      const pdata = []
      const rdata = []
      const prodata = []
      const cdata = []

      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()
        }
        // 3 4 5
        if (item.empowerType === '3') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().add(1, 'months')).getTime()
        }
        if (item.empowerType === '4') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().add(3, 'months')).getTime()
        }
        if (item.empowerType === '5') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().add(6, 'months')).getTime()
        }
        pdata.push({
          docType: 'date',
          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: 'date',
          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: 'date',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          projectId: item.projectId,
          projectName: item.projectName,
        })
      })
      this.companyData.forEach((item) => {
        let sl = null
        let el = null
        if (item.empowerType === '2') {
          sl = new Date(moment()).getTime()
          el = new Date(moment().endOf('year')).getTime()
        }
        cdata.push({
          docType: 'date',
          empowerType: item.empowerType,
          startDate: sl,
          endDate: el,
          unitCode: item.unitCode,
          unitName: item.unitName,
          unitId: item.unitId,
        })
      })
      if (pdata.length === 0 && prodata.length === 0 && rdata.length === 0 && cdata.length === 0) {
        Modal.warning({
          title: '提示',
          content: '请至少选择一个授权项!',
        })
        return false
      }
      this.$refs.pdata.valid().then((res) => {
        if (res) {
          this.$refs.rdata.valid().then((res) => {
            if (res) {
              this.$refs.prodata.valid().then((res) => {
                if (res) {
                  const params = {
                    iamDataCubeEntities: this.classdata, // 分类集合
                    iamServerInfoEntities: tableArr, // 表集合
                    iamLicenseUnitEntities: [], // 授权单位
                    iamLicensePeopleEntities: pdata, // 授权人员
                    iamLicenseProjectEntities: prodata, // 授权项目
                    iamLicenseRoleEntities: rdata, // 授权角色
                    iamAppointUnitEntities: cdata, // 指定单位
                  }
                  dataMarketService.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
    },
    deletedClassList() {
      // 清空数据表
      const ids = this.classdata.map((item) => {
        return parseInt(item.id)
      })
      this.selectData = this.selectData.filter((item) => {
        return ids.includes(item.classifyId)
      })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 获取table字段信息
    getfields() {
      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/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/iamLicenseProject',
        method: 'get',
      }).then((res) => {
        this.projectTable = res.data.pageFormData.pageFieldInfos
        this.projectTable.find((item) => item.name === 'empowerType').attr.mandatory = 'false'
      })
    },
    // 获取分类列表
    getClassList(ids) {
      const arrid = ids.split(',')
      arrid.forEach((id) => {
        dataMarketService.findclassList(id).then((data) => {
          this.classdata.push(data)
        })
      })
      // <!-- 数据表 -->
      // arrid.forEach((id) => {
      //   dataMarketService.findOutsideAll(id).then((data) => {
      //     if (data.length > 0) {
      //       this.selectData.push(...data)
      //     }
      //   })
      // })
    },
    // 获取表列表
    getOutside(ids) {
      dataMarketService.findoutside(ids).then((data) => {
        this.selectData = data
        // this.selectData.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
    },
    changecompany(values) {
      let iamAuditDwMxs = []
      iamAuditDwMxs = this.companyData
      values.forEach((element) => {
        if (iamAuditDwMxs.findIndex((dw) => dw.unitId === element.code) === -1) {
          const mmc = {}
          mmc.unitName = element.name
          mmc.unitCode = element.code
          mmc.unitId = element.id
          iamAuditDwMxs.push(mmc)
        }
      })
      this.companyData = iamAuditDwMxs
    },
    flRelSelect() {
      this.flTableShow = true
    },
    indexlistMxSelected(keys, keyinfos) {
      const ids = this.classdata.map((item) => {
        return parseInt(item.id)
      })

      keyinfos.map((item) => {
        if (!ids.includes(item.id)) {
          this.classdata.push({
            categoryName: item.categoryName,
            categoryId: item.categoryId,
            categoryDesc: item.categoryDesc,
            id: item.id,
          })
        }
      })
      // 清空数据表
      // this.selectData = []
    },
    // 选择数据表
    selectMxSelected(keys, keyinfos) {
      const ids = this.selectData.map((item) => {
        return parseInt(item.id)
      })
      keyinfos.map((item) => {
        if (!ids.includes(item.id)) {
          this.selectData.push({
            tableNameCN: item.tableNameCN,
            tableNameEN: item.tableNameEN,
            classifyName: item.classifyName,
            classifyId: item.classifyId,
            id: item.id,
          })
        }
      })
    },
  },
}
</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>
