<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 { useVbenVxeGrid } from '#/adapter/vxe-table';

import { queryFormSchema, tableColumns } from './visitorCheckTask-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',
      exceptionCount: 5,
      noPhotoCount: 2,
      executor: '张三',
      position: 'manager',
      comment: 'commented',
      deadline: '2024-05-01',
      handleTime: '2024-04-30',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: 2,
      taskName: '访客检查任务2',
      station: '油站2',
      exceptionCount: 3,
      noPhotoCount: 1,
      executor: '李四',
      position: 'clerk',
      comment: 'uncommented',
      deadline: '2024-05-02',
      handleTime: '2024-05-03',
      status: 'overdue',
      createType: 'manual',
    },
    {
      id: 3,
      taskName: '访客检查任务3',
      station: '油站3',
      exceptionCount: 0,
      noPhotoCount: 0,
      executor: '王五',
      position: 'safety',
      comment: 'commented',
      deadline: '2024-05-03',
      handleTime: '2024-05-03',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: 4,
      taskName: '访客检查任务4',
      station: '油站1',
      exceptionCount: 2,
      noPhotoCount: 0,
      executor: '赵六',
      position: 'manager',
      comment: 'uncommented',
      deadline: '2024-05-04',
      handleTime: '2024-05-05',
      status: 'overdue',
      createType: 'manual',
    },
    {
      id: 5,
      taskName: '访客检查任务5',
      station: '油站2',
      exceptionCount: 7,
      noPhotoCount: 3,
      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请求 - 这里需要根据页面类型调用不同的接口
        // 注意：该页面(visitorCheckTask)应调用与casualInspection不同的接口
        // visitorCheckTask 页面接口示例：/api/visitorFeedback/visitorCheckTask/list
        // casualInspection 页面接口示例：/api/visitorFeedback/casualInspection/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 === 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-visitorCheckTask-index',
};

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

// 查看任务详情
function handleView(row: any) {
  router.push(`/schedule/detail/${row.id}`);
}

// 打印任务
function handlePrint(row: any) {
  // 打印功能暂未实现
  console.log('打印任务:', row.id);
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="检查任务列表">
      <template #action="{ row }">
        <ElSpace>
          <ElButton size="small" type="primary" plain @click="handleView(row)">
            查看
          </ElButton>
          <ElButton size="small" type="default" plain @click="handlePrint(row)">
            打印
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>

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