// @ts-ignore
import { LOGIN_CONFIG } from "@/devLoginParams";
import { getBase64 } from "./utils/crypto";
import { httpApp } from "@/plugins/axiosApp";
import vls from "./utils/storage";

const { loginName, password, schoolCode, studentIds, mark, arbitrate } =
  LOGIN_CONFIG;

export async function initLogin() {
  const res = await httpApp.post("/api/admin/common/login", {
    loginName,
    password: getBase64(password),
    schoolCode,
    type: "ACCOUNT",
  });
  const data = res.data;

  if (data.orgInfo) vls.set("orgId", data.orgInfo.id);
  if (data.schoolInfo && data.schoolInfo.length) {
    const curSchool = data.schoolInfo.find((item) => item.code === schoolCode);
    if (curSchool) {
      vls.set("schoolId", curSchool.id);
      vls.set("schoolName", curSchool.name);
    }
  }
  vls.set("user", data);
  vls.set("token", data.accessToken);

  studentIds
    ? vls.set("check-students", studentIds)
    : vls.remove("check-students");
  mark ? vls.set("mark", mark) : vls.remove("mark");
  arbitrate ? vls.set("arbitrate", arbitrate) : vls.remove("arbitrate");
}
