import { getLodop } from "../print.js";
import JsBarcode from "jsbarcode";
import QRCode from "qrcode";
import axios from "axios";
//设置日志
const setLog = logs => {
    let wmsUser = JSON.parse(localStorage.getItem("wms-user"));
    let baseUrl = `${import.meta.env.VITE_APP_API_BASEURL + import.meta.env.VITE_APP_API_VERSION}ClientLog/logs`;
    const { newUserToken } = wmsUser;
    axios
        .post(baseUrl, logs, {
            headers: {
                "Content-Type": "application/json",
                Authorization: newUserToken
            }
        })
        .then(res => {
            console.log(res, "=============res==============");
        });
};
//箱唛列表打印
let LODOP; //声明为全局变量
export const boxMarkListPrint = (printData, printNumber) => {
    LODOP = getLodop();
    LODOP.PRINT_INIT("箱唛打印");
    LODOP.SET_PRINT_PAGESIZE(0, 900, 400, "");
    LODOP.SET_PRINT_STYLE("FontSize", 9.3);
    LODOP.SET_PRINT_STYLE("Bold", 1);
    LODOP.SET_PRINT_STYLE("FontName", "黑体");

    let logs = [];
    let printDataClone = [];
    let resultArray = [];

    printDataClone = JSON.parse(JSON.stringify(printData));
    resultArray = printDataClone.reduce(function (acc, obj) {
        let existing = acc.find(function (item) {
            return item[0].id === obj.id;
        });
        if (existing) {
            existing.push(obj);
        } else {
            acc.push([obj]);
        }
        return acc;
    }, []);

    let resultArrayLength = resultArray.length;
    for (let i = 0; i < resultArrayLength; i++) {
        let item = resultArray[i];

        resultArray[i].sort(function (a, b) {
            return a.sort - b.sort;
        });
        let itemLength = item.length;
        for (let j = 0; j < itemLength; j++) {
            let itemJ = resultArray[i][j];
            LODOP.NewPage();
            AddPrintContent({ item: itemJ, logs: logs });
        }
    }
    setTimeout(() => {
        LODOP.SET_PRINT_COPIES(printNumber); //指定份数
        LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1); //设置为非逐份打印，1-1  2-2  3-3
        LODOP.PREVIEW();
        setLog(logs);
    }, 1500);
};
function AddPrintContent(obj) {
    const { item, logs } = obj;

    const {
        isTail,
        orderBillNo,
        specifications,
        materialName,
        cratingQty,
        productQty,
        remark,
        sort,
        createTime,
        barCode,
        tailboxQty,
        tailboxNetWeightQty,
        cratingNetWeightQty,
        tailboxGrossWeightQty,
        cratingGrossWeightQty,
        boxSortCount
    } = item;

    let logStr = "";
    let image64 = "";
    let imageCode = "";
    if (specifications) {
        let chinesePattern = /[\u4e00-\u9fa5]/; // 匹配中文字符的正则表达式
        if (chinesePattern.test(specifications)) {
            if (specifications.length > 15) {
                LODOP.ADD_PRINT_TEXT(8, 8, "20mm", 10, "产品型号:");
                LODOP.ADD_PRINT_TEXT(8, 64, "45mm", 20, `${specifications}`);
                LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -5);
            } else {
                LODOP.ADD_PRINT_TEXT(8, 8, "20mm", "10mm", "产品型号:");
                LODOP.ADD_PRINT_TEXT(8, 64, "45mm", "100%", `${specifications}`);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -5);
            }
        } else {
            if (specifications.length > 25) {
                LODOP.ADD_PRINT_TEXT(8, 8, "20mm", 10, "产品型号:");
                LODOP.ADD_PRINT_TEXT(8, 64, "45mm", 20, `${specifications}`);
                LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -5);
            } else {
                LODOP.ADD_PRINT_TEXT(8, 8, "20mm", "10mm", "产品型号:");
                LODOP.ADD_PRINT_TEXT(8, 64, "45mm", "100%", `${specifications}`);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -5);
            }
        }
        logStr = `规格型号${specifications}`;
    }
    //备注
    if (remark) {
        if (remark.length > 13) {
            LODOP.ADD_PRINT_TEXT(8, 232, "28mm", 30, `${remark}`);
            LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
            LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
            LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
            LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -6);
        } else {
            LODOP.ADD_PRINT_TEXT(8, 232, "28mm", 30, `${remark}`);
            LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
            LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
            LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
            LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -6);
        }
    }
    //条码;

    let barCodeClone = barCode ? barCode.replace(/\s/g, "") : barCode;
    if (barCodeClone && barCodeClone.length === 13) {
        barCodeClone = Number(barCodeClone);
        //去掉空
        if (!isNaN(barCodeClone)) {
            JsBarcode("#barcode", barCodeClone, {
                format: "EAN13", // 指定条形码格式
                width: 1, //1.5
                height: 30,
                displayValue: false //不显示条形码下面的文字
            });
            // 获取 SVG 元素
            let svgElement = document.getElementById("barcode-box");
            image64 = svgElement.innerHTML;
            LODOP.ADD_PRINT_IMAGE(83, 226, "80%", "100%", svgElement.innerHTML);
            let str = barCodeClone + "";
            let str1 = str.substring(0, 1);
            let str2 = str.substring(1, 7);
            let str3 = str.substring(7, 14);
            //224
            LODOP.ADD_PRINT_TEXT(130, 231, "10mm", "10mm", `${str1}`);
            LODOP.SET_PRINT_STYLEA(0, "FontName", "SourceHanSansCN-Bold");
            //238
            LODOP.ADD_PRINT_TEXT(130, 245, "15mm", "10mm", `${str2}`);
            LODOP.SET_PRINT_STYLEA(0, "FontName", "SourceHanSansCN-Bold");
            //283
            LODOP.ADD_PRINT_TEXT(130, 290, "15mm", "10mm", `${str3}`);
            LODOP.SET_PRINT_STYLEA(0, "FontName", "SourceHanSansCN-Bold");
        }
    }

    //产品名称
    if (materialName) {
        LODOP.ADD_PRINT_TEXT(37, 8, "20mm", "10mm", "产品名称:");
        LODOP.ADD_PRINT_TEXT(37, 64, "45mm", 20, `${materialName}`);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
        LODOP.SET_PRINT_STYLEA(0, "LineSpacing", -4);
    }

    //二维码
    if (orderBillNo) {
        let options = {
            type: "image/jpeg", //类型
            quality: 1, //图片质量A Number between 0 and 1
            width: 49, //高度
            height: 49, //宽度
            errorCorrectionLevel: "H", //容错率
            margin: 0, //外边距
            color: {
                dark: "#000000", //前景色
                light: "#ffffff" //背景色
            }
        };

        QRCode.toString(orderBillNo, options, (error, string) => {
            if (error) {
                throw error;
            }
            // 获取 SVG 元素
            let svgElement = document.getElementById("qrcode-box");
            let qrSvgElement = document.getElementById("qrcode");
            qrSvgElement.innerHTML = string;
            imageCode = svgElement.innerHTML;
            LODOP.ADD_PRINT_IMAGE(42, 236, "100%", "100%", imageCode);
        });
    }
    //尾数图片
    if (isTail) {
        let weishuElement = `<div style="font-size:16px;color:#000;font-weight:bold;">尾数</div>
          </div>`;
        LODOP.ADD_PRINT_HTM(68, 295, "45mm", "100%", weishuElement);
        LODOP.SET_PRINT_STYLEA(0, "Angle", 30);
        LODOP.ADD_PRINT_ELLIPSE(48, 293, 40, 40, 1, 1);
    }
    //订单号
    LODOP.ADD_PRINT_TEXT(65, 8, "78mm", 10, `订单号:${orderBillNo}`);
    LODOP.SET_PRINT_STYLEA(0, "Overflow", 2);
    //装箱序号
    if (isTail) {
        LODOP.ADD_PRINT_TEXT(85, 8, "50mm", "10mm", `装箱序号:${boxSortCount + "/" + boxSortCount}`);
    } else {
        LODOP.ADD_PRINT_TEXT(85, 8, "50mm", "10mm", `装箱序号:${sort + "/" + boxSortCount}`);
    }

    //净重
    LODOP.ADD_PRINT_TEXT(85, 107, "30mm", "10mm", `净重:${isTail ? tailboxNetWeightQty : cratingNetWeightQty} KG`);
    //装箱数量

    //特殊处理:当可装箱数量大于产品数量时,要取产品数量
    if (cratingQty > productQty) {
        LODOP.ADD_PRINT_TEXT(107, 8, "50mm", "10mm", `装箱数量:${productQty} PCS`);
    } else {
        LODOP.ADD_PRINT_TEXT(107, 8, "50mm", "10mm", `装箱数量:${isTail ? tailboxQty : cratingQty} PCS`);
    }

    //毛重
    LODOP.ADD_PRINT_TEXT(107, 107, "30mm", "10mm", `毛重:${isTail ? tailboxGrossWeightQty : cratingGrossWeightQty} KG`);
    //日期
    let createTimeClone = createTime.substring(0, 10);
    LODOP.ADD_PRINT_TEXT(129, 8, "50mm", "10mm", `日期:${createTimeClone}`);

    logStr = `规格型号:【${specifications}】->订单:【${orderBillNo}】->条码:【${barCodeClone}】->条码图片路径:【${image64}】->序号:【${
        isTail ? boxSortCount + "/" + boxSortCount : sort + "/" + boxSortCount
    }】->二维码:【${imageCode}】`;
    logs.push(logStr);
}
