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

/** 清理复核任务 */
export async function clearInspectedTask(
  studentId?: string,
  subjectCode?: string
) {
  const form = new FormData();
  studentId && form.append("studentId", studentId);
  subjectCode && form.append("subjectCode", subjectCode);
  return httpApp.post<void>("/admin/exam/inspected/clear", form);
}

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

export interface InspectedParams {
  subjectCode: string;
  startScore?: string;
  endScore?: string;
  mainNumber?: string;
  mainStartScore?: string;
  mainEndScore?: string;
  questionScore?: string;
  selectiveStatus?: string;
  secretNumber?: string;
}

/** 批量复核得到单个学生的复核任务 */
export async function getOneOfInspectedTask({
  subjectCode = "",
  startScore = "",
  endScore = "",
  mainNumber = "",
  mainStartScore = "",
  mainEndScore = "",
  questionScore = "",
  selectiveStatus = "",
  secretNumber = "",
}: InspectedParams) {
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  form.append("startScore", startScore);
  form.append("endScore", endScore);
  form.append("mainNumber", mainNumber);
  form.append("mainStartScore", mainStartScore);
  form.append("mainEndScore", mainEndScore);
  form.append("questionScore", questionScore);
  form.append("selectiveStatus", selectiveStatus);
  form.append("secretNumber", secretNumber);
  return httpApp.post<Task>("/admin/exam/inspected/getTask", form);
}

/** 批量复核得到任务总数 */
export async function getInspectedTaskStatus({
  subjectCode = "",
  startScore = "",
  endScore = "",
  mainNumber = "",
  mainStartScore = "",
  mainEndScore = "",
  questionScore = "",
  selectiveStatus = "",
  secretNumber = "",
}: InspectedParams) {
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  form.append("startScore", startScore);
  form.append("endScore", endScore);
  form.append("mainNumber", mainNumber);
  form.append("mainStartScore", mainStartScore);
  form.append("mainEndScore", mainEndScore);
  form.append("questionScore", questionScore);
  form.append("selectiveStatus", selectiveStatus);
  form.append("secretNumber", secretNumber);
  return httpApp.post<{ valid: boolean; totalCount: number }>(
    "/admin/exam/inspected/getStatus",
    form
  );
}

/** 批量复核设置 */
export async function getAdminPageSetting(subjectCode?: string) {
  const form = new FormData();
  subjectCode && form.append("subjectCode", subjectCode);
  return httpApp.post<AdminPageSetting>(
    "/admin/exam/inspected/getSetting",
    form
  );
}

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

/** 保存复核任务 */
export async function saveInspectedTask(studentId: string) {
  const form = new FormData();
  form.append("studentId", studentId);
  return httpApp.post<CommonResponse>("/admin/exam/inspected/save", form);
}

/** 复核任务打回问题 */
export async function rejectInspectedTask(
  studentId: string,
  questionList: Array<Question>,
  reason: string
) {
  // questionList = questionList.map((item: any) => {
  //   if (Array.isArray(item.trackList)) {
  //     item.trackList = item.trackList.map((v: any) => {
  //       delete v.isByMultMark;
  //       return v;
  //     });
  //   }
  //   return item;
  // });
  questionList = JSON.parse(
    JSON.stringify(questionList, (key, value) => {
      const keys: string[] = ["markerId", "color", "isByMultMark"];
      // eslint-disable-next-line @typescript-eslint/no-unsafe-return
      return !key.startsWith("__") && !keys.includes(key) ? value : undefined;
    })
  );
  return httpApp.post<CommonResponse>("/admin/exam/inspected/rejected", {
    studentId,
    questionList,
    reason,
  });
}
