<template>
  <div>
    <BasicTable @register="registerTable"
                @selection-change="handleSelectionChange"
    >
      <template #tableTitle>
        <a-button
          danger
          type="primary"
          :disabled="state.multiple"
          @click="handleDel()"
        >删除</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '查阅审批',
                icon: 'fa6-solid:eye',
                onClick: handleViewAudit.bind(null, record)
              }, {
                label: '删除',
                icon: 'fa6-solid:trash',
                onClick: handleDel.bind(null, record)
              }]"
          />
        </template>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts">
  /**
   * 提供模板规范代码参考,请尽量保证编写代码风格跟模板规范代码一致
   * 采用vben-动态表格表单封装组件编写,不采用 setup 写法
   * Copyright © 2023-2023 <a href="https://godolphinx.org">海豚生态开源社区</a> All rights reserved.
   * author wangxiang4
   */
  import { defineComponent, reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns } from './workflowCopy.data';
  import { getTaskDefinition } from '/@/api/platform/workflow/controller/task';
  import { PageEnum } from '/@/enums/workflowEnum';
  import { useRouter } from 'vue-router';
  import { listWorkflowCopy, delWorkflowCopy } from '/@/api/platform/workflow/extension/controller/workflowCopy';
  import {useMessage} from '/@/hooks/web/useMessage';

  interface TableState {
    ids: string[];
    single: boolean;
    multiple: boolean;
  }
  export default defineComponent({
    name: 'WorkflowCopy',
    components: {
      BasicTable,
      TableAction,
    },
    setup() {
      const state = reactive<TableState>({
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
      });
      const { push } = useRouter();
      const { createConfirm } = useMessage();
      const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
        api: listWorkflowCopy,
        rowKey: 'id',
        columns,
        rowSelection: { type: 'checkbox' },
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        clickToRowSelect: false,
        showIndexColumn: false,
        actionColumn: {
          width: 240,
          title: '操作',
          dataIndex: 'action',
          fixed: false
        },
        handleSearchInfoFn: () => clearSelectedRowKeys()
      });

      function handleSelectionChange(selection?: Recordable) {
        const rowSelection = toRaw(selection?.keys) || [];
        state.ids = rowSelection;
        state.single = rowSelection.length != 1;
        state.multiple = !rowSelection.length;
      }

      async function handleViewAudit(record: Recordable) {
        const task = await getTaskDefinition({
          processInsId: record.processInsId,
          processDefId: record.processDefId
        });
        await push({
          path: PageEnum.TASK_FORM_VIEW_PAGE,
          query: {
            _meta: 'y',
            title: record.processInsName,
            formTitle: record.processInsName,
            formType: task.formType,
            formKey: task.formKey,
            processDefKey: task.processDefKey,
            taskDefKey: task.taskDefKey,
            processInsId: task.processInsId,
            processDefId: task.processDefId,
            taskId: task.taskId,
            businessId: task.businessId
          }
        });
      }

      async function handleDel(record?: Recordable) {
        const ids = record?.id || toRaw(state.ids);
        createConfirm({
          iconType: 'warning',
          title: '警告',
          content: '是否确认删除所选项编号为"' + ids + '"的数据项?',
          onOk: async () => {
            await delWorkflowCopy(ids);
            handleRefreshTable();
          }
        });
      }

      function handleRefreshTable() {
        clearSelectedRowKeys();
        reload();
      }

      return {
        state,
        registerTable,
        handleViewAudit,
        handleDel,
        handleSelectionChange
      };
    }
  });
</script>
