//打印箱码
import { getLodop } from "../print.js";
import { codeSN } from "../barCode/utils";

export const boxCodePrint = async (list, callBack) => {
    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", 10);

    // 循环打印条码，每页打印两个
    for (let i = 0; i < list.length; i += 2) {
        // 添加一页
        LODOP.NewPage();
        // 打印第一个条码
        printLeftContent(list[i]);
        // 打印第二个条码，如果有的话
        if (i + 1 < list.length) {
            printRightContent(list[i + 1]);
        }
    }
    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) {
    const { boxBillNo } = item;
    //SN码和底部文字
    if (boxBillNo && boxBillNo !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 17, 140, 50, '<img src="' + codeSN(boxBillNo) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        // LODOP.ADD_PRINT_TEXT(62, 52, 100, 10, boxBillNo);
        let element = `<div style="overflow: hidden;white-space: nowrap;width: 144px;text-align: center;border:font-size:12px;font-weight: bold;font-family:'黑体';">${boxBillNo}</div>`;
        LODOP.ADD_PRINT_HTM(54, 17, 140, 12, element);
    }
}
//右边打印
function printRightContent(item) {
    const { boxBillNo } = item;
    //SN码和底部文字
    if (boxBillNo && boxBillNo !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 177, 140, 50, '<img src="' + codeSN(boxBillNo) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        //  / LODOP.ADD_PRINT_TEXT(62, 212, 100, 10, boxBillNo);
        let element = `<div style="overflow: hidden;white-space: nowrap;width: 144px;text-align: center;border:font-size:12px;font-weight: bold;font-family:'黑体'">${boxBillNo}</div>`;

        LODOP.ADD_PRINT_HTM(54, 177, 140, 12, element);
    }
}
