<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"
      @resetForm="resetForm"
    >
      <template>
        <a-col :span="12">
          <a-form-model-item label="转换名称" prop="name">
            <a-select v-model="formData.configName" :options="configOptions" :allow-clear="true">
            </a-select>
          </a-form-model-item>
        </a-col>

        <a-col :span="12">
          <a-form-model-item label="是否启用" prop="databaseType">
            <a-select v-model="formData.databaseType" :allow-clear="true">
              <a-select-option value="1">是</a-select-option>
              <a-select-option value="0">否</a-select-option>
            </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="iamFinFinancialJob"
        page-id="dm/financlalJob/iamFinFinancialJob"
        :search-fields="['name']"
        :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 axios from '@/common/services/axios-instance'
import { message } from '@/common/one-ui'
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-financlaljob-list-import'

export default {
  name: 'IamDmFinanclaljobList',
  metaInfo: {
    title: '定时采集配置列表',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      checked: true,
      checking: false, // 按钮是否执行中
      dataType: [],
      searchform: 'searchform',
      expressions: [],
      formData: {
        name: '',
        databaseStatus: '',
      },
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '转换名称',
          dataIndex: 'configName',
          scopedSlots: { customRender: 'islink' },
          width: '300px',
        },
        {
          title: '执行规则',
          dataIndex: 'jobCron',
        },
        {
          title: '维护人员',
          dataIndex: 'creatorName',
        },
        {
          title: '维护日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '启用',
          dataIndex: 'jobStatus',
          sdRender: TableColumnTypes.ex.switch,
        },
      ],
      actions: [
        {
          label: '删除',
          permission: null,
          type: TableActionTypes.ex.delete,
        },
        {
          label: '新建',
          type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
          permission: null, // 权限控制
          callback: () => {
            const url = '/iam-dm-financlaljob-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refresh()
              }
            })
            // window.open('#/sd-flow-guide?code=AUDIT_MATERIAL&projectId=' + this.projectId)
          },
        },
        {
          label: '立即执行',
          permission: null,
          id: 'runNow',
          callback: () => {
            this.runJobNow(this.$refs.dataTable.getSelectedRows())
          },
        },
      ],
    }
  },
  mounted() {
    axios({
      url: 'api/xcoa-mobile/v1/iamfinfinancialjob/getOptions',
      method: 'get',
    }).then((res) => {
      if (res) {
        this.configOptions = [...res.data].map((item) => {
          return {
            value: item.configName,
            label: item.configName,
            key: item.id,
          }
        })
      }
    })
  },
  methods: {
    // 开关点击事件
    switchClick(record) {
      // console.log(record)
    },
    clearSelection() {
      this.$refs.sjzlTable.clearSelection()
    },
    // 刷新列表
    refresh() {
      // this.$refs.sjzlTable.refresh()
      this.$emit('refreshTable')
    },
    // 新建、详情打开新页面
    rowClick(record) {
      const url = '/iam-dm-financlaljob-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 重置查询
    resetForm() {
      this.expressions = []
      this.formData = {}
    },
    // 查询
    handleSearch() {
      this.expressions = []

      // 配置名称
      if (this.formData.configName) {
        this.expressions.push({
          dataType: 'str',
          name: 'configName',
          op: 'eq',
          stringValue: `${this.formData.configName}`,
        })
      }

      // 是否启用
      if (this.formData.databaseType) {
        this.expressions.push({
          dataType: 'str',
          name: 'jobStatus',
          op: 'eq',
          stringValue: `${this.formData.databaseType}`,
        })
      }
    },
    runJobNow(val) {
      if (val.length === 0 || val.length > 1) {
        message.warning('请选择一个任务执行！')
      } else {
        if (val[0].jobStatus === '否') {
          message.warning('请先启用该任务！')
        } else {
          axios({
            url: `api/xcoa-mobile/v1/iamfinfinancialjob/runJobNow?id=${val[0].id}`,
            method: 'get',
          }).then((res) => {
            if (res.data.indexOf('成功') !== -1) {
              message.success(res.data, 1)
            } else {
              message.error(res.data, 1)
            }
          })
        }
      }
    },
  },
}
</script>

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