<template>
  <div :class="$style.wrapHeight">
    <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="12">
                <a-form-model-item :label="'模型编号'" prop="modelCode">
                  <a-input v-model="formData.maintainCode" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'模型名称'" prop="modelName">
                  <a-input v-model="formData.maintainName" 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"
            :search-fields="['maintainCode', 'maintainName']"
            :show-advance-query="true"
            :filter-expressions="expressions"
          >
            <template slot="maintainName" slot-scope="text, record">
              <a @click="goDetail(record.id)">{{ text }}</a>
            </template>
            <template slot="status" slot-scope="text">
              <span v-if="text === 1">启用</span>
              <span v-else>禁用</span>
            </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 auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import TableColumnTypes from '@/common/services/table-column-types'
import auditMaintainService from './audit-maintain-service'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import components from './_import-components/audit-record-list-import'
import SdDataTableEx from '@/common/components/sd-data-table-ex.vue'

export default {
  name: 'AuditRecordList',
  metaInfo: {
    title: '模型授权记录',
  },
  components: {
    ...components,
    SdDataTableEx,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],

  data() {
    return {
      key: 0,
      treeData: [],
      searchform: 'searchform',

      formData: {
        maintainName: '',
        maintainCode: '',
      },
      formId: 'iamModelMaintain',
      columns: [
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          title: '模型名称',
          dataIndex: 'maintainName',
          scopedSlots: { customRender: 'maintainName' },
        },
        {
          title: '模型编号',
          dataIndex: 'maintainCode',
        },
        {
          title: '授权单位',
          dataIndex: 'licenseUnit',
        },
        {
          title: '授权人员',
          dataIndex: 'licensePeople',
        },
        {
          title: '授权项目',
          dataIndex: 'licenseProject',
        },
        {
          title: '授权角色',
          dataIndex: 'licenseRole',
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '创建时间',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '撤销',
          dataIndex: 'cancel',
          scopedSlots: { customRender: 'cancel' },
        },
      ],

      actions: [],
      type: 'message',

      expressions: [
        {
          dataType: 'str',
          name: 'docType',
          op: 'eq',
          stringValue: 'model',
        },
      ],
      catalogId: null,
      catalogName: '',
      isroot: true,
    }
  },

  methods: {
    goDetail(id) {
      const url = '#/audit-warrant-readonly?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/framework/v1/page/wp/audit/maintain/iamLicenseRecord',
        method: 'get',
        params: {
          id: record.id,
        },
      }).then((res) => {
        const resJson = res.data
        const data = {
          eventId: 'save',
          inputs: resJson.pageFormData.pageFieldInfos.map((v) => {
            if (v.name === 'cancel') {
              return {
                name: v.name,
                value: cancel,
              }
            } else if (v.name === 'status') {
              return {
                name: v.name,
                value: status,
              }
            } else {
              return {
                name: v.name,
                value: v.value,
              }
            }
          }),
          pageFlowId: resJson.attrs.pageflowId,
          pagePath: resJson.attrs.pagePath,
        }
        axios({
          url: 'api/framework/v1/page/handleData',
          method: 'post',
          data,
        }).then(() => {
          this.refresh()
        })
      })
    },
    handleSearch() {
      this.expressions = []
      // // 模型编号
      if (this.formData.maintainCode) {
        this.expressions.push({
          dataType: 'str',
          name: 'maintainCode',
          op: 'like',
          stringValue: `%${this.formData.maintainCode}%`,
        })
      }
      // 模型名称
      if (this.formData.maintainName) {
        this.expressions.push({
          dataType: 'str',
          name: 'maintainName',
          op: 'like',
          stringValue: `%${this.formData.maintainName}%`,
        })
      }

      this.expressions.push({
        dataType: 'str',
        name: 'docType',
        op: 'eq',
        stringValue: 'model',
      })
    },
  },
}
</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%;
    }
  }
}
.no-action {
  color: $text-color-secondary;
}
</style>
