<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 './checkBack-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,
      checkedPerson: '张三',
      checkedPersonPosition: '营业员',
      station: '油站1',
      taskName: '访客检查任务1',
      taskProcessingTime: '2024-05-01',
      checkPerson: '李四',
      checkPersonPosition: '站长',
      checkTime: '2024-05-06',
      intervalDays: 5,
    },
    {
      id: 2,
      checkedPerson: '王五',
      checkedPersonPosition: '安全员',
      station: '油站2',
      taskName: '安全检查任务1',
      taskProcessingTime: '2024-05-02',
      checkPerson: '赵六',
      checkPersonPosition: '主管',
      checkTime: '2024-05-05',
      intervalDays: 3,
    },
    {
      id: 3,
      checkedPerson: '孙七',
      checkedPersonPosition: '站长',
      station: '油站3',
      taskName: '管理检查任务1',
      taskProcessingTime: '2024-05-03',
      checkPerson: '周八',
      checkPersonPosition: '区域经理',
      checkTime: '2024-05-10',
      intervalDays: 7,
    },
    {
      id: 4,
      checkedPerson: '吴九',
      checkedPersonPosition: '营业员',
      station: '油站1',
      taskName: '服务检查任务1',
      taskProcessingTime: '2024-05-04',
      checkPerson: '郑十',
      checkPersonPosition: '主管',
      checkTime: '2024-05-06',
      intervalDays: 2,
    },
    {
      id: 5,
      checkedPerson: '张三',
      checkedPersonPosition: '营业员',
      station: '油站2',
      taskName: '访客检查任务2',
      taskProcessingTime: '2024-05-05',
      checkPerson: '李四',
      checkPersonPosition: '站长',
      checkTime: '2024-05-09',
      intervalDays: 4,
    },
    {
      id: 6,
      checkedPerson: '王五',
      checkedPersonPosition: '安全员',
      station: '油站3',
      taskName: '安全检查任务2',
      taskProcessingTime: '2024-05-06',
      checkPerson: '赵六',
      checkPersonPosition: '主管',
      checkTime: '2024-05-12',
      intervalDays: 6,
    },
    {
      id: 7,
      checkedPerson: '孙七',
      checkedPersonPosition: '站长',
      station: '油站4',
      taskName: '管理检查任务2',
      taskProcessingTime: '2024-05-07',
      checkPerson: '周八',
      checkPersonPosition: '区域经理',
      checkTime: '2024-05-08',
      intervalDays: 1,
    },
    {
      id: 8,
      checkedPerson: '吴九',
      checkedPersonPosition: '营业员',
      station: '油站5',
      taskName: '服务检查任务2',
      taskProcessingTime: '2024-05-08',
      checkPerson: '郑十',
      checkPersonPosition: '主管',
      checkTime: '2024-05-11',
      intervalDays: 3,
    },
    {
      id: 9,
      checkedPerson: '钱一',
      checkedPersonPosition: '主管',
      station: '油站1',
      taskName: '设备检查任务1',
      taskProcessingTime: '2024-05-09',
      checkPerson: '张三',
      checkPersonPosition: '站长',
      checkTime: '2024-05-15',
      intervalDays: 6,
    },
    {
      id: 10,
      checkedPerson: '孙二',
      checkedPersonPosition: '营业员',
      station: '油站2',
      taskName: '库存检查任务1',
      taskProcessingTime: '2024-05-10',
      checkPerson: '李四',
      checkPersonPosition: '主管',
      checkTime: '2024-05-14',
      intervalDays: 4,
    },
    {
      id: 11,
      checkedPerson: '周三',
      checkedPersonPosition: '安全员',
      station: '油站3',
      taskName: '消防检查任务1',
      taskProcessingTime: '2024-05-11',
      checkPerson: '王五',
      checkPersonPosition: '区域经理',
      checkTime: '2024-05-18',
      intervalDays: 7,
    },
    {
      id: 12,
      checkedPerson: '吴四',
      checkedPersonPosition: '站长',
      station: '油站4',
      taskName: '环境检查任务1',
      taskProcessingTime: '2024-05-12',
      checkPerson: '赵六',
      checkPersonPosition: '主管',
      checkTime: '2024-05-15',
      intervalDays: 3,
    },
  ],
  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.taskName) {
          filteredRows = filteredRows.filter(row => row.taskName.includes(formValues.taskName));
        }
        if (formValues.taskProcessingTime && formValues.taskProcessingTime.length === 2) {
          const [startTime, endTime] = formValues.taskProcessingTime;
          filteredRows = filteredRows.filter(
            row => row.taskProcessingTime >= startTime && row.taskProcessingTime <= endTime
          );
        }
        if (formValues.checkPerson) {
          filteredRows = filteredRows.filter(row => row.checkPerson.includes(formValues.checkPerson));
        }
        if (formValues.checkPersonPosition) {
          filteredRows = filteredRows.filter(row => row.checkPersonPosition === formValues.checkPersonPosition);
        }
        if (formValues.checkedPerson) {
          filteredRows = filteredRows.filter(row => row.checkedPerson.includes(formValues.checkedPerson));
        }
        if (formValues.checkedPersonPosition) {
          filteredRows = filteredRows.filter(row => row.checkedPersonPosition === formValues.checkedPersonPosition);
        }

        // 模拟分页
        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: 'check-back-record-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>

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