//打印序列码
import { getLodop } from "../print.js";
import { set69Code, isInvalidEAN13 } from "./utils";
import { useMsg } from "@/hooks/useMsg";
export const numberCodePrint = async (list, callBack) => {
    // 仅当69码存在且格式非法时，弹窗并终止 "6942229761912"
    if (isInvalidEAN13(list[0].barCode)) {
        useMsg("warning", `69码【${list[0].barCode}】格式非法（校验码错误），无法打印！`);
        return; // 终止后续打印逻辑
    }
    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列码");
    LODOP.SET_PRINT_STYLE("GraphicsMode", 0);
    LODOP.SET_PRINT_PAGESIZE(0, 875, 200, "A4");
    LODOP.SET_PRINT_STYLE("Bold", 1);
    LODOP.SET_PRINT_STYLE("FontName", "黑体");
    LODOP.SET_PRINT_STYLE("FontSize", 7);
    let code69Img = set69Code(list[0].barCode);

    // 获取 SVG 元素用于生成EAN13码
    let svgElement = "";
    if (list[0].barCode && list[0].barCode !== " " && list[0].barCode.length === 13) {
        svgElement = document.getElementById("barcode-box2");
        JsBarcode("#barcode2", list[0].barCode, {
            format: "EAN13", // 指定条形码格式
            width: 3, //1.5
            height: 40,
            displayValue: false //不显示条形码下面的文字
        });
    }

    // 循环打印条码，每页打印两个
    for (let i = 0; i < list.length; i += 2) {
        // 添加一页
        LODOP.NewPage();
        // 打印第一个条码
        printLeftContent(list[i], code69Img, svgElement);
        // 打印第二个条码，如果有的话
        if (i + 1 < list.length) {
            printRightContent(list[i + 1], code69Img, svgElement);
        }
    }
    LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
    LODOP.PREVIEW();
    if (LODOP.CVERSION) {
        LODOP.On_Return = function (TaskID, Value) {
            console.log(TaskID, Value);
            if (Value) {
                callBack && callBack();
            }
        };
    }
};

//左边打印
function printLeftContent(item, code69Img, svgElement) {
    const { specifications, numberCode } = item;
    let str1 = "";
    let str2 = "";
    let str3 = "";
    if (code69Img) {
        str1 = code69Img.str1;
        str2 = code69Img.str2;
        str3 = code69Img.str3;
    }

    //69码
    if (str1) {
        LODOP.ADD_PRINT_IMAGE(10, 25, 130, 32, svgElement.innerHTML);
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
    }
    //规格型号
    if (specifications) {
        LODOP.ADD_PRINT_TEXT(4, 15, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
    }
    //SN码和底部文字
    if (numberCode && numberCode !== " ") {
        //SN码;
        JsBarcode("#barcode3", numberCode, {
            format: "code128b", // 指定条形码格式
            width: 3, //1.5
            height: 40,
            displayValue: false //不显示条形码下面的文字
        });
        let svgElement1 = document.getElementById("barcode-box3");
        LODOP.ADD_PRINT_IMAGE(37, 16, 140, 32, svgElement1.innerHTML);
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(66, 35, 140, 10, numberCode);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
    //69文字
    if (str1) {
        LODOP.ADD_PRINT_TEXT(32, 22, 10, 10, `${str1}`);
        LODOP.ADD_PRINT_TEXT(32, 37, 74, 10, `${str2}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
        LODOP.ADD_PRINT_TEXT(32, 95, 74, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
}

//右边打印
function printRightContent(item, code69Img, svgElement) {
    const { specifications, numberCode } = item;
    let str1 = "";
    let str2 = "";
    let str3 = "";
    if (code69Img) {
        str1 = code69Img.str1;
        str2 = code69Img.str2;
        str3 = code69Img.str3;
    }

    if (str1) {
        LODOP.ADD_PRINT_IMAGE(10, 182, 130, 32, svgElement.innerHTML);
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
    }
    //规格型号最多28个字
    if (specifications) {
        LODOP.ADD_PRINT_TEXT(4, 177, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
    }

    //SN码和底部文字
    if (numberCode && numberCode !== " ") {
        JsBarcode("#barcode4", numberCode, {
            format: "code128b", // 指定条形码格式
            width: 3, //1.5
            height: 40,
            displayValue: false //不显示条形码下面的文字
        });
        let svgElement1 = document.getElementById("barcode-box4");
        LODOP.ADD_PRINT_IMAGE(37, 175, 140, 32, svgElement1.innerHTML);
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(66, 195, 140, 10, numberCode);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
    //69文字
    if (str1) {
        LODOP.ADD_PRINT_TEXT(32, 180, 10, 10, `${str1}`);
        LODOP.ADD_PRINT_TEXT(32, 194, 74, 10, `${str2}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
        LODOP.ADD_PRINT_TEXT(32, 252, 74, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
}
