<template>
  <div>
    <a-card>
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="'searchform'"
        :search-style="{ height: '170px', left: '20px', top: '50px' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'项目名称'" prop="projectTitle">
              <a-input v-model="formData.projectTitle" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'外部检查机构'" prop="inspectionOrg">
              <a-input v-model="formData.inspectionOrg" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'检查类型'" prop="inspectionType">
              <sd-select
                v-model="formData.inspectionType"
                :allow-clear="true"
                :options="itemStatusOptions"
              />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <sd-data-table-ex
        ref="iamExternalProjectTable"
        form-id="iamExternalProject"
        page-id="audit/externalProject/iamExternalProject"
        :columns="columns"
        :actions="actions"
        :show-advance-query="true"
        :search-fields="['projectTitle', 'inspectionOrg', 'inspectionType']"
        show-selection
        :filter-expressions="filterExpressions"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="islink" slot-scope="text, record">
          <a :title="text" @click="rowClick(record)">{{ text }}</a>
        </template>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import TableActionTypes from '@/common/services/table-action-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import components from './_import-components/iam-audit-external-project-import'

export default {
  name: 'IamAuditExternalProject',
  metaInfo: {
    title: '外部项目',
  },
  components: {
    auditAdvancedQuery,
    ...components,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      itemStatusOptions: [],
      formData: { projectTitle: '', inspectionOrg: '', inspectionType: '' },
      expand: false,
      filterExpressions: [],

      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '项目名称',
          dataIndex: 'projectTitle',
          width: '30%',
          scopedSlots: { customRender: 'islink' },
        },
        {
          title: '项目编号',
          dataIndex: 'projectCode',
          sorter: true,
        },
        {
          title: '检查类型',
          dataIndex: 'inspectionType',
        },
        {
          title: '被检查机构',
          dataIndex: 'auditedUnitNames',
        },
        {
          title: '外部检查机构',
          dataIndex: 'inspectionOrg',
        },

        {
          title: '外部检查联系人',
          dataIndex: 'linkMan',
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          // type: TableActionTypes.ex.create,
          permission: 'create',
          type: TableActionTypes.primary,
          callback: () => {
            const url = '/audit-externalProject-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                setTimeout(this.refresh(), 3000)
              }
            })
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
        },
      ],
    }
  },
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  created() {},
  methods: {
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=DICT_CHECK_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.itemStatusOptions = res.data
        }
      })
    },
    // 新建、详情打开新页面
    rowClick(record) {
      const url = '/audit-externalProject-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    handleSearch() {
      // 修改表格过滤条件进行，再刷新进行数据过滤
      // 获取当前表格过滤条件
      this.filterExpressions = []

      // 外部检查机构
      if (this.formData.projectTitle) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'projectTitle',
          op: 'like',
          stringValue: `%${this.formData.projectTitle}%`,
        })
      }
      // 检查类型
      if (this.formData.inspectionOrg) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'inspectionOrg',
          op: 'like',
          stringValue: `%${this.formData.inspectionOrg}%`,
        })
      }
      // 项目名称
      if (this.formData.inspectionType[0]) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'inspectionType',
          op: 'like',
          stringValue: `%${this.formData.inspectionType[0].id}%`,
        })
      }
    },
    searchedClick() {
      this.expand = false
    },
    refresh() {
      this.$refs.iamExternalProjectTable.refresh()
    },
    getInspectionType() {},
  },
}
</script>
