//打印序列条码
import { getLodop } from "../print";
import { codeSN, set69Code, dateCreate, isInvalidEAN13 } from "./utils";
import { useMsg } from "@/hooks/useMsg";
export const sequenceCodePrint = (list, callBack) => {
    // 仅当69码存在且格式非法时，弹窗并终止 "6942229761912"
    console.log(isInvalidEAN13(list[0].barCode), "==isInvalidEAN13(list[0].barCode)=");
    if (isInvalidEAN13(list[0].barCode)) {
        useMsg("warning", `69码【${list[0].barCode}】格式非法（校验码错误），无法打印！`);
        return; // 终止后续打印逻辑
    }
    //list[0].barCode
    let code69Img = set69Code(list[0].barCode);

    let date = dateCreate();
    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列码");
    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);
    LODOP.On_Return_Remain = true;
    // 循环打印条码，每页打印两个
    for (let i = 0; i < list.length; i += 2) {
        // 添加一页
        LODOP.NewPage();
        // 打印第一个条码
        printLeftContent(list[i], code69Img, date);
        // 打印第二个条码，如果有的话
        if (i + 1 < list.length) {
            printRightContent(list[i + 1], code69Img, date);
        }
    }
    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, date) {
    const { specifications, serialNumber } = item;
    //69码
    if (code69Img) {
        const { image, str1, str2, str3 } = code69Img;
        // image
        LODOP.ADD_PRINT_IMAGE(10, 24, 130, 32, image); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //69文字
        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);
    }

    //规格型号
    if (specifications) {
        LODOP.ADD_PRINT_TEXT(4, 15, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
        LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
        console.log("到这里");
    }
    //SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(41, 16, 140, 24, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(65, 26, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(65, 125, 40, 10, date);
        LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    }
}
//右边打印
function printRightContent(item, code69Img, date) {
    const { specifications, serialNumber } = item;

    if (code69Img) {
        const { image, str1, str2, str3 } = code69Img;
        LODOP.ADD_PRINT_IMAGE(10, 182, 130, 32, image); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        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, 254, 74, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }

    //规格型号最多28个字
    if (specifications) {
        LODOP.ADD_PRINT_TEXT(4, 177, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
        LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    }

    //SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(41, 175, 140, 24, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(65, 182, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(65, 285, 40, 10, date);
    }
}
