import axios, { AxiosResponse } from 'axios';
import {
  SubjectTotalScoreStatItem,
  SubjectListPageParam,
  SubjectListPageRes,
  SubjectSettingInfo,
  PaperStructureListPageRes,
  PaperStructureItem,
  PaperStructureUpdateParams,
  SubjectItem,
  OptionalQuestionItem,
  OptionalRuleUpdateParam,
  OptionalRuleItem,
} from './types/subject';

// 科目管理
// 获取科目总分统计列表
export function getSubjectTotalScoreStatList(): Promise<
  SubjectTotalScoreStatItem[]
> {
  return axios.post(`/api/admin/subject/statistic`);
}

// 获取科目列表
export function getSubjectList(
  params: SubjectListPageParam
): Promise<SubjectListPageRes> {
  return axios.post('/api/admin/subject/query', {}, { params });
}
// 获取科目详情
export function getSubjectDetail(subjectCode: string): Promise<SubjectItem> {
  return axios.post(
    '/api/admin/subject/find',
    {},
    { params: { code: subjectCode } }
  );
}

// 科目分析计算
export function subjectAnalysis(subjectCode?: string): Promise<boolean> {
  return axios.post('/api/admin/subject/report', {
    params: { subjectCode },
  });
}

// 客观题统分
export function subjectObjectiveMarkScore(
  subjectCode?: string
): Promise<boolean> {
  return axios.post(
    '/api/admin/subject/calculate',
    {},
    {
      params: { subjectCode },
    }
  );
}

// 保存科目设置信息
export function saveSubjectSetting(
  data: SubjectSettingInfo
): Promise<SubjectSettingInfo> {
  return axios.post('/api/admin/subject/update', data);
}

// 导出主客观题
export function exportSubjectMainObjective(
  objective: boolean
): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/subject/export',
    {},
    {
      responseType: 'blob',
      params: { objective },
    }
  );
}

// 下载主客观题导入模版
export function downloadSubjectObjectiveImportTemplate(params: {
  objective: boolean;
}): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/subject/template',
    {},
    {
      responseType: 'blob',
      params,
    }
  );
}

// 试卷结构 -------------->
// 获取科目试卷结构列表
export function getPaperStructureList(
  subjectCode: string
): Promise<PaperStructureListPageRes> {
  return axios.post(
    `/api/admin/question/list`,
    {},
    { params: { subjectCode } }
  );
}

// 新增/编辑试卷结构
export function savePaperStructure(
  data: PaperStructureUpdateParams
): Promise<PaperStructureItem> {
  if (data.id) {
    return axios.post('/api/admin/question/update', data);
  }
  return axios.post('/api/admin/question/save', data);
}

// 批量删除试卷结构
export function deletePaperStructures(ids: number[]): Promise<boolean> {
  return axios.post(
    '/api/admin/question/delete',
    {},
    {
      params: { ids },
    }
  );
}

// 选做题设置  ---------------->
// 获取选做题试题列表
export function getOptionalQuestionList(
  subjectCode: string
): Promise<OptionalQuestionItem[]> {
  return axios.post(
    '/api/subject/optional-question/list',
    {},
    {
      params: { subjectCode },
    }
  );
}

// 获取选做题规则列表
export function getOptionalQuestionRuleList(
  subjectCode: string
): Promise<OptionalRuleItem[]> {
  return axios.post(
    '/api/subject/optional-question/rule/list',
    {},
    {
      params: { subjectCode },
    }
  );
}

// 保存选做题设置
export function saveOptionalQuestionRule(
  params: OptionalRuleUpdateParam
): Promise<boolean> {
  return axios.post('/api/subject/optional-question/rule', params);
}

// 删除选做题规则
export function deleteOptionalQuestionRule(id: number): Promise<boolean> {
  return axios.post(
    '/api/subject/optional-question/rule/delete',
    {},
    {
      params: { id },
    }
  );
}
