// @ts-ignore: 忽略类型检查错误
import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { h } from 'vue';

const examStatusColorMap: any = {
  pending: {
    text: '待考试',
    color: 'orange',
  },
  ongoing: {
    text: '进行中',
    color: 'green',
  },
  absent: {
    text: '缺考',
    color: 'red',
  },
};

export const getStatus = (row: any) => {
  let status = '';
  if (new Date(row.examStartTime) > new Date()) {
    status = 'pending';
  } else if (new Date(row.examEndTime) > new Date()) {
    status = 'ongoing';
  } else {
    status = 'absent';
  }
  return status;
};

// 待考试表格列配置
export const pendingExamColumns: VxeGridProps['columns'] = [
  {
    title: '考试名称',
    field: 'examName',
    minWidth: 200,
    align: 'center',
    fixed: 'left',
  },
  {
    title: '考试开始时间',
    field: 'examStartTime',
    minWidth: 180,
    align: 'center',
  },
  {
    title: '考试结束时间',
    field: 'examEndTime',
    minWidth: 180,
    align: 'center',
  },
  {
    title: '状态',
    field: 'status',
    minWidth: 100,
    align: 'center',
    slots: {
      default: ({ row }) => {
        // 考试状态：待考试、进行中、缺考
        const status = getStatus(row);
        return h(
          'span',
          {
            style: {
              color: examStatusColorMap[status].color,
            },
          },
          examStatusColorMap[status].text,
        );
      },
    },
  },
  { title: '及格分数', field: 'passScore', minWidth: 100, align: 'center' },
  { title: '满分', field: 'totalScore', minWidth: 100, align: 'center' },
  {
    title: '考试时间长(分钟)',
    field: 'examDuration',
    minWidth: 120,
    align: 'center',
  },
  {
    title: '操作',
    field: 'action',
    minWidth: 120,
    align: 'center',
    fixed: 'right',
    slots: {
      default: 'actionSlot',
    },
  },
];

// 已考试表格列配置
export const completedExamColumns: VxeGridProps['columns'] = [
  {
    title: '考试名称',
    field: 'examName',
    minWidth: 200,
    align: 'center',
    fixed: 'left',
  },
  {
    title: '考试开始时间',
    field: 'examStartTime',
    minWidth: 180,
    align: 'center',
  },
  {
    title: '考试结束时间',
    field: 'examEndTime',
    minWidth: 180,
    align: 'center',
  },
  {
    title: '开始答题时间',
    field: 'startAnswerTime',
    minWidth: 180,
    align: 'center',
  },
  {
    title: '答题时长(分钟)',
    field: 'duration',
    minWidth: 120,
    align: 'center',
    formatter: ({ row }) => {
      // 计算两个时间差
      const startTime = new Date(row.startAnswerTime);
      const endTime = new Date(row.submitAnswerTime);
      const duration = (endTime.getTime() - startTime.getTime()) / 60_000;
      return duration.toFixed(2) || '-';
    },
  },
  { title: '得分', field: 'examScore', minWidth: 100, align: 'center' },
  {
    title: '是否及格',
    field: 'isPass',
    minWidth: 100,
    align: 'center',
    slots: {
      default: ({ row }) => {
        // 得分大于总分及格, 及格绿色，不及格红色
        return h(
          'span',
          {
            style: {
              color: row.examScore >= row.totalScore ? 'green' : 'red',
            },
          },
          row.examScore >= row.totalScore ? '是' : '否',
        );
      },
    },
  },
  { title: '批改人', field: 'correctorName', minWidth: 120, align: 'center' },
  {
    title: '操作',
    field: 'action',
    minWidth: 120,
    align: 'center',
    fixed: 'right',
    slots: {
      default: 'actionSlot',
    },
  },
];

// 搜索表单配置 (待考试和已考试共用)
export const examQueryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'examName',
    label: '考试内容',
    componentProps: {
      placeholder: '请输入考试名称',
    },
  },
];
