<template>
  <div>
    <a-card>
      <!-- 高级搜索组件 -->
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{ height: '170px', left: '20px', top: '57px' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
        @resetForm="resetForm"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'合同名称'" prop="contractName">
              <a-input v-model="formData.contractName" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'合同类型'" prop="contractType">
              <a-select
                v-model="formData.contractType"
                :allow-clear="true"
                :options="contractType"
              />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>

      <sd-data-table-ex
        ref="CubeDataTable"
        :filter-expressions="expressions"
        :columns="columns"
        :actions="actions"
        form-id="iamCubeContract"
        page-id="cube/contract/iamCubeContract"
        :search-fields="['contractName', 'contractType']"
        show-selection
        :show-advance-query="true"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a :title="text" @click="rowClick(record)">{{ text }}</a>
        </div>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import { sdLocalStorage } from '@/common/services/storage-service'
import { Modal } from '@/common/one-ui'
import cubeServices from '@product/iam/cube/cube-services'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
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 components from './_import-components/cube-contract-list-import'

export default {
  name: 'CubeContractList',
  metaInfo: {
    title: '合同信息录入',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      searchform: 'searchform',
      expressions: [],
      formData: {
        contractName: '',
        contractType: null,
      },
      contractType: [],
      // 高级搜索用
      expand: false,
      formId: 'iamCubeContract',
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '签订日期',
          dataIndex: 'signDate',
          sdRender: TableColumnTypes.date,
          defaultSortOrder: 'desc',
        },
        {
          title: '合同名称',
          dataIndex: 'contractName',
          scopedSlots: { customRender: 'islink' },
        },
        {
          title: '合同编号',
          dataIndex: 'contractCode',
        },
        {
          title: '合同金额',
          dataIndex: 'contractAmount',
        },
        {
          title: '合同类型',
          dataIndex: 'contractType',
        },
        {
          title: '合作方名称',
          dataIndex: 'partnerName',
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          permission: 'create',
          type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
          callback: () => {
            const url = '/cube-contract-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refresh()
              }
            })
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
        },
      ],
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.seniorSearch()
  },
  methods: {
    refresh() {
      return this.$refs.CubeDataTable.refresh(true)
    },
    rowClick(record) {
      const url = '/cube-contract-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 高级搜索
    seniorSearch() {
      // 如果是展示的，那么可能去获取字典值去
      if (!this.strOrArr(this.formData.contractType)) {
        // 获取合同类型的字典值 'contractType'
        if (!this.strOrArr(this.contractType)) {
          // axios({
          //   url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=IAM_CONTRACT_TYPE',
          //   method: 'get',
          // }).then((res) => {
          //   if (res.status === 200) {
          //     this.contractType = res.data
          //   }
          // })
          cubeServices.checkCubeToken('review').then((res) => {
            if (!res) {
              Modal.warning({
                title: '提示',
                content: '未开启CUBE集成功能参数或单点认证失败，请联系管理员',
              })
              return false
            }
            const cubeJson = JSON.parse(sdLocalStorage.getItem('cube') || '{}')
            axios({
              url:
                'api/xcoa-mobile/v1/iamcubecontract/findFileCode?accessToken=' +
                cubeJson.review_token,
            }).then((res) => {
              this.contractType = res.data.map((item) => {
                return {
                  value: item.code,
                  label: item.text,
                }
              })
            })
          })
        }
      }
    },
    // 判断数组或字符串为null或长度为0
    strOrArr(strOrArr) {
      return strOrArr !== null && strOrArr.length > 0
    },
    // 查询
    handleSearch() {
      this.expressions = []
      // 合同名称
      if (this.formData.contractName) {
        this.expressions.push({
          dataType: 'str',
          name: 'contractName',
          op: 'like',
          stringValue: `%${this.formData.contractName}%`,
        })
      }
      // 合同类型
      if (this.formData.contractType) {
        this.expressions.push({
          dataType: 'str',
          name: 'contractTypeCode',
          op: 'eq',
          stringValue: this.formData.contractType[0].id,
        })
      }
    },
    resetForm() {
      this.formData.contractType = null
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
