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

    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列码");
    LODOP.SET_PRINT_PAGESIZE(0, 600, 400);
    LODOP.SET_PRINT_STYLE("Bold", 1);
    LODOP.SET_PRINT_STYLE("FontName", "黑体");
    LODOP.SET_PRINT_STYLE("FontSize", 10);
    LODOP.On_Return_Remain = true;
    // 循环打印条码，每页打印两个 += 2
    for (let i = 0; i < list.length; i++) {
        // 添加一页
        LODOP.NewPage();
        // 打印第一个条码
        printContent(list[i], 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 printContent(item, code69Img, date) {
    const { specifications, serialNumber } = item;
    //69码
    if (code69Img) {
        const { image, str1, str2, str3 } = code69Img;
        LODOP.ADD_PRINT_IMAGE(16, 22, 180, 74, image); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //69文字
        LODOP.ADD_PRINT_TEXT(67, 21, 10, 10, `${str1}`);
        LODOP.ADD_PRINT_TEXT(67, 38, 96, 10, `${str2}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 6);
        LODOP.ADD_PRINT_TEXT(67, 118, 96, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 6);
    }

    //规格型号 border:1px solid red;
    if (specifications) {
        console.log(specifications); //${测试}
        const HTML = `<div
        style="text-align:center;width:210px;white-space: nowrap; overflow: hidden;font-size:13px;font-family:黑体;font-weight:900;">${specifications}</div>`;
        LODOP.ADD_PRINT_HTM(11, 4, 590, 10, HTML);
        LODOP.SET_PRINT_STYLEA(0, "FontName", "黑体");
    }
    //SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(82, 15, 198, 42, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(126, 14, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(126, 185, 60, 10, date);
    }
}
