import request from "@/utils/request";
import parseTime from "@/utils/index.js";

export function postJson(url, data) {
  return request({
    url: process.env.VUE_APP_BASE_API + url,
    method: "post",
    timeout: 600000,
    data,
  });
}

export function postJson2(url, data) {
  return request({
    url: url,
    method: "post",
    timeout: 600000,
    data,
  });
}

export function downloadfileCRM(data) {
  return request({
    url: "/downloadfileCRM",
    responseType: "blob",
    method: "post",
    data,
  });
}

export function wxpush(data) {
  return request({
    url: "/authdata/wxpush",
    method: "post",
    data,
  });
}

export function GetDataByName(data) {
  return request({
    url: "/authdata/GetDataByName",
    method: "post",
    data,
  });
}

export function ExeSqlJiade(data) {
  return request({
    url: "/authdata/exesql",
    method: "post",
    data,
  });
}
export function dashboardListJiade(data) {
  return request({
    url: "/authdata/Dashboards",
    method: "post",
    data,
  });
}

export function updateDashboardJiade(data) {
  return request({
    url: "/authdata/Updatedashboard",
    method: "post",
    data,
  });
}

export function GetReportform(data) {
  return request({
    url: "/authdata/GetReportform",
    method: "post",
    data,
  });
}
export function requestbyname(data, requestname) {
  return request({
    url: "/authdata/" + requestname,
    method: "post",
    data,
  });
}

export function GetDataByNameXlsx(data) {
  return request({
    url: "/authdata/GetDataByName",
    method: "post",
    data,
    responseType: "blob",
  });
}

export function GetDataByNames(data) {
  return request({
    url: "/authdata/GetDataByNames",
    method: "post",
    data,
  });
}

export function PostDataByName(data) {
  return request({
    url: "/authdata/PostDataByName",
    method: "post",
    data,
  });
}
export function GetUpkeepPlan(data) {
  return request({
    url: "/authdata/GetUpkeepPlan",
    method: "post",
    data,
  });
}
export function GetAccount(data) {
  return request({
    url: "/authdata/GetAccount",
    method: "post",
    timeout: 600000,
    data,
  });
}

export function GetArrList(data) {
  return request({
    url: "/authdata/GetArrList",
    method: "post",
    data,
  });
}
export function PostDataByNames(data) {
  return request({
    url: "/authdata/PostDataByNames",
    method: "post",
    data,
  });
}

export function ExecDataByConfig(data) {
  return request({
    url: "/authdata/ExecDataByConfig",
    method: "post",
    data,
  });
}

export function removeimage(data) {
  return request({
    url: "/authdata/removeimage",
    method: "post",
    data,
  });
}

export function getRecuData(data) {
  return request({
    url: "/authdata/GetRecuDataByName",
    method: "post",
    data,
  });
}

export function failproccess(data, notify) {
  if (data.data.includes("Duplicate")) {
    notify({
      title: "失败",
      message: "不可以录入重复数据",
      type: "error",
      duration: 2000,
    });
  } else {
    notify({
      title: "失败",
      message: "数据存在错误，请校验好重新录入，不可以录入数据",
      type: "error",
      duration: 2000,
    });
  }
}

export function form_edit_saveRelation(data) {
  return request({
    url: "/authdata/form_edit_saveRelation",
    method: "post",
    data,
  });
}

export function transData(a, idStr, pidStr, chindrenStr) {
  var r = [];
  var hash = {};
  var id = idStr;
  var pid = pidStr;
  var children = chindrenStr;
  var i = 0;
  var j = 0;
  var len = a.length;
  for (; i < len; i++) {
    hash[a[i][id]] = a[i];
  }
  for (; j < len; j++) {
    var aVal = a[j];
    var hashVP = hash[aVal[pid]];
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = []);
      hashVP[children].push(aVal);
    } else {
      r.push(aVal);
    }
  }
  return r;
}
export function UpdateDataRelation(data) {
  return request({
    url: "/authdata/UpdateDataRelation",
    method: "post",
    data,
  });
}
export function checkButtons(buttonsList, PermissionButtons) {
  // console.log(PermissionButtons)
  for (let i = 0; i < buttonsList.length; i++) {
    if (
      buttonsList[i].path === PermissionButtons &&
      buttonsList[i].menu_id &&
      buttonsList[i].path
    ) {
      // path不为空且menu_id不为空时返回true
      return true;
    }
  }
  return false;
}

