<template>
  <div :class="$style.wrapHeight">
    <a-card>
      <!-- 高级搜索组件 -->
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{
          height: '170px',
        }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
        @resetForm="resetForm"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item label="准则版本编号">
              <a-input v-model="formData.criteriaCode"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="准则适用范围">
              <sd-group-picker v-model="formData.criteriaDeptCode" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'\u2003\u2003是否启用'">
              <sd-select v-model="formData.status" :options="statusList" allow-clear />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <sd-data-table-ex
        v-show="flag"
        :key="key"
        ref="riskCriteriaTable"
        form-id="riskCriteria"
        page-id="risk/criteria/riskCriteria"
        data-url="api/xcoa-mobile/v1/riskcriteria/all-list"
        :columns="columns"
        :actions="actions"
        :show-selection="actions.length > 0"
        :show-advance-query="true"
        :filter-expressions="expressions"
        :search-fields="['criteriaCode']"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="islink" slot-scope="text, record">
          <a @click="open(record)">{{ text }}</a>
        </template>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import systemManage from '@/system-manage/system-manage'
import TableActionTypes from '@/common/services/table-action-types'
import TableColumnTypes from '@/common/services/table-column-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQuery from '@product/iam/components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '@product/iam/components/audit-advanced-query-mixins'
import riskCriteriaService from './risk-criteria-service'
import components from './_import-components/risk-criteria-list-import'
export default {
  name: 'RiskCriteriaList',
  metaInfo: {
    title: '风险准则',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      statusList: [
        {
          id: 0,
          name: '启用',
        },
        {
          id: 1,
          name: '作废',
        },
      ],
      waterMark: systemManage.getFormWaterMark(),
      flag: true,
      key: 0,
      searchform: 'searchform',
      formData: {
        criteriaCode: '',
        criteriaDeptCode: [],
        status: [],
      },
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'id', sdHidden: true },
        {
          title: '准则版本编号',
          dataIndex: 'criteriaCode',
          scopedSlots: { customRender: 'islink' },
        },
        {
          title: '准则适用范围',
          dataIndex: 'criteriaDeptName',
          width: '300px',
        },
        {
          title: '状态',
          dataIndex: 'state',
        },
        {
          title: '创建人员',
          dataIndex: 'creatorName',
        },
        {
          title: '创建日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
          // sorter: true,
          defaultSortOrder: 'descend',
        },
      ],
      actions: [],
      expressions: [],
    }
  },
  mounted() {
    // 判断用户权限
    riskCriteriaService
      .checkUserPermission({
        formId: 'riskCriteria',
      })
      .then((res) => {
        if (res.data) {
          this.actions = [
            {
              label: '新建',
              id: 'new',
              type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
              permission: null,
              callback: this.createReport,
              index: 4,
            },
            {
              label: '删除',
              id: 'delete',
              type: TableActionTypes.ex.delete,
              permission: null,
              index: 3,
            },
          ]
        }
      })
  },
  methods: {
    createReport() {
      const url = '/risk-criteria-form' // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          // 这里写或者调刷新的方法
          this.refreshDataTable()
        }
      })
    },

    refreshDataTable() {
      if (this.$refs.riskCriteriaTable) {
        this.$refs.riskCriteriaTable.clearSelection()
        this.$refs.riskCriteriaTable.refresh()
      }
    },
    open(record) {
      const url = '/risk-criteria-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refreshDataTable()
        }
      })
    },
    handleSearch() {
      this.expressions = []
      // 报告名称
      if (this.formData.criteriaCode) {
        this.expressions.push({
          dataType: 'str',
          name: 'criteriaCode',
          op: 'like',
          stringValue: '%' + this.formData.criteriaCode + '%',
        })
      }
      // 被评价单位
      if (this.formData.criteriaDeptCode.length > 0) {
        const unitExp = this.formData.criteriaDeptCode.map((item) => item.code)
        this.expressions.push({
          dataType: 'str',
          name: 'criteriaDeptCode',
          op: 'in',
          stringValue: unitExp.join(','),
        })
      }
      // 是否启用
      if (this.formData.status.length > 0) {
        this.expressions.push({
          dataType: 'str',
          name: 'state',
          op: 'eq',
          stringValue: this.formData.status[0].id + '',
        })
      }

      this.expressions = [...this.expressions]
    },

    // 重置
    resetForm() {
      this.formData = {
        criteriaCode: '',
        criteriaDeptCode: [],
        status: [],
      }
    },
  },
}
</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%;
    }
  }
}
</style>
