<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'name'">
          {{ record.name }}
          <a-button v-if="record.rollBack"
                    size="small"
                    type="primary"
                    @click="handleRollBackTask(record)"
          >回滚任务</a-button>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              label: '历史',
              icon: 'fa6-solid:clock-rotate-left',
              onClick: handleProcessView.bind(null, record)
            }]"
          />
        </template>
      </template>
    </BasicTable>
    <WorkflowChartModal @register="registerModal"/>
  </div>
</template>

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

  export default defineComponent({
    name: 'WorkflowHistoryTask',
    components: {
      BasicTable,
      TableAction,
    },
    setup() {
      const { push } = useRouter();
      const { createConfirm, createMessage } = useMessage();
      const [registerTable, { reload }] = useTable({
        api: listHistoryTask,
        rowKey: 'id',
        columns: historyColumns,
        formConfig: {
          compact: true,
          labelWidth: 100,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
          showAdvancedButton: true,
          autoAdvancedLine: 3,
          fieldMapToTime: [['dateRange', ['beginTime', 'endTime'], 'YYYY-MM-DD']]
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        clickToRowSelect: false,
        showIndexColumn: false,
        actionColumn: {
          width: 240,
          title: '操作',
          dataIndex: 'action',
          fixed: false
        },
      });

      async function handleProcessView(record: Recordable) {
        const task = await getTaskDefinition({
          taskDefKey: record.taskDefKey,
          processInsId: record.processInsId,
          processDefId: record.processDefId
        });
        await push({
          path: PageEnum.TASK_FORM_VIEW_PAGE,
          query: {
            _meta: 'y',
            taskId: record.id,
            taskDefKey: task.taskDefKey,
            title: `${record.processDefName}【${record.name}】`,
            formTitle: `${record.processDefName}`,
            formType: task.formType,
            formKey: task.formKey,
            processDefKey: task.processDefKey,
            processInsId: task.processInsId,
            processDefId: task.processDefId,
            businessId: task.businessId,
          }
        });
      }

      function handleRollBackTask(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: '警告',
          content: '确定回滚该已办任务吗?',
          onOk: async () => {
            const { taskInfo } = record;
            await undoTask({
              processInsId: record.processInsId,
              currentTaskId: taskInfo.id,
              currentTaskDefKey: taskInfo.taskDefKey,
              undoTaskId: record.id,
              undoTaskDefKey: record.taskDefKey
            });
            handleRefreshTable();
          }
        });
      }

      function handleRefreshTable() {
        reload();
      }

      return {
        handleProcessView,
        registerTable,
        handleRollBackTask,
      };
    }
  });
</script>
