import { request } from "@/utils/request";
import { ExamParams } from "./types/common";
import {
  SubjectItem,
  ExamListParams,
  ExamListResult,
  UploadSheetParams,
  UploadSliceParams,
  UploadFileResult,
  CardInfoParams,
  CardInfoResult,
} from "./types/base";

export const getExamList = (data: ExamListParams): Promise<ExamListResult> =>
  request({
    url: "/api/admin/exam/list",
    data,
  });

export const getSubjectList = (data: ExamParams): Promise<SubjectItem[]> =>
  request({
    url: "/api/admin/subject/list",
    method: "post",
    data,
  });

// 原图上传
export const updateSheet = (
  data: UploadSheetParams
): Promise<UploadFileResult> => {
  const formData = new FormData();
  for (const key in data) {
    const propKey = key as keyof UploadSheetParams;
    if (Object.prototype.hasOwnProperty.call(data, propKey)) {
      const val = data[propKey];
      formData.append(propKey, val instanceof Blob ? val : `${val}`);
    }
  }
  return request({
    url: "/api/admin/scan/answer/sheet/update",
    method: "post",
    data: formData,
  });
};
// 裁切图上传
export const updateSlice = (
  data: UploadSliceParams
): Promise<UploadFileResult> => {
  const formData = new FormData();
  for (const key in data) {
    const propKey = key as keyof UploadSliceParams;
    if (Object.prototype.hasOwnProperty.call(data, propKey)) {
      const val = data[propKey];
      formData.append(propKey, val instanceof Blob ? val : `${val}`);
    }
  }
  return request({
    url: "/api/admin/scan/answer/slice/update",
    method: "post",
    data: formData,
  });
};

// 裁切图修改
export const uploadSlice = (
  data: UploadSliceParams
): Promise<UploadFileResult> => {
  const formData = new FormData();
  for (const key in data) {
    const propKey = key as keyof UploadSliceParams;
    if (Object.prototype.hasOwnProperty.call(data, propKey)) {
      const val = data[propKey];
      formData.append(propKey, val instanceof Blob ? val : `${val}`);
    }
  }
  return request({
    url: "/api/admin/scan/answer/slice/upload",
    method: "post",
    data: formData,
  });
};

// 获取卡格式
export const cardInfo = (data: CardInfoParams): Promise<CardInfoResult> => {
  return request({
    url: "/api/card/info",
    method: "post",
    data,
  });
};

// 证号填涂位数获取
export const examNumberFillCount = (examId: Number): Promise<number> => {
  return request({
    url: "/api/admin/exam/exam-number/fill-count",
    method: "post",
    data: { examId },
  });
};
