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

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

export function getActivityDetail({ id = "", pageNumber = 1, pageSize = 10 }) {
  if (id) {
    const data = pickBy({ id, pageNumber, pageSize }, (v) => v !== "");
    return httpApp.post(
      "/api/admin/activity/query?" + object2QueryString(data)
    );
  } else {
    throw "id必填";
  }
}

export function saveActivity({
  id = "",
  enable = 1,
  examId = "",
  finishTime = "",
  maxDurationSeconds = 0,
  openingSeconds = 0,
  prepareSeconds = 0,
  startTime = "",
}) {
  const data = pickBy(
    {
      examId,
      id,
      enable,
      finishTime,
      maxDurationSeconds,
      openingSeconds,
      prepareSeconds,
      startTime,
    },
    (v) => v !== ""
  );
  return httpApp.post("/api/admin/activity/save", [data]);
}

export function saveActivities(ary) {
  return httpApp.post("/api/admin/activity/save", ary);
}

export function toggleEnableActivity({ examId, id, enable }) {
  return httpApp.post("/api/admin/activity/save", [{ examId, id, enable }]);
}
