import dayjs from "dayjs";

/**
 * 节点状态值
 *
 * @return {*}
 */
function getStatusName(statu) {
  const statusName = new Map();

  statusName.set(11, "未订舱");
  statusName.set(12, "已订舱");

  statusName.set(21, "未预装");
  statusName.set(22, "预装中");
  statusName.set(23, "预装审核中");
  statusName.set(24, "预装审核失败");
  statusName.set(25, "预装审核成功");

  statusName.set(2111, "未理货");
  statusName.set(2112, "已理货");

  statusName.set(31, "未派车");
  statusName.set(32, "已派车");

  statusName.set(41, "未装柜");
  statusName.set(42, "装柜中");
  statusName.set(43, "已装柜、待封柜");
  statusName.set(44, "封柜审核中");
  statusName.set(45, "封柜审核失败");
  statusName.set(46, "封柜审核成功");
  statusName.set(47, "已封柜,待出仓");

  statusName.set(51, "未报关");
  statusName.set(52, "报关中");
  statusName.set(53, "已报关");

  statusName.set(61, "未配船");
  statusName.set(62, "已配船");

  statusName.set(71, "未提单补料");
  statusName.set(72, "已提单补料");

  statusName.set(81, "未驳船");
  statusName.set(82, "已驳船");

  statusName.set(91, "未起运");
  statusName.set(92, "已起运");

  statusName.set(101, "未上传");
  statusName.set(102, "已上传");

  statusName.set(111, "未清关文件");
  statusName.set(112, "已清关文件");

  statusName.set(121, "未到港");
  statusName.set(122, "已到港");

  statusName.set(131, "未清关");
  statusName.set(132, "已清关");

  statusName.set(181, "未卸柜");
  statusName.set(182, "卸柜中");
  statusName.set(183, "卸柜审核中");
  statusName.set(184, "卸柜审核失败");
  statusName.set(185, "卸柜审核成功");
  statusName.set(186, "已卸柜");

  statusName.set(191, "未结算");
  statusName.set(192, "结算中");
  statusName.set(193, "已结算");

  return statusName.get(statu);
}

/**
 * 海运流程
 *
 * @return {*}
 */
