<template>
  <div>
    <a-card>
      <!-- 高级搜索组件 -->
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{ height: '130px', left: '20px', top: '57px' }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'流程名称'" prop="lcName">
              <a-input v-model="formData.lcName" allow-clear />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'适用组织范围'" prop="orgScope">
              <a-input v-model="formData.orgScope" allow-clear />
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <sd-data-table-ex
        ref="workFlowDataTable"
        :filter-expressions="expressions"
        :columns="columns"
        :actions="actions"
        form-id="iamWorkFlow"
        data-url="api/xcoa-mobile/v1/iamworkflow/all-list"
        :search-fields="['lcName', 'isDefault', 'orgScope', 'creatorName']"
        show-selection
        :show-advance-query="true"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="islink" slot-scope="text, record">
          <a @click="rowClick(record)">{{ text }}</a>
        </div>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
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 auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import auditAdvancedGroupMixins from '../../../components/audit-advanced-group-mixins'
import auditAdvancedQuery from '../../../components/audit-advanced-query.vue'
import components from './_import-components/audit-workflow-list-import'

export default {
  name: 'AuditWorkflowList',
  metaInfo: {
    title: '作业流程',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      searchform: 'searchform',
      expressions: [],
      formData: {
        lcName: '',
        orgScope: '',
      },
      formId: 'iamWorkNode',
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '流程名称',
          dataIndex: 'lcName',
          scopedSlots: { customRender: 'islink' },
        },
        {
          title: '是否默认',
          dataIndex: 'isDefault',
        },
        {
          title: '适用组织范围',
          dataIndex: 'orgScope',
        },
        {
          title: '编制人员',
          dataIndex: 'creatorName',
        },
        {
          title: '编制日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
          defaultSortOrder: 'descend', // 没有点击任何排序列时，默认的排序列
          sorter: true,
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          permission: 'create',
          type: TableActionTypes.primary,
          callback: () => {
            const url = '/audit-workflow-form' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refresh()
              }
            })
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理（对sd-data-table无效）
        },
      ],
    }
  },
  computed: {},
  created() {},
  methods: {
    refresh() {
      return this.$refs.workFlowDataTable.refresh(true)
    },
    rowClick(record) {
      const url = '/audit-workflow-form?record=' + record.id // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.refresh()
        }
      })
    },
    // 查询
    handleSearch() {
      this.expressions = []

      // 流程名称
      if (this.formData.lcName) {
        this.expressions.push({
          dataType: 'str',
          name: 'lcName',
          op: 'like',
          stringValue: `%${this.formData.lcName}%`,
        })
      }
      // 适用组织范围
      if (this.formData.orgScope) {
        this.expressions.push({
          dataType: 'str',
          name: 'orgScope',
          op: 'like',
          stringValue: `%${this.formData.orgScope}%`,
        })
      }
    },
  },
}
</script>

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