<script setup lang="tsx">
import type { ProColumns } from '@pubinfo/pro-components';
import { ProTable } from '@pubinfo/pro-components';
import { useRouter } from 'vue-router';
import { postBpmTaskDonePage } from '@/api/modules/bpmTask/guanlihoutailiuchengrenwushili';
import { millisecondsToDayHourMinuteSecond } from '@/utils/fun';
import { ACTION, BPM_TASK_STATUS } from '../flow-manage/enum';

defineOptions({
  name: 'DoneList',
});

async function request(params: any) {
  const { current, pageSize, ...rest } = params;
  const body = {
    currentPage: current,
    pageSize,
    doSearchTotal: true,
    ...rest,
  };
  const res = await postBpmTaskDonePage(body);
  return {
    success: res.success,
    data: res.data?.records ?? [],
    total: res.data?.total ?? 0,
  };
}

const columns: ProColumns<API.BpmTaskRespVO> = [
  {
    hideInSearch: true,
    title: '流程名',
    dataIndex: 'processName',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '摘要',
    dataIndex: 'summary',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '发起人',
    dataIndex: 'startUser',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '发起时间',
    dataIndex: 'processCreateTime',
    ellipsis: true,
  },
  {
    valueType: 'a-input',
    bind: 'value',
    title: '当前任务',
    dataIndex: 'name',
    fieldProps: {
      placeholder: '请输入任务名称',
      allowClear: true,
    },
  },
  // {
  //   hideInSearch: true,
  //   title: '当前任务',
  //   dataIndex: 'nameDisplay',
  //   ellipsis: true,
  // },
  {
    hideInSearch: true,
    title: '任务开始时间',
    dataIndex: 'createTime',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '任务结束时间',
    dataIndex: 'endTime',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '审批状态',
    dataIndex: 'status',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '审批建议',
    dataIndex: 'reason',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '耗时',
    dataIndex: 'durationInMillis',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '流程编号',
    dataIndex: 'processInstanceId',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '任务编号',
    dataIndex: 'id',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    width: 60,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
];

const router = useRouter();

function onAction(action: ACTION, record: API.BpmTaskRespVO) {
  switch (action) {
    case ACTION.HISTORY:
      router.push({ name: 'doneProcessDetail', params: { processInstanceId: record.processInstanceId }, query: { taskId: record.id } });
      break;
    default:
      break;
  }
}
</script>

<template>
  <div
    py-10px
    px-10px
    w-full
    h-full
  >
    <ProTable
      row-key="id"
      :request="request"
      :columns="columns"
      auto-height
      :scroll="{ x: 1400 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'processName'">
          {{ record.processInstance?.name || '-' }}
        </template>
        <template v-if="column.dataIndex === 'summary'">
          <div
            v-if="record.processInstance.summary && record.processInstance.summary.length > 0"
            class="flex flex-col"
          >
            <div v-for="(item, index) in record.processInstance.summary" :key="index">
              {{ item.key }} : {{ item.value }}
            </div>
          </div>
        </template>
        <template v-if="column.dataIndex === 'startUser'">
          {{ record.processInstance?.startUser?.nickname || '-' }}
        </template>
        <template v-if="column.dataIndex === 'processCreateTime'">
          {{ record.processInstance?.createTime || '-' }}
        </template>
        <template v-if="column.dataIndex === 'name'">
          {{ record.name || '-' }}
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          {{ record.createTime || '-' }}
        </template>
        <template v-if="column.dataIndex === 'endTime'">
          {{ record.endTime || '-' }}
        </template>
        <template v-if="column.dataIndex === 'status'">
          <span>{{ BPM_TASK_STATUS.find((item: any) => item.value === record.status)?.label || '-' }}</span>
        </template>
        <template v-if="column.dataIndex === 'reason'">
          {{ record.reason || '-' }}
        </template>
        <template v-if="column.dataIndex === 'durationInMillis'">
          {{ millisecondsToDayHourMinuteSecond(record.durationInMillis) }}
        </template>
        <template v-if="column.dataIndex === 'processInstanceId'">
          {{ record.processInstanceId || '-' }}
        </template>
        <template v-if="column.dataIndex === 'id'">
          {{ record.id || '-' }}
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a @click.prevent="onAction(ACTION.HISTORY, record)">历史</a>
        </template>
      </template>
    </ProTable>
  </div>
</template>