function seaBaseData() {
  return [
    [
      {
        title: "订舱",
        imgSrc: {
          start: require("@/assets/images/shipping/dc-start.png"),
          end: require("@/assets/images/shipping/dc-end.png"),
        },
        type: "booking", // 类型
        dataKey: "1", // 字典数据键值
        /**
         * 订舱状态:11、未订舱;12、已订舱
         */
        voName: "bookSeaInfo", // 订舱对象vo
        keyName: "bkStatus",
        status: {
          start: [11],
          wait: [],
          end: [12],
        },
      },
    ],
    [
      {
        title: "排单",
        imgSrc: {
          start: require("@/assets/images/shipping/yz-start.png"),
          wait: require("@/assets/images/shipping/yz-wait.png"),
          end: require("@/assets/images/shipping/yz-end.png"),
        },
        type: "preinstall",
        dataKey: "2", // 字典数据键值
        /**
         * 预装状态:21、未预装;22、预装中;23、预装审核中;24、预装审核失败;25、预装审核成功
         */
        voName: "preInstallInfo",
        keyName: "prStatus",
        status: {
          start: [21],
          wait: [22, 23, 24],
          end: [25],
        },
      },
      {
        title: "AGENT",
        imgSrc: {
          start: require("@/assets/images/shipping/agent-start.png"),
          wait: require("@/assets/images/shipping/agent-wait.png"),
          end: require("@/assets/images/shipping/agent-end.png"),
        },
        type: "agent",
        voName: "agentInfo",
        currStatus: "start",
      },
    ],
    [
      {
        title: "理货",
        imgSrc: {
          start: require("@/assets/images/shipping/lh-start.png"),
          wait: require("@/assets/images/shipping/lh-wait.png"),
          end: require("@/assets/images/shipping/lh-end.png"),
        },
        type: "tally",
        dataKey: "16", // 字典数据键值
        /**
         * 理货状态:2111、未理货;2112、已理货
         */
        voName: "tallyInfo",
        keyName: "tyStatus",
        status: {
          start: [2111],
          wait: [],
          end: [2112],
        },
      },
      {
        title: "拖车",
        imgSrc: {
          start: require("@/assets/images/shipping/tc-start.png"),
          wait: require("@/assets/images/shipping/tc-wait.png"),
          end: require("@/assets/images/shipping/tc-end.png"),
        },
        type: "trailer",
        dataKey: "3", // 字典数据键值
        /**
         * 拖车状态:31、未派车;32、已派车
         */
        voName: "trailerInfo",
        keyName: "tlStatus",
        status: {
          start: [31],
          wait: [],
          end: [32],
        },
      },
    ],
    [
      {
        title: "装柜",
        imgSrc: {
          start: require("@/assets/images/shipping/zg-start.png"),
          wait: require("@/assets/images/shipping/zg-wait.png"),
          end: require("@/assets/images/shipping/zg-end.png"),
        },
        type: "cabinet",
        dataKey: "4", // 字典数据键值
        /**
         * 装柜状态:41、未装柜;42、装柜中;43、已装柜、待封柜;44、封柜审核中;45、封柜审核失败;46、封柜审核成功;47、已封柜,待出仓
         */
        voName: "cabinetInfo",
        keyName: "ldStatus",
        status: {
          start: [41],
          wait: [42, 43, 44, 45, 46],
          end: [47],
        },
      },
    ],
    [
      {
        title: "报关",
        imgSrc: {
          start: require("@/assets/images/shipping/bg-start.png"),
          wait: require("@/assets/images/shipping/bg-wait.png"),
          end: require("@/assets/images/shipping/bg-end.png"),
        },
        type: "cusDeclaration",
        dataKey: "5", // 字典数据键值
        /**
         *报关状态:51、未报关;52、报关中;53、已报关
         */
        voName: "customsInfo",
        keyName: "dcStatus",
        status: {
          start: [51],
          wait: [52],
          end: [53],
        },
      },
      {
        title: "配船",
        imgSrc: {
          start: require("@/assets/images/shipping/pc-start.png"),
          wait: require("@/assets/images/shipping/pc-wait.png"),
          end: require("@/assets/images/shipping/pc-end.png"),
        },
        type: "ship",
        dataKey: "6", // 字典数据键值
        /**
         * 配船状态:61、未配船;62、已配船
         */
        voName: "shipConfigInfo",
        keyName: "saStatus",
        status: {
          start: [61],
          wait: [],
          end: [62],
        },
      },
      {
        title: "提单补料",
        imgSrc: {
          start: require("@/assets/images/shipping/tdcl-start.png"),
          wait: require("@/assets/images/shipping/tdcl-wait.png"),
          end: require("@/assets/images/shipping/tdcl-end.png"),
        },
        type: "subMaterial",
        dataKey: "7", // 字典数据键值
        /**
         * 提单补料状态:71、未提单补料;72、已提单补料
         */
        voName: "ladingBillInfo",
        keyName: "blStatus",
        status: {
          start: [71],
          wait: [],
          end: [72],
        },
      },
    ],
    [
      {
        title: "驳船",
        imgSrc: {
          start: require("@/assets/images/shipping/bc-start.png"),
          wait: require("@/assets/images/shipping/bc-wait.png"),
          end: require("@/assets/images/shipping/bc-end.png"),
        },
        type: "barge",
        dataKey: "8", // 字典数据键值
        /**
         * 驳船状态:81、未驳船;82、已驳船
         */
        voName: "bargeInfo",
        keyName: "bgStatus",
        status: {
          start: [81],
          wait: [],
          end: [82],
        },
      },
    ],
    [
      {
        title: "起运",
        imgSrc: {
          start: require("@/assets/images/shipping/qy-start.png"),
          wait: require("@/assets/images/shipping/qy-wait.png"),
          end: require("@/assets/images/shipping/qy-end.png"),
        },
        type: "departure",
        dataKey: "9", // 字典数据键值
        /**
         * 起运状态:91、未起运;92、已起运
         */
        voName: "shippingInfo",
        keyName: "dtStatus",
        status: {
          start: [91],
          wait: [],
          end: [92],
        },
      },
    ],
    [
      {
        title: "提单Copy",
        imgSrc: {
          start: require("@/assets/images/shipping/tdcopy-start.png"),
          wait: require("@/assets/images/shipping/tdcopy-wait.png"),
          end: require("@/assets/images/shipping/tdcopy-end.png"),
        },
        type: "blCopy",
        dataKey: "10", // 字典数据键值
        /**
         * 提单COPY状态:101、未上传;102、已上传
         */
        voName: "ladingCopyInfo",
        keyName: "cpStatus",
        status: {
          start: [101],
          wait: [],
          end: [102],
        },
      },
      {
        type: "clrDocument",
        imgSrc: {
          start: require("@/assets/images/shipping/qg-start.png"),
          wait: require("@/assets/images/shipping/qg-wait.png"),
          end: require("@/assets/images/shipping/qg-end.png"),
        },
        title: "清关文件",
        dataKey: "11", // 字典数据键值
        /**
         * 清关文件状态:111、未清关文件;112、已清关文件
         */
        voName: "clearanceDocInfo",
        keyName: "cdStatus",
        status: {
          start: [111],
          wait: [],
          end: [112],
        },
      },
    ],
    [
      {
        title: "到港",
        imgSrc: {
          start: require("@/assets/images/shipping/dg-start.png"),
          wait: require("@/assets/images/shipping/dg-wait.png"),
          end: require("@/assets/images/shipping/dg-end.png"),
        },
        type: "arrival",
        dataKey: "12", // 字典数据键值
        /**
         * 到港状态:121、未到港;112、已到港
         */
        voName: "arrivalInfo",
        keyName: "apStatus",
        status: {
          start: [121],
          wait: [],
          end: [122],
        },
      },
    ],
    [
      {
        title: "清关",
        imgSrc: {
          start: require("@/assets/images/shipping/qg-start.png"),
          wait: require("@/assets/images/shipping/qg-wait.png"),
          end: require("@/assets/images/shipping/qg-end.png"),
        },
        type: "cusClearance",
        dataKey: "13", // 字典数据键值
        /**
         * 清关状态:131、未清关;132、已清关
         */
        voName: "clearanceInfo",
        keyName: "clStatus",
        status: {
          start: [131],
          wait: [],
          end: [132],
        },
      },
    ],
    [
      {
        title: "卸柜",
        imgSrc: {
          start: require("@/assets/images/shipping/xg-start.png"),
          wait: require("@/assets/images/shipping/xg-wait.png"),
          end: require("@/assets/images/shipping/xg-end.png"),
        },
        type: "unloading",
        dataKey: "14", // 字典数据键值
        /**
         * 卸柜状态:181、未卸柜;182、卸柜中;183、卸柜审核中;184、卸柜审核失败;185、卸柜审核成功;186、已卸柜
         */
        voName: "cabinetUnloadInfo",
        keyName: "ulStatus",
        status: {
          start: [181],
          wait: [182, 183, 184, 185],
          end: [186],
        },
      },
    ],
    [
      {
        title: "结算",
        imgSrc: {
          start: require("@/assets/images/shipping/js-start.png"),
          wait: require("@/assets/images/shipping/js-wait.png"),
          end: require("@/assets/images/shipping/js-end.png"),
        },
        type: "settlement",
        dataKey: "15", // 字典数据键值
        /**
         * 结算状态:191、未结算;192、结算中;193、已结算
         */
        voName: "settlementInfo",
        keyName: "slStatus",
        status: {
          start: [191],
          wait: [192],
          end: [193],
        },
      },
    ],
  ];
}

