<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          @click="
            downloadExcel(TenderTaskExport, '招标摘要任务数据', getForm().getFieldsValue())
          "
          v-auth="'productManagement:TenderTask:export'"
          >导出</a-button
        >
        <a-button
          type="primary"
          danger
          @click="multipleRemove(TenderTaskRemove)"
          :disabled="!selected"
          v-auth="'productManagement:TenderTask:remove'"
          >删除</a-button
        >
        <a-button
          type="primary"
          @click="handleAdd"
          v-auth="'productManagement:TenderTask:add'"
          >新增</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '详情',
                icon: IconEnum.EDIT,
                type: 'primary',
                ghost: true,
                ifShow: () => {
                  if (
                    record.progressStatus != 'PENDING' &&
                    record.progressStatus != 'STARTED' &&
                    record.progressStatus != 'REVOKED'
                  ) {
                    return true;
                  } else {
                    return false;
                  }
                },
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '下载',
                icon: IconEnum.DOWNLOAD,
                type: 'primary',
                color: 'success',
                ghost: true,
                ifShow: () => {
                  if (
                    record.progressStatus != 'PENDING' &&
                    record.progressStatus != 'STARTED' &&
                    record.progressStatus != 'REVOKED'
                  ) {
                    return true;
                  } else {
                    return false;
                  }
                },
                onClick: handleDownload.bind(null, record),
              },
              {
                label: '终止任务',
                icon: IconEnum.DELETE,
                type: 'primary',
                danger: true,
                ghost: true,
                ifShow: () => {
                  if (record.progressStatus == 'PENDING') {
                    return true;
                  } else {
                    return false;
                  }
                },
                popConfirm: {
                  placement: 'left',
                  title: '是否终止当前任务?',
                  confirm: handleStop.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <TenderTaskModal @register="registerModal" @reload="reload" />
    <DocsDrawer @register="registerDrawer" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import {
    TenderTaskList,
    TenderTaskExport,
    TenderTaskRemove,
  } from '@/api/tenderReview/TenderTask';
  import { downloadExcel } from '@/utils/file/download';
  import { useModal } from '@/components/Modal';
  import TenderTaskModal from './TenderTaskModal.vue';
  import { formSchemas, columns } from './TenderTask.data';
  import { IconEnum } from '@/enums/appEnum';
  import DocsDrawer from '@/views/documentReview/DocumentTasks/DocsDrawer.vue';
  import { useDrawer } from '@/components/Drawer';
  import { DocumentTasksStop } from '@/api/documentReview/DocumentTasks';
  import {
    DocumentTaskResultsInfoByTaskId,
    DocumentTaskResultDownload,
  } from '@/api/documentReview/DocumentTaskResults';
  const [registerDrawer, { openDrawer }] = useDrawer();

  defineOptions({ name: 'TenderTask' });

  const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '招标审核任务列表',
    api: TenderTaskList,
    showIndexColumn: false,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      baseColProps: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 6,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

  const [registerModal, { openModal }] = useModal();

  async function handleDetail(record: Recordable) {
    try {
      let res = await DocumentTaskResultsInfoByTaskId(record.id);

      openDrawer(true, { value: res.result, type: 'markdown' });
      console.log('res', res);
    } catch (ex) {
      openDrawer(true, { value: '加载失败，请刷新页面', type: 'markdown' });
    }
    //根据record.id查询结果详情
  }

  async function handleStop(record: Recordable) {
    await DocumentTasksStop(record.id);
    await reload();
  }

  function handleAdd() {
    openModal(true, { update: false });
  }
  async function handleDownload(record: Recordable) {
    await DocumentTaskResultDownload([record.id]);
    await reload();
  }
  async function handleDelete(record: Recordable) {
    await TenderTaskRemove([record.id]);
    await reload();
  }
</script>

<style scoped></style>
