import { postFile, postParseFile } from "./axios";

export async function updateFile(name, file, corpId, sendType, getType) {
  const formData = new FormData();
  formData.append("file", file);
  formData.append("fileName", name);
  formData.append("corpId", sessionStorage.getItem("corpId")); // 添加 corpId 到表单数据
  formData.append("sendType", sendType);
  formData.append("getType", getType);
  const { filePath, success, message, media_id, url } = await postFile(formData);
  if (getType === "mediaId") {
    return { success, message, media_id };
  }
  const download_url = success ? `${import.meta.env.VITE_API_URL}${filePath}` : "";
  return {
    download_url,
    url,
    success,
    message,
  };
}


export async function uploadDrugCsv(file, form) {
  const formData = new FormData();
  formData.append("file", file);
  if (Object.prototype.toString.call(form) === "[object Object]") {
    Object.keys(form).forEach(key => {
      formData.append(key, form[key]);
    });
  }
  const res = await postParseFile(formData, '/upload-drug-csv');
  return res;
} 

// 配送/线上购药药品库 CSV 导入，对接 /upload-online-drug-csv
export async function uploadOnlineDrugCsv(file, form) {
  const formData = new FormData();
  formData.append("file", file);
  if (Object.prototype.toString.call(form) === "[object Object]") {
    Object.keys(form).forEach((key) => {
      formData.append(key, form[key]);
    });
  }
  const res = await postParseFile(formData, "/upload-online-drug-csv");
  return res;
}
