<template>
  <div style="width: 100%">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '详情',
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '撤销',
                color: 'error',
                popConfirm: {
                  title: '是否确认撤销',
                  confirm: () => {
                    handleRevocation(record);
                  },
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="auditOpen"
      title="详情"
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <Look ref="posRef" :processId="processId" />
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, h, ref, onMounted, watch } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getLoadMyCompletedPage } from '@/api/sys/WFTask';
  import { revokeAudit } from '@/api/sys/WFProcess';
  import { Tag } from 'ant-design-vue';
  import { Look } from './process/page';
  import { useMessage } from '@/hooks/web/useMessage';
  import { getDetail } from '@/api/sys/WFSchemeInfo';
  import { flowStore } from '@/store/modules/flow';
  import { useRoute } from 'vue-router';

  const props = defineProps({
    componentCode: String,
  });
  watch(
    () => props.componentCode,
    (val) => {
      if (val == 4) {
        reload();
      }
    },
  );
  const { createMessage } = useMessage();
  const flowWfDataStore = flowStore();
  const searchInfo = reactive<Recordable>({});
  const auditOpen = ref(false);
  const processId = ref('');
  const route = useRoute();
  searchInfo.category = route.query.category;
  const [registerTable, { reload }] = useTable({
    api: getLoadMyCompletedPage,
    columns: [
      {
        title: '任务',
        dataIndex: 'unitName',
      },
      {
        title: '标题',
        dataIndex: 'processTitle',
      },
      {
        title: '审核',
        dataIndex: 'operationName',
      },
      {
        title: '状态',
        dataIndex: 'isFinished',
        width: 80,
        customRender: ({ record }) => {
          if (record.isFinished == 1) {
            const color = 'red';
            const text = '结束';
            return h(Tag, { color: color }, () => text);
          } else {
            const color = 'green';
            const text = '审批中';
            return h(Tag, { color: color }, () => text);
          }
        },
      },
      {
        title: '提交人',
        dataIndex: 'processUserName',
      },
      {
        title: '创建时间',
        dataIndex: 'createDate',
      },
    ],
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: [
        {
          field: '[StartDate, EndDate]',
          label: '日期范围',
          component: 'RangePicker',
          componentProps: {
            format: 'YYYY-MM-DD',
            placeholder: ['开始日期', '结束日期'],
          },
          colProps: { span: 8 },
        },
        {
          field: 'keyWord',
          label: '关键字',
          component: 'Input',
          colProps: { span: 8 },
        },
      ],
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
    },
  });
  async function handleDetail(record) {
    let data = await getDetail({ code: record.processCode });
    let scheme = JSON.parse(data.scheme.content);
    let wfData = scheme.wfData;
    flowWfDataStore.setWfDataAll(wfData);
    processId.value = record.processId;
    auditOpen.value = true;
    // go('/dashboard/task_look_preview/detail?processId=' + record.processId);
  }
  async function handleRevocation(record) {
    if (record.isFinished == 1) {
      return createMessage.warning('结束的流程不能撤销。');
    }
    const data = await revokeAudit({
      taskId: record.id,
      id: record.processId,
    });
    if (data) {
      reload();
      return createMessage.success('撤销成功');
    } else {
      return createMessage.error('撤销失败');
    }
  }
</script>
<style lang="less">
  .full-modal {
    .ant-modal {
      max-width: 100%;
      top: 0;
    }

    .ant-modal-content {
      height: calc(100vh);
    }

    .ant-modal-body {
      height: 85%;
    }
  }
</style>
