import { httpApp } from "@/plugins/axiosIndex";
import { pickBy } from "lodash-es";
import { object2QueryString, AESString } from "@/utils/utils";

export function searchStudents({
  enable,
  name = "",
  identity = "",
  pageNumber = 1,
  pageSize = 10,
}) {
  const data = pickBy(
    { identity, name, enable, pageNumber, pageSize },
    (v) => v !== ""
  );
  return httpApp.post("/api/admin/student/query?" + object2QueryString(data));
}

export function saveStudent({
  orgId = "",
  id = "",
  roleCode,
  loginName = "",
  name = "",
  enable = "",
  password = "",
  mobileNumber = "",
}) {
  const data = pickBy(
    { orgId, id, roleCode, loginName, name, enable, password, mobileNumber },
    (v) => v !== ""
  );
  return httpApp.post("/api/admin/student/save", {
    ...data,
    ...(password.length > 0 ? { password: AESString(password) } : {}),
  });
}

export function toggleEnableStudent({ id, enable }) {
  return httpApp.post("/api/admin/student/toggle", { id, enable });
}

export function resetStudentPassword({ id, password }) {
  return httpApp.post("/api/admin/student/updatePwd", {
    id,
    password: AESString(password),
  });
}

export function searchStudentExamRecord({
  studentId = "",
  examId = "",
  pageNumber = 1,
  pageSize = 10,
}) {
  const data = pickBy(
    { studentId, examId, pageNumber, pageSize },
    (v) => v !== ""
  );
  return httpApp.post(
    "/api/admin/student/exam_record/query?" + object2QueryString(data)
  );
}
