<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="FTP文件名" prop="ftpFileName">
            <a-input v-model="formData.ftpFileName" allow-clear />
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item label="任务执行状态" prop="taskStatus">
            <a-select v-model="formData.taskStatus" :options="dataType" :allow-clear="true">
            </a-select>
          </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="iamFinDataTransformLog"
        page-id="dm/datatransLog/iamFinDataTransformLog"
        :search-fields="['name']"
        :show-advance-query="true"
        show-selection
        :modal-props="{ class: 'noBottom' }"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a @click="rowClick(record)">详情</a>
        </div>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
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-datatranslog-list-import'

export default {
  name: 'IamDmDatatranslogList',
  metaInfo: {
    title: '采集日志管理列表',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      checked: true,
      checking: false, // 按钮是否执行中
      dataType: [
        { label: '待启动', value: 'READY' },
        { label: '执行中', value: 'RUNNING' },
        { label: '已完成', value: 'FINSHED' },
        { label: '发生错误', value: 'ERROR' },
      ],
      searchform: 'searchform',
      expressions: [],
      formData: {
        name: '',
        databaseStatus: '',
      },
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '任务编号',
          dataIndex: 'taskId',
          width: '300px',
        },
        {
          title: '任务开始时间',
          dataIndex: 'beginTime',
          sdRender: TableColumnTypes.dateTime,
        },
        {
          title: '任务结束时间',
          dataIndex: 'endTime',
          sdRender: TableColumnTypes.dateTime,
        },
        {
          title: '任务执行状态',
          dataIndex: 'taskStatus',
          customRender: (text, record, index) => {
            if (record.taskStatus === 'READY') {
              return '待启动'
            } else if (record.taskStatus === 'RUNNING') {
              return '执行中'
            } else if (record.taskStatus === 'FINSHED') {
              return '已完成'
            } else if (record.taskStatus === 'ERROR') {
              return '发生错误'
            } else {
              return ''
            }
          },
        },
        {
          title: 'FTP文件名',
          dataIndex: 'ftpFileName',
        },
        {
          title: 'XX平台单位编码',
          dataIndex: 'lastUnitCode',
        },
        {
          title: '是否发生错误',
          dataIndex: 'hasError',
          customRender: (text, record, index) => {
            return record.hasError === 1 ? '是' : '否'
          },
        },
        {
          title: '详情',
          scopedSlots: { customRender: 'islink' },
        },
      ],
      actions: [
        {
          label: '删除',
          permission: null,
          type: TableActionTypes.ex.delete,
        },
      ],
    }
  },
  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: {
    // 刷新列表
    refresh() {
      // this.$refs.sjzlTable.refresh()
      this.$emit('refreshTable')
    },
    // 新建、详情打开新页面
    rowClick(record) {
      const url = '/iam-dm-datatranslog-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 查询
    handleSearch() {
      this.expressions = []

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

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

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