<template>
  <div :class="[$style.wrapHeight, $style.dataclass]">
    <div :class="$style.rowHeight">
      <div :class="$style.rightcard">
        <a-card>
          <!-- 高级搜索组件 -->
          <audit-advanced-query
            :expand="expand"
            :search-data="formData"
            :ref-name="searchform"
            :search-style="{ height: '150px', left: '20px', top: '57px' }"
            :search-fun="handleSearch"
            @searchedClick="searchedClick"
          >
            <template>
              <a-col :span="8">
                <a-form-model-item :label="'人员名称'">
                  <a-input v-model="formData.licensePeople" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item :label="'人员公司名称'">
                  <a-input v-model="formData.licensePeopleUnit" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item :label="'人员工号'">
                  <a-input v-model="formData.licensePeopleNumber" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <SdDataTableEx
            :key="key"
            ref="dataTable"
            form-id="iamLicenseRecord"
            page-id="audit/maintain/iamLicenseRecord"
            :columns="columns"
            :filter-expressions="expressions"
            :show-advance-query="true"
            :search-fields="['licensePeopleUnit', 'licensePeopleNumber', 'licensePeople']"
            @searchbtnClick="searchbtnClick"
          >
            <template slot="maintainName" slot-scope="text, record">
              <a @click="goDetail(record.id)">{{ text }}</a>
            </template>

            <template slot="indexName" slot-scope="text, record">
              <a @click="goDetail(record.id)">{{ text }}</a>
            </template>
            <template slot="cancel" slot-scope="text, record">
              <a-popconfirm
                title="确定要撤销吗？"
                ok-text="是"
                cancel-text="否"
                @confirm="revokeChange(record)"
              >
                <a-switch
                  :checked="shareStatus(record.cancel)"
                  checked-children="是"
                  un-checked-children="否"
                ></a-switch>
              </a-popconfirm>
            </template>
            <!-- 详情表单 -->
          </SdDataTableEx>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import SdDataTableEx from '@/common/components/sd-data-table-ex.vue'
import TableColumnTypes from '@/common/services/table-column-types'

export default {
  name: 'DataRecord',
  metaInfo: {
    title: '数据授权记录',
  },
  components: {
    SdDataTableEx,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      editnode: true,
      key: 0,
      searchform: 'searchform',
      formData: {
        licensePeople: '',
        licensePeopleUnit: '',
        licensePeopleNumber: '',
      },
      auditOrgId: null,
      parentId: 0,
      level: 1,
      isEnd: null,
      columns: [
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          title: '序号',
          dataIndex: 'sortNum',
          key: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '分类名称',
          dataIndex: 'maintainName',
          scopedSlots: { customRender: 'maintainName' },
        },
        {
          title: '表名称',
          dataIndex: 'indexName',
          scopedSlots: { customRender: 'indexName' },
        },
        {
          title: '指定公司',
          dataIndex: 'appointUnit',
        },
        {
          title: '授权单位',
          dataIndex: 'licenseUnit',
        },
        {
          title: '授权人员',
          dataIndex: 'licensePeople',
        },
        {
          title: '人员公司名称',
          dataIndex: 'licensePeopleUnit',
        },
        {
          title: '人员工号',
          dataIndex: 'licensePeopleNumber',
        },
        {
          title: '授权项目',
          dataIndex: 'licenseProject',
        },
        {
          title: '授权角色',
          dataIndex: 'licenseRole',
        },
        // 创建时间
        {
          title: '创建时间',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '状态',
          dataIndex: 'status',
          sdHidden: true,
        },
        {
          title: '撤销',
          dataIndex: 'cancel',
          scopedSlots: { customRender: 'cancel' },
        },
      ],
      expressions: [
        {
          dataType: 'str',
          name: 'docType',
          op: 'eq',
          stringValue: 'datacube',
        },
      ],
      isroot: true,
      className: '',
    }
  },
  methods: {
    handleSearch() {
      this.expressions = []
      // 人员名称 licensePeople
      if (this.formData.licensePeople) {
        this.expressions.push({
          dataType: 'str',
          name: 'licensePeople',
          op: 'like',
          stringValue: `%${this.formData.licensePeople}%`,
        })
      }
      // 人员公司名称
      if (this.formData.licensePeopleUnit) {
        this.expressions.push({
          dataType: 'str',
          name: 'licensePeopleUnit',
          op: 'like',
          stringValue: `%${this.formData.licensePeopleUnit}%`,
        })
      }
      // 人员工号
      if (this.formData.licensePeopleNumber) {
        this.expressions.push({
          dataType: 'str',
          name: 'licensePeopleNumber',
          op: 'like',
          stringValue: `%${this.formData.licensePeopleNumber}%`,
        })
      }

      this.expressions.push({
        dataType: 'str',
        name: 'docType',
        op: 'eq',
        stringValue: 'datacube',
      })
    },
    goDetail(id) {
      const url = '#/data-record-info?selectid=' + id // 新页面要打开的路由地址
      // 新页面要打开的路由地址
      window.open(url, '_blank')
    },
    refresh() {
      this.$refs.dataTable.refresh()
    },
    shareStatus(record) {
      return record === 1
    },
    revokeChange(record) {
      const cancel = record.cancel === 1 ? 0 : 1
      const status = record.cancel === 1 ? 1 : 0
      axios({
        url: 'api/xcoa-mobile/v1/iamLicenseRecord/undo-restore',
        method: 'get',
        params: {
          id: record.id,
          type: cancel,
        },
      }).then((res) => {
        this.refresh()
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;

  .row-height {
    display: flex;
    flex: auto;
    height: 100%;

    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
    }
  }
}

.dataclass {
  :global(.projectlist .ant-table-empty .ant-table-body) {
    overflow-x: hidden !important;
  }

  :global(span > .ant-btn:nth-child(2)) {
    color: #fff;
    background-color: #1890ff;
    border-color: #1890ff;
  }

  // :global(.ant-table-placeholder) {
  //   width: auto;
  // }
}
</style>
