<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { queryFormSchema, tableColumns } from './visitorDetails-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 mockData = {
  rows: [
    {
      id: 1,
      area: '片区1',
      station: '油站1',
      name: '访客检查任务1',
      type: '常规检查',
      score: 85,
      percentageScore: 85,
      exceptionCount: 2,
      time: '2024-05-01 10:30:00',
      executor: '张三',
      position: '营业员',
    },
    {
      id: 2,
      area: '片区2',
      station: '油站2',
      name: '安全检查任务1',
      type: '专项检查',
      score: 92,
      percentageScore: 92,
      exceptionCount: 1,
      time: '2024-05-02 14:45:00',
      executor: '李四',
      position: '安全员',
    },
    {
      id: 3,
      area: '片区1',
      station: '油站3',
      name: '服务质量检查1',
      type: '常规检查',
      score: 78,
      percentageScore: 78,
      exceptionCount: 3,
      time: '2024-05-03 09:15:00',
      executor: '王五',
      position: '服务专员',
    },
    {
      id: 4,
      area: '片区3',
      station: '油站4',
      name: '设备维护检查1',
      type: '专项检查',
      score: 95,
      percentageScore: 95,
      exceptionCount: 0,
      time: '2024-05-04 16:20:00',
      executor: '赵六',
      position: '设备管理员',
    },
    {
      id: 5,
      area: '片区2',
      station: '油站5',
      name: '访客检查任务2',
      type: '常规检查',
      score: 88,
      percentageScore: 88,
      exceptionCount: 2,
      time: '2024-05-05 11:50:00',
      executor: '孙七',
      position: '营业员',
    },
    {
      id: 6,
      area: '片区4',
      station: '油站1',
      name: '安全检查任务2',
      type: '专项检查',
      score: 90,
      percentageScore: 90,
      exceptionCount: 1,
      time: '2024-05-06 15:10:00',
      executor: '周八',
      position: '安全员',
    },
    {
      id: 7,
      area: '片区3',
      station: '油站2',
      name: '服务质量检查2',
      type: '常规检查',
      score: 82,
      percentageScore: 82,
      exceptionCount: 2,
      time: '2024-05-07 10:00:00',
      executor: '吴九',
      position: '服务专员',
    },
    {
      id: 8,
      area: '片区5',
      station: '油站3',
      name: '设备维护检查2',
      type: '专项检查',
      score: 94,
      percentageScore: 94,
      exceptionCount: 0,
      time: '2024-05-08 14:30:00',
      executor: '郑十',
      position: '设备管理员',
    },
    {
      id: 9,
      area: '片区4',
      station: '油站4',
      name: '访客检查任务3',
      type: '常规检查',
      score: 87,
      percentageScore: 87,
      exceptionCount: 1,
      time: '2024-05-09 09:45:00',
      executor: '张三',
      position: '营业员',
    },
    {
      id: 10,
      area: '片区5',
      station: '油站5',
      name: '安全检查任务3',
      type: '专项检查',
      score: 91,
      percentageScore: 91,
      exceptionCount: 1,
      time: '2024-05-10 16:00:00',
      executor: '李四',
      position: '安全员',
    },
  ],
  total: 10,
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 模拟数据过滤和分页
        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.timeRange && formValues.timeRange.length === 2) {
          const [startTime, endTime] = formValues.timeRange;
          filteredRows = filteredRows.filter(
            row => row.time >= startTime && row.time <= endTime
          );
        }
        if (formValues.executor) {
          filteredRows = filteredRows.filter(row => row.executor.includes(formValues.executor));
        }

        // 模拟分页
        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,
    buttons: [
      {
        code: 'query',
        name: '查询',
        icon: 'Search',
        status: 'primary',
      },
      {
        code: 'reset',
        name: '重置',
        icon: 'Refresh',
      },
    ],
    onClick: (code: string) => {
      if (code === 'query') {
        // 执行查询操作
        basicTableApi.refresh();
      } else if (code === 'reset') {
        // 执行重置操作
        basicTableApi.resetForm();
      }
    },
  },
  id: 'system-checklis-visitorCheckTask-index',
};

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

// 查看任务详情
function handleView(row: any) {
  console.log('查看详情:', row);
}

// 导出数据
function handleExport() {
  // 这里可以添加实际的导出逻辑
  console.log('导出数据');
}


</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="访客打分明细列表">
      <template #toolbar-actions>
        <div class="text-sm text-gray-600 bg-gray-50 p-2 rounded-md ml-2">
          提示：请输入搜索条件后点击查询按钮获取数据，或点击重置按钮清空所有搜索条件。
        </div>
      </template>
      <template #toolbar-tools>
        <ElSpace>
          <ElButton
            @click="handleExport"
            v-access:code="['system:taskScoringRecord:export']"
          >
            导出
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>
