<template>
  <div>
    <audit-advanced-query
      :expand="expand"
      :search-data="formData"
      :ref-name="searchform"
      :search-style="{ height: '135px', left: '0px', top: '40px' }"
      :search-fun="handleSearch"
      @searchedClick="searchedClick"
    >
      <template>
        <a-col :span="12">
          <a-form-model-item label="数据源名称" prop="configName">
            <a-input v-model="formData.configName" allow-clear />
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item label="数据源ID" prop="databaseId">
            <a-input v-model="formData.databaseId" allow-clear />
          </a-form-model-item>
        </a-col>
      </template>
    </audit-advanced-query>
    <a-card>
      <sd-data-table-ex
        ref="dataTable"
        :filter-expressions="expressions"
        :columns="columns"
        :actions="actions"
        form-id="iamFinSysEtlConfig"
        page-id="dm/finsysetlconfig/iamFinSysEtlConfig"
        :search-fields="['configName']"
        :show-advance-query="true"
        show-selection
        :modal-props="{ class: 'noBottom' }"
        @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 moment from 'moment'
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 '@product/iam/components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '@product/iam/components/audit-advanced-query-mixins'
import components from './_import-components/iam-dm-finsysetlconfig-list-import'

export default {
  name: 'IamDmFinsysetlconfigList',
  metaInfo: {
    title: '财务转换配置列表',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      dataType: [],
      searchform: 'searchform',
      expressions: [],
      formData: {
        configName: '',
        databaseId: '',
      },
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          scopedSlots: { customRender: 'islink' },
          width: '200px',
        },
        {
          title: '数据源ID',
          dataIndex: 'databaseId',
        },
        {
          title: '接入系统代码',
          dataIndex: 'appCode',
        },
        {
          title: '末次导入日期',
          dataIndex: 'lastImportTime',
          sdRender: TableColumnTypes.date,
          width: '200px',
        },
        {
          title: '财务系统版本',
          dataIndex: 'financialSystemId',
        },
        {
          title: '默认采集年',
          dataIndex: 'defaultBeginYear',
          customRender: (text, record, index) => {
            return moment(text).format('YYYY')
          },
        },
        {
          title: '采集开始年',
          dataIndex: 'collYears',
          customRender: (text, record, index) => {
            return moment(text).format('YYYY')
          },
        },
        {
          title: '维护人员',
          dataIndex: 'creatorName',
        },
        {
          title: '维护日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
        },
      ],
      actions: [
        {
          label: '删除',
          permission: null,
          type: TableActionTypes.ex.delete,
        },
        {
          label: '新建',
          type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
          permission: null, // 权限控制
          callback: () => {
            const url = '/iam-dm-finsysetlconfig-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refresh()
              }
            })
            // window.open('#/sd-flow-guide?code=AUDIT_MATERIAL&projectId=' + this.projectId)
          },
        },
      ],
    }
  },
  mounted() {
    // 获取数据类型字典
    axios({
      url: `api/xcoa-mobile/v1/iam-law/dictionary?key=DATABASE_TYPE`,
      method: 'get',
    }).then((res) => {
      this.dataType = res.data.map((item) => {
        return {
          label: item.name,
          value: item.id,
        }
      })
    })
  },
  methods: {
    clearSelection() {
      this.$refs.sjzlTable.clearSelection()
    },
    // 刷新列表
    refresh() {
      // this.$refs.sjzlTable.refresh()
      this.$emit('refreshTable')
    },
    // 新建、详情打开新页面
    rowClick(record) {
      const url = '/iam-dm-finsysetlconfig-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 查询
    handleSearch() {
      this.expressions = []

      // 资料名称
      if (this.formData.name) {
        this.expressions.push({
          dataType: 'str',
          name: 'configName',
          op: 'like',
          stringValue: `%${this.formData.configName}%`,
        })
      }

      // 被审计单位
      if (this.formData.databaseType) {
        this.expressions.push({
          dataType: 'str',
          name: 'databaseId',
          op: 'like',
          stringValue: `%${this.formData.databaseId}%`,
        })
      }
    },
  },
}
</script>

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