// export function checkButtons(buttonsList, PermissionButtons) {
//   // console.log(PermissionButtons)
//   for (let i = 0; i < buttonsList.length; i++) {
//     if (buttonsList[i].path === PermissionButtons && buttonsList[i].editbutton === 1) { // path不为空且menu_id不为空时返回true
//       return true
//     }
//   }
//   return false
// }

export function formatJson(filterVal, jsonData) {
  return jsonData.map((v) =>
    filterVal.map((j) => {
      if (j === "timestamp") {
        return parseTime(v[j]);
      } else {
        return v[j];
      }
    })
  );
}

export function DownloadExcel(data, filename) {
  const content = data;
  const blob = new Blob([content]);
  const fileName = filename + ".xlsx";
  if ("download" in document.createElement("a")) {
    // 非IE下载
    const elink = document.createElement("a");
    elink.download = fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
}

export function calculativeWidth() {
  var operateArr = [];
  for (let i = 0; i < arguments.length; i++) {
    operateArr.push(arguments[i]);
  }
  console.log("operateArr==========", operateArr);
  var tureCount = operateArr.filter((item) => item === true).length;
  if (tureCount) {
    // 每个按钮预计90宽度
    var countWidth = tureCount * 90;
    console.log(countWidth);
    return countWidth;
  } else {
    return 0;
  }
}

// console.log = function () {

// }

export function handleWechatPush(send_weixin_data1, serviceId) {
  return new Promise((resolve, reject) => {
    GetDataByName(send_weixin_data1)
      .then((response) => {
        console.log("微信推送(获取参数)======>", response);
        if (response.data.message == "ok") {
          var receiverStr = response.data.list[0].receivers;
          var receiverArr = receiverStr.split(",");
          var value1 = response.data.list[0].value1;
          var value2 = response.data.list[0].value2;
          var value3 = response.data.list[0].value3;
          var value4 = response.data.list[0].value4;
          var send_weixin_data2 = {
            target: receiverArr,
            serviceId: serviceId,
            content: [
              { color: "#173177", value: value1 },
              { color: "#173177", value: value2 },
              { color: "#173177", value: value3 },
              { color: "#173177", value: value4 },
            ],
          };

          wxpush(send_weixin_data2)
            .then((response) => {
              console.log("微信推送(发送返回)======>", response);
              resolve(response);
            })
            .catch((error) => {
              reject(error);
            });
        } else {
          reject(new Error("获取推送参数失败"));
        }
      })
      .catch((error) => {
        reject(error);
      });
  });
}

export function handleWechatPushOfOrder(send_weixin_data1, serviceId) {
  return new Promise((resolve, reject) => {
    GetDataByName(send_weixin_data1)
      .then((response) => {
        console.log("微信推送(获取参数)======>", response);
        if (response.data.message == "ok") {
          var receiverStr = response.data.list[0].receivers;
          var receiverArr = receiverStr.split(",");
          var value1 = response.data.list[0].value1;
          var value2 = response.data.list[0].value2;
          var value3 = response.data.list[0].value3;
          var value4 = response.data.list[0].value4;
          var value5 = response.data.list[0].value5;

          value4 =
            value4.length > 17 ? value4.substring(0, 17) + "..." : value4;

          value3 = value3.length > 5 ? value3.substring(0, 5) : value3;

          var send_weixin_data2 = {
            target: receiverArr,
            serviceId: serviceId,
            keys: [
              "thing3",
              "thing9",
              "phrase5",
              "thing14",
              "character_string15",
            ],
            content: [
              { color: "#173177", value: value1 },
              { color: "#173177", value: value2 },
              { color: "#173177", value: value3 },
              { color: "#173177", value: value4 },
              { color: "#173177", value: value5 },
            ],
          };

          wxpush(send_weixin_data2)
            .then((response) => {
              console.log("微信推送(发送返回)======>", response);
              resolve(response);
            })
            .catch((error) => {
              reject(error);
            });
        } else {
          reject(new Error("获取推送参数失败"));
        }
      })
      .catch((error) => {
        reject(error);
      });
  });
}
