<!--
 * @Author: Mm
 * @Date: 2025-04-07 10:13:17
 * @LastEditors: Mm
 * @LastEditTime: 2025-05-29 18:27:08
 * 不写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, Table,Button } from 'ant-design-vue';
import dayjs from 'dayjs';

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

import { ycldTasksList, taskRemove } from '#/api/tasks/index';
import { ycldColumns, queryDwspSchema } from './data';
import { useRouter } from 'vue-router';
import xzdDetailModal from './xzd-detail-modal.vue';

import { getDictOptions } from '#/utils/dict';
import { DictTag } from '#/components/dict';
const dictInfo = getDictOptions('biz_task_work_status');

const router = useRouter();
import { useUserStore } from '@vben/stores';

const userStore = useUserStore();
const userId = computed(() => {
  return userStore.userInfo?.userId;
});

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: ycldColumns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        const formValuess = tableApi.formApi.form.values;
        return await ycldTasksList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValuess,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'ycld-index',
};

const columns:any = [
  {
    title: '协作单位',
    dataIndex: 'deptName',
    key: 'deptName',
    width:320,
    align: 'center',

  },
  {
    title: '协作人员',
    dataIndex: 'sponsorName',
    key: 'sponsorName',
    width:150,
    align: 'center',


  },
  {
    title: '当前状态',
    dataIndex: 'status',
    width:150,
    key: 'status',
    align: 'center',

  },
  {
    title: '协作内容',
    dataIndex: 'workContent',
    key: 'workContent',
    ellipsis: true,
    align: 'center',
  },
  {
    title: '操作',
    key: 'action',
    width:130,
    align: 'center',
  },
];

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

// 协作单详情弹窗
const [XzdDetailModal, xzdDetailModalApi] = useVbenModal({
  connectedComponent: xzdDetailModal,
});







//详情
function handleDetail(row: Recordable<any>) {
  router.push(
    '/tasks/detail?instanceId=' + row.taskNo + '&nodeId=' + row.nodeId+'&isdb=' + true,
  );
}

// 协作单详情
async function handleXzdDetail(row: Recordable<any>) {
  xzdDetailModalApi.setData({
    id: row.id,
  });
  xzdDetailModalApi.open();
}

</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="已处理的任务">
      <template #toolbar-tools>
        <Space>
          <a-button>
            {{ $t('pages.common.export') }}
          </a-button>
        </Space>
      </template>

     <template #expand_content="{ row }">
       <div
         style="width: 100%; padding: 10px 15px"
         v-if="row.bizTaskWorkList && row.bizTaskWorkList.length > 0"
       >
         <Table
           size="small"
           :dataSource="row.bizTaskWorkList"
           :columns="columns"
           :pagination="false"
           bordered
         >
           <template #bodyCell="{ column, record }">
             <template v-if="column.key === 'status'">
               <DictTag :dicts="dictInfo" :value="record.status"></DictTag>
             </template>

             <template v-else-if="column.key === 'action'">
               <span>
                 <Button type="link"  style="padding: 0 5px"
                 @click.stop="handleXzdDetail(record)">详情</Button>
               </span>
             </template>
           </template>
         </Table>
       </div>
       <span v-else></span>
     </template>
      <template #action="{ row }">
        <Space>
          <ghost-button @click.stop="handleDetail(row)"> 详情 </ghost-button>
        </Space>
      </template>
    </BasicTable>
    <!-- 协作单详情弹框 -->
    <XzdDetailModal @reload="tableApi.query()" />
  </Page>
</template>
