<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),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="auditOpen"
      title="审核"
      footer=""
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <Audit
        ref="posRef"
        :processId="processId"
        :taskId="taskId"
        :isRead="isRead"
        :type="type"
        @closeModel="closeMolder"
      />
    </a-modal>
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="previewOpen"
      title="流程发起"
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <Preview
        ref="posRef"
        :code="childRecord.childSchemeInfoCode"
        :ParentNodeId="childRecord.unitId"
        :ParentTaskId="childRecord.id"
        :ParentProcessId="childRecord.processId"
        @closeModel="closeMolder"
      />
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, h, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getLoadMyUncompletedPage } from '@/api/sys/WFTask';
  import { getDetail } from '@/api/sys/WFSchemeInfo';
  import { Tag } from 'ant-design-vue';
  import { Audit } from './process/page';
  import { flowStore } from '@/store/modules/flow';
  import { Preview } from '../create/index';
  import { useRoute } from 'vue-router';

  const flowWfDataStore = flowStore();
  const searchInfo = reactive<Recordable>({});
  const processId = ref('');
  const taskId = ref('');
  const isRead: any = ref(0);
  const type = ref('');
  const auditOpen = ref(false);
  const previewOpen = ref(false);
  const childRecord = ref();
  const route = useRoute();
  searchInfo.category = route.query.category;
  const [registerTable, { reload }] = useTable({
    api: getLoadMyUncompletedPage,
    columns: [
      {
        title: '任务',
        dataIndex: 'unitName',
      },
      {
        title: '标题',
        dataIndex: 'processTitle',
      },
      {
        title: '状态',
        dataIndex: 'isUrge',
        width: 80,
        customRender: ({ record }) => {
          if (record.isUrge == '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) {
    if (record.type == 3) {
      previewOpen.value = true;
      childRecord.value = record;
    } else {
      let data = await getDetail({ code: record.processCode });
      let scheme = JSON.parse(data.scheme.content);
      let wfData = scheme.wfData;
      flowWfDataStore.setWfDataAll(wfData);
      auditOpen.value = true;
      processId.value = record.processId;
      taskId.value = record.id;
      type.value = record.type;
    }
    // go(
    //   '/dashboard/task_audit_preview/detail?processId=' +
    //     record.processId +
    //     '&taskId=' +
    //     record.id +
    //     '&type=' +
    //     record.type +
    //     '&isRead=0',
    // );
  }
  function closeMolder() {
    auditOpen.value = false;
    previewOpen.value = false;
    reload();
  }
</script>
<style lang="less">
  .full-modal {
    .ant-modal {
      max-width: 100%;
      top: 0;
    }

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

    .ant-modal-body {
      height: 85%;
    }
  }

  ::v-deep .full-modal .ant-modal-body {
    height: 95% !important;
  }
</style>
