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

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

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

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

import { ElMessage } from 'element-plus';

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

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

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

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


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

// 模拟访客检查任务数据
const mockData = {
  rows: [
    {
      id: '1',
      taskName: '访客检查任务1',
      station: '油站1',
      attachments: ['附件1.pdf', '附件2.docx'],
      remark: '这是一个重要的访客检查任务',
      executor: '张三',
      position: 'manager',
      comment: 'commented',
      deadline: '2024-05-01',
      handleTime: '2024-04-30',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: '2',
      taskName: '访客检查任务2',
      station: '油站2',
      attachments: ['附件3.jpg'],
      remark: '需要重点检查安全措施',
      executor: '李四',
      position: 'clerk',
      comment: 'uncommented',
      deadline: '2024-05-02',
      handleTime: '2024-05-03',
      status: 'overdue',
      createType: 'manual',
    },
    {
      id: '3',
      taskName: '访客检查任务3',
      station: '油站3',
      attachments: [],
      remark: '常规访客检查',
      executor: '王五',
      position: 'safety',
      comment: 'commented',
      deadline: '2024-05-03',
      handleTime: '2024-05-03',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: '4',
      taskName: '访客检查任务4',
      station: '油站1',
      attachments: ['附件4.pdf', '附件5.pdf', '附件6.pdf'],
      remark: '需要拍照记录',
      executor: '赵六',
      position: 'manager',
      comment: 'uncommented',
      deadline: '2024-05-04',
      handleTime: '2024-05-05',
      status: 'overdue',
      createType: 'manual',
    },
    {
      id: '5',
      taskName: '访客检查任务5',
      station: '油站2',
      attachments: ['附件7.docx'],
      remark: '检查访客登记情况',
      executor: '孙七',
      position: 'clerk',
      comment: 'commented',
      deadline: '2024-05-05',
      handleTime: '2024-05-05',
      status: 'onTime',
      createType: 'system',
    },
  ],
  total: 5,
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 模拟API请求 - collectAttach 页面应调用 /api/visitorFeedback/collectAttach/list 接口
        console.log('查询参数:', formValues, page);
        // 模拟过滤
        let filteredRows = [...mockData.rows];

        // 片区过滤
        if (formValues.area) {
          filteredRows = filteredRows.filter(
            (row) => row.area === formValues.area,
          );
        }

        // 油站过滤
        if (formValues.station) {
          filteredRows = filteredRows.filter(
            (row) => row.station === formValues.station,
          );
        }

        // 完成状态过滤
        if (formValues.status) {
          filteredRows = filteredRows.filter(
            (row) => row.status === formValues.status,
          );
        }

        // 处理时间过滤
        if (formValues.handleTime) {
          filteredRows = filteredRows.filter((row) =>
            row.handleTime.includes(formValues.handleTime),
          );
        }

        // 岗位过滤
        if (formValues.position) {
          filteredRows = filteredRows.filter(
            (row) => row.position === formValues.position,
          );
        }

        // 评论过滤
        if (formValues.comment) {
          filteredRows = filteredRows.filter(
            (row) => row.comment === formValues.comment,
          );
        }

        // 创建方式过滤
        if (formValues.createType) {
          filteredRows = filteredRows.filter(
            (row) => row.createType === formValues.createType,
          );
        }

        // 执行人过滤
        if (formValues.executor) {
          filteredRows = filteredRows.filter((row) =>
            row.executor.includes(formValues.executor),
          );
        }

        // 任务名称过滤
        if (formValues.taskName) {
          filteredRows = filteredRows.filter((row) =>
            row.taskName.includes(formValues.taskName),
          );
        }

        // 模拟分页
        const start = (page.currentPage - 1) * page.pageSize;
        const end = start + page.pageSize;
        const paginatedRows = filteredRows.slice(start, end);
        return {
          items: paginatedRows,
          total: filteredRows.length,
        };
      },
    },
  },
  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) {
  // 跳转到任务详情页面
  router.push(`/schedule/detail/${row.id}`);
}

// 打印任务
function printTask(row: any) {
  // 打印功能暂未实现
  console.log('打印任务:', row.id);
  ElMessage.info('打印功能开发中');
}
</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>
          <ElButton size="small" type="primary" plain @click="printTask(row)">
            打印
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>

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