import { httpApp } from "@/plugins/axiosApp";
import { httpNoAuth } from "@/plugins/axiosNoAuth";

/** 用户登录 */
export async function loginApi(
  accountType: string,
  accountValue: string,
  password: string,
  domain: string,
  rootOrgId: number,
  geeParams: object,
  timestamp: number
) {
  return httpApp.post<{ code: string; desc: string; content: any }>(
    "/api/ecs_core/login",
    {
      accountType,
      accountValue,
      password,
      domain,
      rootOrgId,
      alwaysOK: true,
      ...geeParams,
    },
    { setGlobalMask: true, headers: { timestamp } }
  );
}

/** 用户登录后获取额外信息 */
export async function getStudentInfoBySessionApi() {
  return httpApp.get<any>("/api/ecs_core/student/getStudentInfoBySession", {
    setGlobalMask: true,
    noErrorMessage: true,
    "axios-retry": { retries: 3 },
  });
}

/** 用户登录后获取专业 */
export async function getStudentSpecialtyNameListApi() {
  return httpApp.get<string[]>(
    "/api/ecs_exam_work/exam_student/specialtyNameList/",
    { setGlobalMask: true, noErrorMessage: true, "axios-retry": { retries: 3 } }
  );
}

/** 获得云存储地址签名 */
export async function getCapturePhotoYunSign(signParams: object) {
  return httpApp.get<{
    formUrl: string;
    accessUrl: string;
    signIdentifier: string;
    formParams: object;
  }>("/api/ecs_oe_student/examControl/getCapturePhotoYunSign", {
    params: signParams,
  });
}

/** 保存图片到云存储 */
export async function saveCapturePhoto(
  formUrl: string,
  formParams: object,
  payload: object
) {
  const myFormData = new FormData();
  for (const [k, v] of Object.entries(formParams)) {
    myFormData.append(k, v as string);
  }
  for (const [k, v] of Object.entries(payload)) {
    myFormData.append(k, v as Blob);
  }

  return httpNoAuth.post(formUrl, myFormData);
}
