import { request } from "@/utils/request";
import { getFileMD5 } from "@/utils/crypto";
import { obj2formData } from "@/utils/tool";
import { ConfigInfo } from "./types/base";

export const addOrEditExam = (params: { id?: string | number; name: string }) =>
  request({
    url: "/api/admin/exam/save",
    params,
  });

export const getBaseDataConfig = (params: {
  examId: number;
}): Promise<ConfigInfo> =>
  request({
    url: "/api/admin/exam/config/info",
    params,
  });
export const saveBaseDataConfig = (data: {
  examId: number;
  paperTypeBarcodeContent: string[];
  imageCheckRatio: string | number;
  imageCheckOrder: string;
  enableSyncVerify: boolean;
  scannerAssignedMaxCount: string | number;
  scannerAssignedVerifyPassword: string;
}) =>
  request({
    url: "/api/admin/exam/config/save",
    data,
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
    },
  });

export const getUserList = (params: { role: string } & PageBaseParams) =>
  request({
    url: "/api/admin/user/page",
    params,
  });

export const toggleUserStatus = (params: { userId: number; enable: boolean }) =>
  request({
    url: "/api/admin/user/toggle",
    params,
    loading: true,
  });

export const addUser = (params: {
  role: string;
  loginName: string;
  password: string;
}) =>
  request({
    url: "/api/admin/user/save",
    params,
    loading: true,
  });

export const resetUserPwd = (params: { userId: number; password: string }) =>
  request({
    url: "/api/admin/user/password/reset",
    params,
    loading: true,
  });

export const getCardList = (params: { examId: number } & PageBaseParams) =>
  request({
    url: "/api/admin/card/page",
    params,
  });

export const importCard = async (params: {
  examId: any;
  subjectCode: string;
  remark?: string;
  file: any;
}) => {
  const md5 = await getFileMD5(params.file);
  const formData = obj2formData(params);
  return request({
    url: "/api/admin/card/import",
    data: formData,
    headers: {
      md5,
      "Content-Type": "multipart/form-data",
    },
  });
};

export const deleteCard = (params: { examId: number; number: number }) =>
  request({
    url: "/api/admin/card/delete",
    params,
    loading: true,
  });

export const getStuList = (params: { examId: number }) =>
  request({
    url: "/api/admin/student/count",
    params,
  });
export const getStuImportSet = (params: { examId: number }) =>
  request({
    url: "/api/admin/student/import/config",
    params,
  });

export const saveStuImportSet = (params: {
  examId: number;
  year: number | string;
  yearHalf: number | string;
}) =>
  request({
    url: "/api/admin/student/import/config/save",
    params,
  });

export const clearStuData = (params: { examId: number; subjectCode: string }) =>
  request({
    url: "/api/admin/student/clear",
    params,
  });

export const importStu = async (params: {
  examId: number;
  file: File | null;
}) => {
  const md5 = await getFileMD5(params.file as File);
  const formData = obj2formData(params);
  return request({
    url: "/api/admin/student/import",
    data: formData,
    headers: {
      md5,
      "Content-Type": "multipart/form-data",
    },
  });
};

export const exportStu = (params: { examId: any }) => {
  return request({
    url: "/api/admin/student/import/template",
    method: "get",
    params,
    download: true,
  });
};
