<template>
  <div class="auditsqapplylist">
    <a-card>
      <a-radio-group v-model="radioValue" allow-clear @change="radioOnChange">
        <a-radio :value="1">
          全部
        </a-radio>
        <a-radio :value="2">
          待处理
        </a-radio>
      </a-radio-group>
      <!-- 高级搜索组件 -->
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{ height: '150px', left: '20px', top: '80px' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'申请人'" prop="creatorName">
              <a-input v-model="formData.creatorName" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'申请单位'" prop="unitName">
              <a-input v-model="formData.unitName" allow-clear />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>

      <sd-data-table-ex
        ref="sqapplyDataTable"
        :filter-expressions="expressions"
        :columns="columns"
        :actions="actions"
        form-id="iamAuditSqapply"
        data-url="api/xcoa-mobile/v1/iamauditsqapply/iamModelSqapplyList"
        :search-fields="['creatorName']"
        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>
    <audit-advanced-export
      ref="export"
      :v-if="false"
      :exclebxh="false"
      table-id="iamAuditSqapply"
      :expressions="exportExpressions"
      :config-id="161"
    />
  </div>
</template>

<script>
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedExport from '../../components/audit-advanced-export.vue'
import components from './_import-components/audit-sqapply-list-import'

export default {
  name: 'AuditSqapplyList',
  metaInfo: {
    title: '授权申请',
  },
  components: {
    ...components,
    auditAdvancedQuery,
    auditAdvancedExport,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      searchform: 'searchform',
      radioValue: 1,
      expressions: [],
      exportExpressions: [],
      formData: {
        creatorName: '',
        unitName: '',
      },
      formId: 'iamAuditSqapply',
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '申请人',
          dataIndex: 'creatorName',
        },
        {
          title: '申请单位',
          dataIndex: 'unitName',
        },
        {
          title: '模型名称',
          dataIndex: 'modelName',
          scopedSlots: { customRender: 'isLink' },
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
        },
        {
          title: '授权开始日期',
          dataIndex: 'startDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '授权截止日期',
          dataIndex: 'endDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '申请日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
          defaultSortOrder: 'desc',
          sorter: true,
        },
        {
          title: '当前状态',
          dataIndex: 'flowState',
        },
        {
          title: '当前处理人',
          dataIndex: 'currentUser',
        },
      ],
      actions: [
        {
          label: '授权申请',
          id: 'sqapply',
          permission: null,
          type: TableActionTypes.primary,
          callback: () => {
            // PRODUCT_IAM_SQSQ 原来
            const url = '/sd-flow-guide?code=PRODUCT_IAM_MLSQSQ' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refreshDataTable()
              }
            })
          },
        },
        {
          label: '导出',
          id: 'export',
          permission: null,
          callback: () => {
            this.fnexport()
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理（对sd-data-table无效）
        },
      ],
    }
  },
  methods: {
    radioOnChange(e) {
      if (this.selectedType !== e.target.value) {
        this.expressions = []
        this.selectedType = e.target.value
        if (e.target.value === 1) {
          this.expressions.push({
            dataType: 'str',
            name: 'stateFlag',
            op: 'eq',
            stringValue: 'true',
          })
        } else if (e.target.value === 2) {
          this.expressions.push({
            dataType: 'str',
            name: 'stateFlag',
            op: 'eq',
            stringValue: 'false',
          })
        }
      }
      this.$refs.sqapplyDataTable.clearSelection()
    },
    fnexport: function() {
      this.exportExpressions = []
      this.exportExpressions = [
        { dataType: 'str', name: 'docType', op: 'eq', stringValue: 'model_apply' },
      ]
      setTimeout(() => {
        this.$refs.export.exportdata()
      }, 200)
    },
    refreshDataTable() {
      this.$refs.sqapplyDataTable.clearSelection()
      this.$refs.sqapplyDataTable.refresh()
    },
    handleSearch() {
      this.expressions = []
      if (this.radioValue === 1) {
        this.expressions.push({
          dataType: 'str',
          name: 'stateFlag',
          op: 'eq',
          stringValue: 'true',
        })
      } else if (this.radioValue === 2) {
        this.expressions.push({
          dataType: 'str',
          name: 'stateFlag',
          op: 'eq',
          stringValue: 'false',
        })
      }
      // 申请人
      if (this.formData.creatorName) {
        this.expressions.push({
          dataType: 'str',
          name: 'creatorName',
          op: 'like',
          stringValue: `%${this.formData.creatorName}%`,
        })
      }
      // 申请单位
      if (this.formData.unitName) {
        this.expressions.push({
          dataType: 'str',
          name: 'unitName',
          op: 'like',
          stringValue: `%${this.formData.unitName}%`,
        })
      }
    },
    rowClick(record) {
      // 判断权限
      if (
        (record.flowState === '起草') | (record.flowState === '开始') &&
        record.creatorAccount === getUserInfo().account
      ) {
        const url = '/sd-webflow/pages/draft/' + record.instId // 编辑面要打开的路由地址
        crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
          if (refreshFlag) {
            // 这里写或者调刷新的方法
            this.refreshDataTable()
          }
        })
      } else {
        window.open('#/sd-webflow/done-pages/' + record.instId)
      }
    },
  },
}
</script>

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