<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 './photoTask-data';

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

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

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


// 模拟访客检查任务数据
const mockData = {
  rows: [
    {
      id: '1',
      taskName: '访客检查任务1',
      station: '油站1',
      executor: '张三',
      position: 'manager',
      comment: 'commented',
      deadline: '2024-01-15 18:00:00',
      handleTime: '2024-01-15 17:30:00',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: '2',
      taskName: '访客检查任务2',
      station: '油站2',
      executor: '李四',
      position: 'clerk',
      comment: 'uncommented',
      deadline: '2024-01-16 18:00:00',
      handleTime: '2024-01-17 09:00:00',
      status: 'overdue',
      createType: 'manual',
    },
    {
      id: '3',
      taskName: '访客检查任务3',
      station: '油站3',
      executor: '王五',
      position: 'safety',
      comment: 'commented',
      deadline: '2024-01-17 18:00:00',
      handleTime: '2024-01-17 16:45:00',
      status: 'onTime',
      createType: 'system',
    },
    {
      id: '4',
      taskName: '访客检查任务4',
      station: '油站1',
      executor: '赵六',
      position: 'manager',
      comment: 'uncommented',
      deadline: '2024-01-18 18:00:00',
      handleTime: '2024-01-18 15:30:00',
      status: 'onTime',
      createType: 'manual',
    },
    {
      id: '5',
      taskName: '访客检查任务5',
      station: '油站2',
      executor: '孙七',
      position: 'clerk',
      comment: 'commented',
      deadline: '2024-01-19 18:00:00',
      handleTime: '2024-01-20 10:20:00',
      status: 'overdue',
      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请求
        console.log('查询参数:', formValues, page);
        // 模拟过滤
        let filteredRows = [...mockData.rows];

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

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

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

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

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

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

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

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

        // 任务名称过滤
        if (formValues.taskName) {
          filteredRows = filteredRows.filter((item) =>
            item.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-photoTask-index',
};

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

// 查看任务详情
function viewDetail(row: any) {
  // 跳转到任务详情页面
  router.push(`/schedule/detail/${row.id}`);
}

// 打印任务
function printTask(row: any) {
  // 打印功能（暂未实现）
  ElMessage.info('打印功能开发中');
}
</script>

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

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