/**
 * 详情显示列
 *
 * @return {*}
 */
function getColmnMapping() {
  return {
    bookSeaInfo: [
      {
        title: "SO NO",
        key: "sono",
      },
      {
        title: "船公司类型",
        key: "shipCompanyType",
        type: "supplier",
      },
      {
        title: "驳船港",
        key: "bargePortId",
        type: "dock",
      },
      {
        title: "大船港",
        key: "bigPortId",
        type: "dock",
      },
      {
        title: "目的港",
        key: "destPortId",
        type: "dock",
      },
      {
        title: "订舱公司",
        key: "spaceCompanyId",
        type: "supplier",
      },
      {
        title: "预计驳船时间",
        key: "bargeTime",
        type: "date",
      },
      {
        title: "预计开船时间",
        key: "sailTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    preInstallInfo: [
      {
        title: "预装时间",
        key: "createTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    trailerInfo: [
      {
        title: "货柜号",
        key: "tlContainerNo",
      },
      {
        title: "封条",
        key: "tlStripSeal",
      },
      {
        title: "拖车公司",
        key: "tlCompanyId",
        type: "supplier",
      },
      {
        title: "拖车时间",
        key: "tlTime",
        type: "date",
      },
      {
        title: "车牌",
        key: "tlLicensePlate",
      },
      {
        title: "司机",
        key: "tlDriver",
      },
      {
        title: "联系方式",
        key: "tlDriverContact",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    tallyInfo: [
      {
        title: "完成理货时间",
        key: "tyTime",
        type: "datetime",
      },
      {
        title: "业务员",
        key: "tyOperator",
      },
    ],
    agentInfo: [
      {
        title: "代理商",
        key: "agentId",
        type: "supplier",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    cabinetInfo: [
      {
        title: "到仓时间",
        key: "ldInWarehouseTime",
        type: "date",
      },
      {
        title: "仓库类型",
        key: "ldWarehouseType",
        type: "warehouse",
      },
      {
        title: "装柜时间",
        key: "ldBoxTime",
        type: "date",
      },
      {
        title: "出仓时间",
        key: "ldOutWarehouseTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    customsInfo: [
      {
        title: "报关方式",
        key: "dcCustomsType",
        type: "shipping_customs_type",
      },
      {
        title: "报关行公司",
        key: "dcCompanyId",
        type: "supplier",
      },
      {
        title: "截关时间",
        key: "dcCutOffTime",
        type: "date",
      },
      {
        title: "状态",
        key: "dcCustomsStatus",
        type: "shipping_dcCustoms_status",
      },
      {
        title: "放行时间",
        key: "dcPassTime",
        type: "date",
      },
      {
        title: "新封条",
        key: "dcStripSeal",
      },
      {
        title: "柜重",
        key: "dcBoxWgt",
      },
      {
        title: "货重",
        key: "dcGoodsWgt",
      },
      {
        title: "VGM重量",
        key: "dcVgmWgt",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    shipConfigInfo: [
      {
        title: "已配/免配",
        key: "saExmtStatus",
        type: "saExmtStatus",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    ladingBillInfo: [
      {
        title: "清关代理",
        key: "agentId",
        type: "supplier",
      },
      {
        title: "提单品名",
        key: "blMblNo",
      },
      {
        title: "通知方",
        key: "notifyingId",
        type: "user",
      },
      {
        title: "提单备注",
        key: "remarks",
      },
      {
        title: "数量(个)",
        key: "packageNum",
      },
      {
        title: "体积",
        key: "cbm",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    bargeInfo: [
      {
        title: "驳船",
        key: "bgExmtStatus",
        type: "bgExmtStatus",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    shippingInfo: [
      {
        title: "实际开船时间",
        key: "dtRealShipTime",
        type: "datetime",
      },
      {
        title: "预计到港时间",
        key: "dtEstArrivalTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    clearanceDocInfo: [
      {
        title: "agent list",
        key: "cdAgentlistType",
        type: "isFile",
      },
      {
        title: "soncap",
        key: "cdSoncapType",
        type: "isFile",
      },
      {
        title: "出单方式",
        key: "cdOutBillType",
        type: "billingMethod",
      },
      {
        title: "寄送时间",
        key: "cdSendTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    arrivalInfo: [
      {
        title: "实际到港时间",
        key: "apRealTime",
        type: "datetime",
      },
      {
        title: "确认到港",
        key: "apConfirmTime",
        type: "datetime",
      },
      {
        title: "卸港时间",
        key: "apUnloadPortTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    clearanceInfo: [
      {
        title: "预计清关时间",
        key: "clEstTime",
        type: "date",
      },
      {
        title: "清关时间",
        key: "clClearTime",
        type: "datetime",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    cabinetUnloadInfo: [
      {
        title: "到仓时间",
        key: "ulWarehouseTime",
        type: "date",
      },
      {
        title: "卸货时间",
        key: "ulBoxTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
    settlementInfo: [
      {
        title: "可结算",
        key: "slSettlementTime",
        type: "date",
      },
      {
        title: "已结算",
        key: "slSettledTime",
        type: "date",
      },
      {
        title: "业务员",
        key: "operator",
      },
    ],
  };
}

/**
 * 常量字典
 */
const constantDict = {
  // 配船状态
  saExmtStatus: [
    {
      value: "1",
      label: "免配",
    },
    {
      value: "2",
      label: "已配",
    },
  ],
  // 驳船
  bgExmtStatus: [
    {
      value: "1",
      label: "无驳船",
    },
    {
      value: "2",
      label: "有驳船",
    },
  ],
  // 出单方式
  billingMethod: [
    {
      value: "1",
      label: "电放",
    },
    {
      value: "2",
      label: "正本",
    },
  ],
  // 是否有文件
  isFile: [
    {
      value: "1",
      label: "无需",
    },
    {
      value: "2",
      label: "已做",
    },
  ],
};

/**
 * string format number
 *
 * @param {*} obj
 * @param {*} keys
 * @return {*}
 */
function formatStringNumber(obj, keys) {
  for (const [key, value] of Object.entries(obj)) {
    if (keys.includes(key) && !Number.isNaN(Number(value))) {
      obj[key] = Number(value);
    }
  }
  return obj;
}

/**
 * number format string
 *
 * @param {*} obj
 * @param {*} keys
 * @return {*}
 */
function formatNumberString(obj, keys) {
  for (const [key, value] of Object.entries(obj)) {
    if (keys.includes(key) && value) {
      obj[key] = String(value);
    }
  }
  return obj;
}

/**
 * stringDate format Date
 *
 * @param {*} obj
 * @param {*} keys
 * @param {string} [format="YYYY-MM-DD"]
 * @return {*}
 */
function formatDateStr(obj, keys, format = "YYYY-MM-DD") {
  for (const [key, value] of Object.entries(obj)) {
    if (keys.includes(key) && dayjs(value).isValid()) {
      obj[key] = dayjs(value).format(format);
    }
  }
  return obj;
}

/**
 * 格式化日期
 *
 * @param {*} date
 * @param {string} [format="YYYY-MM-DD"]
 * @return {*}
 */
function formatDate(date, format = "YYYY-MM-DD") {
  if (!date) return date;
  return dayjs(date).format(format);
}

/**
 * 获取海运流程状态
 *
 * @param {*} val
 * @return {*}
 */
function getSeaStatus(val) {
  let currNodeStatus = 11,
    isBreak = false,
    datas = seaBaseData();
  // 迭代每个节点
  for (let i = 0; i < datas.length; i++) {
    const nodes = datas[i];
    for (let j = 0; j < nodes.length; j++) {
      const node = nodes[j];
      const { keyName, voName, status } = node;
      if (!keyName) continue;
      const { start, wait, end } = status;

      if (start.includes(val[keyName]) && val[voName]) {
        currNodeStatus = val[keyName];
      }

      if (wait.includes(val[keyName])) {
        currNodeStatus = val[keyName];
        isBreak = true;
        break;
      }

      if (end.includes(val[keyName])) {
        currNodeStatus = val[keyName];
      }
    }
    if (isBreak) break;
  }
  return currNodeStatus;
}

/**
 * 组装统计信息文字
 *
 * @param {*} total
 * @param {string} [keys=["num", "volume", "weight"]]
 * @return {*}
 */
function getTotlContent(total, keys = ["num", "volume", "weight"]) {
  let content = [];
  for (const key of keys) {
    if (key === "num") {
      content.push(`${total?.num ?? 0}箱`);
    }
    if (key === "volume") {
      content.push(`${total?.volume ?? 0}m³`);
    }
    if (key === "weight") {
      content.push(`${total?.weight ?? 0}kg`);
    }
  }
  return content.join(" ");
}

function getCapacity(capacity) {
  return `${capacity?.name ?? ""}柜型 ${capacity?.volume ?? 0}m³ ${
    capacity?.weight ?? 0
  }kg`;
}

function sumStatistics(val) {
  let count = { num: 0, volume: 0, weight: 0 };
  if (Array.isArray(val)) {
    val.forEach((item) => {
      const { secStatistics } = item;
      if (secStatistics) {
        if (!Number.isNaN(Number(secStatistics.num))) {
          count.num = count.num + Number(secStatistics.num);
        }
        if (!Number.isNaN(Number(secStatistics.volume))) {
          count.volume = count.volume + Number(secStatistics.volume);
        }
        if (!Number.isNaN(Number(secStatistics.weight))) {
          count.weight = count.weight + Number(secStatistics.weight);
        }
      }
    });
  }
  return getTotlContent(count);
}

export const fileTypes = [
  "doc",
  "xls",
  "ppt",
  "txt",
  "pdf",
  "png",
  "jpg",
  "jpeg",
];

/**
 * 服务提示消息回调
 *
 * @export
 * @param {*} result
 * @param {*} _vue
 * @return {*}
 */
function serviceMsg(result, _vue) {
  return new Promise((resolve, reject) => {
    const { code, msg } = result;
    if (code === 0) {
      _vue.$message.success("操作成功");
      resolve();
    } else {
      _vue.$message.error(msg);
      reject();
    }
  });
}

export {
  getStatusName,
  getColmnMapping,
  getSeaStatus,
  seaBaseData,
  constantDict,
  formatStringNumber,
  formatDateStr,
  formatNumberString,
  formatDate,
  getTotlContent,
  getCapacity,
  sumStatistics,
  serviceMsg,
};