import { $postParam, $post } from "@/plugins/axios";
import { fileMD5 } from "@/plugins/md5";
import { isNull } from "@/plugins/utils";

export const cardConfigInfos = (id) => {
  return $postParam("/api/admin/basic/card_rule/get_one", { id });
};
export const cardDetail = (cardId) => {
  return $postParam("/api/admin/exam/card/get_one", { cardId });
};

export const saveCard1 = (datas, config = {}) => {
  if (datas.type === "GENERIC") {
    return $post("/api/admin/exam/card/save_generic", datas, config);
  } else {
    return $post("/api/admin/exam/card/save", datas, config);
  }
};

export const saveCard = async (datas, config = {}) => {
  const formData = new FormData();
  for (const key in datas) {
    const val = datas[key];
    if (key === "content") {
      const file = new File([val], `${Date.now()}.json`, { type: "text/json" });
      formData.append(`${key}File`, file);
      const md5 = await fileMD5(file);
      formData.append(`${key}Md5`, md5);
    } else if (key === "htmlContent") {
      const file = new File([val], `${Date.now()}.html`, { type: "text/html" });
      formData.append(`${key}File`, file);
      const md5 = await fileMD5(file);
      formData.append(`${key}Md5`, md5);
    } else {
      if (!isNull(val)) formData.append(key, val);
    }
  }

  if (datas.type === "GENERIC") {
    return $post("/api/admin/exam/card/save_generic", formData, config);
  } else {
    return $post("/api/admin/exam/card/save", formData, config);
  }
};
export const copyCard = (cardId, courseId) => {
  return $postParam("/api/admin/exam/card/copy", { id: cardId, courseId });
};
