<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, useVbenDrawer } from '@vben/common-ui';

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

// 模拟API，后续需要替换为实际API
import { queryFormSchema, tableColumns } from './questScoring-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: [
    [
      'createTime',
      ['params[beginCreateTime]', 'params[endCreateTime]'],
      ['YYYY-MM-DD', 'YYYY-MM-DD'],
    ],
    [
      'endTime',
      ['params[beginEndTime]', 'params[endEndTime]'],
      ['YYYY-MM-DD', 'YYYY-MM-DD'],
    ],
  ],
};

// 表格配置
const gridOptions: VxeGridProps = {
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 模拟API调用，后续替换为实际API
        const mockData = {
          rows: Array.from({ length: 15 }, (_, index) => ({
            id: index + 1,
            examPaperName: `考卷名称${index + 1}`,
            createTime: '2025-11-11',
            endTime: '2025-11-12',
            totalNum: 100,
            participateNum: 98,
            markedNum: 90,
            unmarkedNum: 8,
          })),
          total: 15,
        };
        return { items: mockData.rows, total: mockData.total };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'examManage-questScoring-index',
};

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

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

// 批改
function handleCorrect(row: any) {
  router.push(`/questScoring/view/${row.id}?mode=correct`);
}
</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="success"
            plain
            @click="handleCorrect(row)"
          >
            批改
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
  <router-view v-if="$route.path.includes('/view/') || $route.path.includes('/correct/')" />
  </div>
</template>

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