import axios from "@/config/axios.js";

const recordObj = {
  contractNo: "合同号",
  matterName: "物料名称",
  batchNo: "批次号",
  customerSupplierName: "供应商",
  forwardingUnitName: "发货单位",
  receivingUintName: "收货单位",
  materialName: "材质",
  specName: "规格",
  shipmentGrossWeight: "发运毛重",
  shipmentNetWeight: "发运净重",
  shipmentNum: "发运数量",
  addWeightSum: "附加重量总和",
  loadPointName: "卸货地点",
  carrierUnitName: "承运单位",
  predictionType: "预报类型",
  meterTypeName: "业务类型",
  usedType: "可用类型",
  effectiveStartTime: "有效开始时间",
  effectiveEndTime: "有效结束时间",
  memo: "备注",
  updateManNo: "修改人编号",
  updateManName: "修改人名称",
  isCompleted: "是否完批",
  predictionNo: "预报编号",
  addWeight: "附加重量",
  railwayNo: "火车车号",
  meterMode: "计量方式",
  carNo: "车号",
  meterWeight: "计量重量",
  meterGroup: "班组",
  weightType: "重量类型",
  baseSpotName: "计量点",
  meterNum: "计量数量",
  meterClass: "班次",
  trailerNo: "挂号",
  createTime: "计量时间",
  indexCode: "物资索引码",
  sourceArea: "原产地",
  total: "总量(kg)",
  toleranceRatio: "超差比例",
  meterSortWeight: "分检重量",
  station: "发站",
  arrival: "到站",
  railwayCarriageNo: "车厢序号",
  isChemical: "是否危化品",
  productName: "产品名称",
  gradeName: "牌号",
  standardName: "标准",
  packageNo: "包号",
  blockNum: "块数",
  sealNo: "铅封号",
  bucketNo: "组合包号",
  baseCode: "基础数据编码",
  baseName: "基础数据名称",
  pBaseCode: "检索码",
  isEditFlag: "编辑状态",
  minusMemo: "扣杂备注",
};

const recordChange = (oldObj, newObj) => {
  let oldStr = "",
    newStr = "";
  for (let newVal in newObj) {
    for (let oldVal in oldObj) {
      // 属性名称相同并且新老值不相等
      if (newVal === oldVal && newObj[newVal] !== oldObj[oldVal]) {
        // 如果初始值和新值都能隐式转换为零则不进行记录
        if (!(newObj[newVal] - 0 === 0 && oldObj[oldVal] - 0 === 0)) {
          // 只有存在于属性字段和属性名称对应字段中的才进行记录
          if (recordObj[newVal]) {
            oldStr += `${recordObj[oldVal]}：${oldObj[oldVal]};`;
            newStr += `${recordObj[newVal]}：${newObj[newVal]};`;
          }
        }
      }
    }
  }
  if (oldStr - 0 !== 0 && newObj - 0 !== 0) {
    oldStr = "修改前：" + oldStr;
    newStr = "修改后：" + newStr;
  } else {
    if (oldStr.length === 0) {
      oldStr = "未对值进行改动";
    }
    if (newStr.length === 0) {
      newStr = "未对值进行改动";
    }
  }
  return {
    oldStr,
    newStr,
  };
};

const formatJson = (filterVal, jsonData) => {
  return jsonData.map((v) => filterVal.map((j) => v[j]));
};

// 将数据导出到Excel
/**
 *
 * @param {要导出的数据} dataList
 * @param {表头} headerList
 * @param {要进行合计的字段名list} valList
 * @param {导出的表格名称} title
 * @param {合计字段集合} totalList
 * @param {合并列设置} merge
 * @param {第一行表头合计设置} mergeRow
 * @returns
 */
const exportToExcel = async (
  dataList,
  headerList,
  valList,
  title,
  totalList,
  merge = [],
  mergeRow = []
) => {
  if (dataList.length === 0) {
    return MessageBox.alert("没有可以导出的数据");
  }
  let valObj = {};
  // 计算合计字段的列位置
  totalList.forEach((item) => {
    let index = valList.findIndex((iItem) => iItem === item);
    if (index !== -1) {
      valObj[item] = index;
    }
  });
  require.ensure([], () => {
    const {
      export_json_to_excel1: jsonToExcel,
    } = require("../excel/Export2Excel");
    const data = formatJson(valList, dataList);
    jsonToExcel(
      null,
      headerList,
      merge,
      data,
      title,
      true,
      totalList,
      valObj,
      mergeRow
    );
  });
};

const deepClone = (obj) => {
  if (obj === null || typeof obj !== "object") return obj;
  // 处理数组
  if (obj instanceof Array) {
    return obj.map((item) => deepClone(item));
  }
  // 处理日期
  if (obj instanceof Date) {
    return new Date(obj);
  }
  // 处理普通对象
  const newObj = {};
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      newObj[key] = deepClone(obj[key]);
    }
  }
  return newObj;
};

export { recordChange, exportToExcel, deepClone };
