import { WeaLoadingGlobal } from "ecCom";

const server = window.server || "";
export const formHeaderPost = (url, method, params, header) => {
  if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
    params.access_token = localStorage.access_token;
  }
  url = server + url + "?__random__=" + (new Date()).valueOf();
  let formdata = new URLSearchParams();
  Object.keys(params).map(key => {
    formdata.append(key, params[key]);
  });
  return fetch(url, {
    method, mode: "cors",
    headers: { "Content-Type": "application/x-www-form-urlencoded", ...header },
    body: formdata
  }).then(res => res.json());
};
export const postFetch = (url, params) => {
  if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
    params.access_token = localStorage.access_token;
  }
  url = server + url + "?__random__=" + (new Date()).valueOf();
  return fetch(url, getFetchParams("POST", params)).then(res => res.json());
};
export const headerRequestFetch = (url, method, params, header) => {
  if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
    params.access_token = localStorage.access_token;
  }
  url = server + url + "?__random__=" + (new Date()).valueOf();
  return fetch(url, getFetchParams(method, params, header)).then(res => res.json());
};
export const postExportFetch = (url, params) => {
  if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
    params.access_token = localStorage.access_token;
  }
  url = server + url + "?__random__=" + (new Date()).valueOf();
  return fetch(url, getFetchParams("POST", params)).then(res => {
    WeaLoadingGlobal.destroy();
    const filename = res.headers.get("Content-Disposition").split("filename=")[1];
    res.blob().then(blob => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = decodeURI(filename);
      a.click();
      window.URL.revokeObjectURL(url);
    });
  });
};
export const getFd = (values) => {
  let fd = {};
  for (let p in values) {
    values[p] = _.isNil(values[p]) ? "" : values[p];
    let item = values[p];
    if (window.E9Encrypt && __AESEcrypt__) item = __AESEcrypt__.aes_data_encrypt(item);
    fd = { ...fd, [p]: item };
  }
  if (window.E9Encrypt && __AESEcrypt__) {
    fd = { ...fd, rsaAes01: __AESEcrypt__.get_rsa_aes_01(), rsaAes02: __AESEcrypt__.get_rsa_aes_02() };
  }
  return fd;
};
const getFetchParams = (method, params, header = {}) => {
  let obj = {
    method, mode: "cors",
    headers: {
      "Content-Type": "application/json",
      "X-Requested-With": "XMLHttpRequest",
      ...header
    }
  };
  if (server === "") {
    obj.credentials = "include";
  }
  if (!_.isEmpty(params) && Object.prototype.toString.call(params) === "[object Object]") {
    obj.body = JSON.stringify(getFd(params));
  } else {
    obj.body = JSON.stringify(params);
  }
  return obj;
};

