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

export function searchStudents({
  enable,
  photo,
  name = "",
  identity = "",
  pageNumber = 1,
  pageSize = 10,
}) {
  const data = pickBy(
    { identity, name, photo, 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: encodePassword(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: encodePassword(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)
  );
}

// student-exam-detail
export function searchStudentExamDetailList({
  examId = "",
  activityId = "",
  roomCode = "",
  courseCode = "",
  name = "",
  identity = "",
  finishType = "",
  pageNumber = 1,
  pageSize = 10,
}) {
  const data = pickBy(
    {
      activityId,
      examId,
      roomCode,
      courseCode,
      name,
      identity,
      finishType,
      pageNumber,
      pageSize,
    },
    (v) => v !== ""
  );
  return httpApp.post(
    "/api/admin/examStudent/exam_record_detail/query?" +
      object2QueryString(data)
  );
}
export function studentExamDetailInfo(examRecordId) {
  return httpApp.post(
    "/api/admin/examStudent/exam_record/paper_view?examRecordId=" + examRecordId
  );
}

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