<!--
 * @Author: Mm
 * @Date: 2025-04-07 10:13:17
 * @LastEditors: Mm
 * @LastEditTime: 2025-05-19 10:26:58
 * 不写bug的程序员不是一个好程序员!
-->
<script setup lang="ts">
import type { Recordable } from '@vben/types';

import { computed, getCurrentInstance, nextTick, onMounted, ref } from 'vue';

import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
import { getVxePopupContainer } from '@vben/utils';

import { Modal, Popconfirm, Space, Tag, message } from 'ant-design-vue';
import dayjs from 'dayjs';

import {
  useVbenVxeGrid,
  vxeCheckboxChecked,
  type VxeGridProps,
} from '#/adapter/vxe-table';

import { dwspTasksList,taskRemove } from '#/api/tasks/index';
import { commonDownloadExcel } from '#/utils/file/download';
import approvalModal from './approval-modal.vue'
import { dwspColumns, queryDwspSchema } from './data';
import InitiateProcess from '../workspace/InitiateProcess.vue';
import vuexStore from '#/store/vuex.js';
import { useRouter } from 'vue-router';

const router = useRouter();

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: queryDwspSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  // 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
  // columns: columns(),
  columns:dwspColumns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        const formValuess = tableApi.formApi.form.values;
        return await dwspTasksList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValuess,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'instanceId',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'web-news-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

interface ProcessFormInstance {
  submit: () => void;
  validate: (callback: (validForm: any, validProcess: any) => void) => void;
}
const processForm = ref<ProcessFormInstance | null>(null);
const isMobile = computed(() => {
  return window.screen.width < 450;
});
const loginUser = computed(() => {
  return vuexStore.state.loginUser;
});

const submitLoading = ref(false);
const selectForm = ref<any>({
  formId: 'wf67fdbee6c717f47dcf804ac8',
  version: 7,
  processDefId: 'wf2c802379908f4ad2b04b2cbba5c295ac',
  formName: '任务流程',
  logo: {
    icon: 'bi:people-fill',
    background: '#1e90ff',
  },
  groupId: 104,
  isStop: false,
  remark: '备注说明',
  updated: '2025-04-21 16:43:26',
  nodeId: undefined,
  instanceId: undefined,
});
const openItemDl = ref(false);
const recentlyUsed = ref<any>([]);

function resetForm() {
  selectForm.value = {
    formId: 'wf67fdbee6c717f47dcf804ac8',
    version: 7,
    processDefId: 'wf2c802379908f4ad2b04b2cbba5c295ac',
    formName: '任务流程',
    logo: {
      icon: 'bi:people-fill',
      background: '#1e90ff',
    },
    groupId: 104,
    isStop: false,
    remark: '备注说明',
    updated: '2025-04-21 16:43:26',
    nodeId: undefined,
    instanceId: undefined,
  };
}

function submitForm() {
  processForm.value?.validate(async (validForm: any, validProcess: any) => {
    if (!isMobile.value) {
      if (validForm && validProcess) {
        processForm.value?.submit();
        // 延迟一下，确保提交完成后入库再刷新
        await new Promise((resolve) => setTimeout(resolve, 500));
        tableApi.reload();
      } else {
        message.warning('请完善表单/流程选项😥');
      }
    }
  });
}

// 审批弹框
const [ApprovalModal, approvalModalApi] = useVbenModal({
  connectedComponent: approvalModal,
});

//详情
function handleDetail(row: Recordable<any>) {
  router.push('/tasks/detail?instanceId=' + row.instanceId+'&nodeId=' + row.nodeId);
}
// 编辑
async function handleEdit(row: Recordable<any>) {
  approvalModalApi.setData({instanceId:row.instanceId,taskId:row.taskId});
  approvalModalApi.open();
}


</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="待我审批任务">
      <template #toolbar-tools>
        <Space>
         
        </Space>
      </template>
      <template #missionStatus="{ row }">
        <Tag color="#006be6" v-if="row.missionStatus === '审批进行中'">{{
          row.missionStatus
        }}</Tag>
        <Tag color="#87d068" v-else>{{ row.missionStatus }}</Tag>
      </template>
      <template #startUser="{row}">
        {{row.startUser.nickName}}
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button @click.stop="handleDetail(row)"> 详情 </ghost-button>
          <ghost-button @click.stop="handleEdit(row)" > 审批 </ghost-button>
        </Space>
      </template>
    </BasicTable>
    <!-- 审核弹框 -->
    <ApprovalModal @reload="tableApi.query()" />
  </Page>
</template>
