<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 './recordTaskTimeChanges-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,
      executor: '张三',
      position: '站长',
      station: '中石化北京朝阳站',
      taskName: '岗位责任制落实情况',
      originalPlanTime: '2023-06-15 09:00:00',
      modifiedPlanTime: '2023-06-15 14:00:00',
      operationTime: '2023-06-14 16:30:00',
    },
    {
      id: 2,
      executor: '王五',
      position: '副站长',
      station: '中石化上海浦东站',
      taskName: '安全生产检查',
      originalPlanTime: '2023-06-15 10:00:00',
      modifiedPlanTime: '2023-06-15 15:00:00',
      operationTime: '2023-06-14 17:45:00',
    },
    {
      id: 3,
      executor: '钱七',
      position: '加油员',
      station: '中石化广州天河站',
      taskName: '服务质量评价',
      originalPlanTime: '2023-06-15 11:00:00',
      modifiedPlanTime: '2023-06-15 16:00:00',
      operationTime: '2023-06-14 18:20:00',
    },
    {
      id: 4,
      executor: '周九',
      position: '收银员',
      station: '中石化深圳南山站',
      taskName: '收银系统维护',
      originalPlanTime: '2023-06-15 14:00:00',
      modifiedPlanTime: '2023-06-15 19:00:00',
      operationTime: '2023-06-14 19:10:00',
    },
    {
      id: 5,
      executor: '吴十',
      position: '安全员',
      station: '中石化杭州西湖站',
      taskName: '消防设备检查',
      originalPlanTime: '2023-06-15 08:00:00',
      modifiedPlanTime: '2023-06-15 13:00:00',
      operationTime: '2023-06-14 20:05:00',
    },
  ],
  total: 5,
};

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.includes(formValues.station));
        }
        if (formValues.operationTime && formValues.operationTime.length === 2) {
          const [startTime, endTime] = formValues.operationTime;
          filteredRows = filteredRows.filter(
            row => row.operationTime >= startTime && row.operationTime <= 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: 'system-checklis-visitorCheckTask-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>
