import { httpApp } from "@/plugins/axiosApp";
import {
  CommonResponse,
  HistoryQueryParams,
  MarkDetail,
  Setting,
  Task,
} from "@/types";

/** 清理仲裁任务(libraryId 与其他参数互斥填写) */
export async function clearArbitrateTask(
  // 这里libraryId为string，是因为它也会从query里面取值，同时利用为null的情况不传值到后台
  libraryId?: string,
  subjectCode?: string,
  groupNumber?: string
) {
  const form = new FormData();
  libraryId && form.append("groupNumber", libraryId);
  subjectCode && form.append("subjectCode", subjectCode);
  groupNumber && form.append("groupNumber", groupNumber);
  return httpApp.post<void>("/admin/exam/arbitrate/clear", form);
}

/** 查看单个学生的仲裁任务 */
export async function getSingleArbitrateTask(historyId: string) {
  const form = new FormData();
  historyId && form.append("historyId", historyId);
  return httpApp.post<Task>("/admin/exam/arbitrate/getTask", form);
}

/** 查看仲裁任务2次分数 */
export async function getArbitrateList(historyId: string) {
  const form = new FormData();
  historyId && form.append("historyId", historyId);
  return httpApp.post<MarkDetail[]>(
    "/admin/exam/arbitrate/getArbitrationList",
    form
  );
}

/** 批量仲裁得到单个学生的仲裁任务 */
export async function getOneOfArbitrateTask(
  subjectCode: string,
  groupNumber: string
) {
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  form.append("groupNumber", groupNumber);
  return httpApp.post<Task>("/admin/exam/arbitrate/getTask", form, {
    setGlobalMask: true,
  });
}

/** 批量仲裁得到任务总数 */
export async function getArbitrateTaskStatus(
  subjectCode: string,
  groupNumber: string
) {
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  form.append("groupNumber", groupNumber);
  return httpApp.post<{
    valid: boolean;
    totalCount: number;
    markedCount: number;
  }>("/admin/exam/arbitrate/getStatus", form);
}

/** 批量仲裁设置 */
export async function getArbitrateSetting(
  historyId: string,
  subjectCode?: string,
  groupNumber?: string
) {
  const form = new FormData();
  historyId && form.append("historyId", historyId);
  subjectCode && form.append("subjectCode", subjectCode);
  groupNumber && form.append("groupNumber", groupNumber);
  return httpApp.post<Setting>("/admin/exam/arbitrate/getSetting", form);
}

/** 批量仲裁历史 */
export async function getArbitrateHistory({
  subjectCode,
  groupNumber,
  pageNumber = 1,
  pageSize = 20,
  order = "markerTime",
  sort = "DESC",
  secretNumber = null,
}: HistoryQueryParams) {
  if (!subjectCode) return;
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  groupNumber && form.append("groupNumber", groupNumber);
  form.append("pageNumber", pageNumber + "");
  form.append("pageSize", pageSize + "");
  form.append("order", order);
  form.append("sort", sort);
  secretNumber && form.append("secretNumber", secretNumber);
  return httpApp.post<Task[]>("/admin/exam/arbitrate/getHistory", form);
}

/** 保存仲裁任务 */
export async function saveArbitrateTask(
  libraryId: string,
  studentId: string,
  markerScore: number,
  scoreList: Array<number>,
  unselective: boolean,
  trackList?: any,
  specialTagList?: any
) {
  return httpApp.post<CommonResponse>("/admin/exam/arbitrate/saveTask", {
    libraryId,
    studentId,
    markerScore,
    scoreList,
    unselective,
    trackList,
    specialTagList,
  });
}
