<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 './tagScoreRecord-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,
      station: '油站1',
      processingTime: '2024-05-01',
      taskName: '访客检查任务1',
      tag: '服务态度',
      tagTotalScore: 95,
      exceptionCount: 0,
      noPhotoCount: 1,
    },
    {
      id: 2,
      station: '油站2',
      processingTime: '2024-05-02',
      taskName: '安全检查任务1',
      tag: '安全管理',
      tagTotalScore: 88,
      exceptionCount: 2,
      noPhotoCount: 0,
    },
    {
      id: 3,
      station: '油站3',
      processingTime: '2024-05-03',
      taskName: '管理检查任务1',
      tag: '设备维护',
      tagTotalScore: 92,
      exceptionCount: 1,
      noPhotoCount: 2,
    },
    {
      id: 4,
      station: '油站1',
      processingTime: '2024-05-04',
      taskName: '服务检查任务1',
      tag: '服务态度',
      tagTotalScore: 90,
      exceptionCount: 0,
      noPhotoCount: 0,
    },
    {
      id: 5,
      station: '油站2',
      processingTime: '2024-05-05',
      taskName: '库存检查任务1',
      tag: '库存管理',
      tagTotalScore: 93,
      exceptionCount: 1,
      noPhotoCount: 1,
    },
    {
      id: 6,
      station: '油站3',
      processingTime: '2024-05-06',
      taskName: '环境检查任务1',
      tag: '环境整洁',
      tagTotalScore: 85,
      exceptionCount: 3,
      noPhotoCount: 0,
    },
    {
      id: 7,
      station: '油站4',
      processingTime: '2024-05-07',
      taskName: '设备检查任务1',
      tag: '设备维护',
      tagTotalScore: 96,
      exceptionCount: 0,
      noPhotoCount: 0,
    },
    {
      id: 8,
      station: '油站5',
      processingTime: '2024-05-08',
      taskName: '安全检查任务2',
      tag: '安全管理',
      tagTotalScore: 89,
      exceptionCount: 1,
      noPhotoCount: 2,
    },
    {
      id: 9,
      station: '油站1',
      processingTime: '2024-05-09',
      taskName: '库存检查任务2',
      tag: '库存管理',
      tagTotalScore: 91,
      exceptionCount: 0,
      noPhotoCount: 1,
    },
    {
      id: 10,
      station: '油站2',
      processingTime: '2024-05-10',
      taskName: '环境检查任务2',
      tag: '环境整洁',
      tagTotalScore: 87,
      exceptionCount: 2,
      noPhotoCount: 0,
    },
    {
      id: 11,
      station: '油站3',
      processingTime: '2024-05-11',
      taskName: '服务检查任务2',
      tag: '服务态度',
      tagTotalScore: 94,
      exceptionCount: 0,
      noPhotoCount: 1,
    },
    {
      id: 12,
      station: '油站4',
      processingTime: '2024-05-12',
      taskName: '设备检查任务2',
      tag: '设备维护',
      tagTotalScore: 90,
      exceptionCount: 1,
      noPhotoCount: 1,
    },
  ],
  total: 12,
};

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.station) {
          filteredRows = filteredRows.filter(row => row.station === formValues.station);
        }
        if (formValues.tag) {
          filteredRows = filteredRows.filter(row => row.tag === formValues.tag);
        }
        if (formValues.taskName) {
          filteredRows = filteredRows.filter(row => row.taskName.includes(formValues.taskName));
        }
        if (formValues.processingTime && formValues.processingTime.length === 2) {
          const [startTime, endTime] = formValues.processingTime;
          filteredRows = filteredRows.filter(
            row => row.processingTime >= startTime && row.processingTime <= endTime
          );
        }

        // 模拟分页
        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: 'tag-score-record-index',
};

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

// 导出数据
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>

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