<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { ref } from 'vue';
import { useRouter } from 'vue-router';

import { Page } from '@vben/common-ui';

import { useVbenVxeGrid } from '#/adapter/vxe-table';

// 模拟API，后续需要替换为实际API
import { queryFormSchema, tableColumns } from './scoreManage-data';

// 路由实例
const router = useRouter();

// 搜索表单配置
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: queryFormSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
  fieldMappingTime: [
    [
      'examTime',
      ['params[beginExamTime]', 'params[endExamTime]'],
      ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm:ss'],
    ],
  ],
};

// 表格配置
const gridOptions: VxeGridProps = {
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 模拟API调用，后续替换为实际API
        const departments = ['tech', 'finance', 'hr', 'marketing', 'sales'];
        const examNames = ['安全知识考试', '业务能力测试', '产品知识考核', '团队协作培训', '新员工入职考试'];
        
        const mockData = {
          rows: Array.from({ length: 37 }, (_, index) => {
            const totalScore = 100;
            const userScore = Math.floor(Math.random() * 101);
            const isPass = userScore >= 60 ? '是' : '否';
            const department = departments[index % departments.length];
            const examName = examNames[index % examNames.length];
            const answerDuration = Math.floor(Math.random() * 120) + 30; // 30-150分钟
            
            return {
              id: index + 1,
              userName: `用户${index + 1}`,
              department: department,
              examName: examName,
              totalScore: totalScore,
              userScore: userScore,
              isPass: isPass,
              answerStartTime: `2025-01-${String(index + 1).padStart(2, '0')} 09:00:00`,
              answerEndTime: `2025-01-${String(index + 1).padStart(2, '0')} ${String(9 + Math.floor((30 + answerDuration) / 60)).padStart(2, '0')}:${String((30 + answerDuration) % 60).padStart(2, '0')}:00`,
              answerDuration: answerDuration,
              examStartTime: `2025-01-${String(index + 1).padStart(2, '0')} 09:00:00`,
            };
          }),
          total: 37,
        };
        return { items: mockData.rows, total: mockData.total };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
    export: true,
  },
  id: 'examManage-scoreManage-index',
};

// 初始化表格
const [BasicTable, basicTableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

// 查看详情
function handleView(row: any) {
  router.push(`/examManage/scoreManage/examApproved/${row.id}`);
}

// 删除
function handleDelete(row: any) {
  // 这里可以添加删除确认逻辑
  console.log('删除记录:', row.id);
  // 模拟删除操作
  basicTableApi.refresh();
}
</script>

<template>
  <div>
  <Page :auto-content-height="true">
    <BasicTable table-title="成绩管理">
      <template #action="{ row }">
        <ElSpace>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleView(row)"
          >
            查看
          </ElButton>
          <ElButton
            size="small"
            type="danger"
            plain
            @click="handleDelete(row)"
          >
            删除
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
  </div>
</template>

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