import axios from 'axios';
import { paramsSerializer } from '@/utils/utils';
import {
  ExamListPageParam,
  ExamListPageRes,
  ExamItem,
  ExamUpdateParam,
  ExamStatDetailInfo,
  ExamGuideInfo,
} from './types/exam';

// 考试列表
export function getExamList(
  params: ExamListPageParam
): Promise<ExamListPageRes> {
  return axios.post('/api/admin/exam/query', params);
}
// 考试详情
export function getExamDetail(examId: number): Promise<ExamItem> {
  return axios.post('/api/admin/exam/find', { examId });
}

// 新增或编辑考试
export function updateExam(data: ExamUpdateParam): Promise<ExamItem> {
  const ndata = { ...data };
  if (ndata.sheetConfig) {
    ndata.sheetConfig = JSON.stringify(ndata.sheetConfig);
  }
  const url = ndata.id ? '/api/admin/exam/update' : '/api/admin/exam/save';
  return axios.post(url, ndata, {
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 获取考试统计详情信息
export function getExamStatisticInfo(
  examId: number
): Promise<ExamStatDetailInfo> {
  return axios.post('/api/admin/exam/detail', { examId });
}

// 结束考试
export function finishExam(ids: number[]): Promise<boolean> {
  return axios.post('/api/admin/exam/finish', paramsSerializer(ids));
}

// 获取考试导览信息
export function getExamGuideInfo(): Promise<ExamGuideInfo> {
  return axios.post('/api/admin/exam/overview', {});
}
