<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

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

import { useRoute, useRouter } from 'vue-router';

import { Page } from '@vben/common-ui';

import { ElMessage } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getTaskAttachmentList } from '#/api/archive/operationsManagement/operationsManagement';

import { queryFormSchema, tableColumns } from './collectAttach-data';

const router = useRouter();
const route = useRoute();

// 打印当前路由和路由参数
console.log('当前路由路径,参数:', route.path, route.query);

// 表单字段到 API 字段的映射
function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
    ...formValues,
  };
  Object.keys(route.query).forEach((key) => {
    apiParams[key] = route.query[key];
  });
  return apiParams;
}

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

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);

        // 调用API
        const response = await getTaskAttachmentList(apiParams);

        // 映射 API 响应数据到页面显示字段
        const items = response.rows;

        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-checklis-collectAttach-index',
};

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

// 批量下载附件
function batchDownloadAttachments() {
  const checkRecords = basicTableApi.grid.getCheckboxRecords();
  if (checkRecords.length <= 0) {
    ElMessage.warning('请选择要下载附件的任务');
    return;
  }

  // 模拟批量下载附件
  console.log('批量下载附件:', checkRecords);
  ElMessage.info('批量下载附件功能开发中');
}

// 查看任务详情
function viewDetail(row: any) {
  // 跳转到任务详情页面
  // const queryParams = new URLSearchParams();
  // queryParams.append('id', row.id);
  // Object.keys(route.query).forEach((key) => {
  //   queryParams.append(key, route.query[key] as string);
  // });
  router.push(`/schedule/detail/${row.taskId}`);
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="附件收集">
      <template #toolbar-tools>
        <ElSpace>
          <ElButton
            type="primary"
            :disabled="
              !(basicTableApi?.grid?.getCheckboxRecords?.()?.length > 0)
            "
            @click="batchDownloadAttachments"
          >
            批量下载附件
          </ElButton>
        </ElSpace>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <ElButton size="small" type="primary" plain @click="viewDetail(row)">
            查看
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>

<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
