//打印数字序列号
import { getLodop } from "../print.js";
import { codeSN } from "./utils";

export const numberCodePrint10 = async (list, callBack) => {
    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列码");
    LODOP.SET_PRINT_STYLE("GraphicsMode", 0);
    LODOP.SET_PRINT_PAGESIZE(0, 875, 100, "A4");
    LODOP.SET_PRINT_STYLE("Bold", 1);
    LODOP.SET_PRINT_STYLE("FontName", "黑体");
    LODOP.SET_PRINT_STYLE("FontSize", 7);
    // 循环打印条码，每页打印两个
    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 { numberCode } = item;
    //SN码和底部文字
    if (numberCode && numberCode !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 16, 140, 20, '<img src="' + codeSN(numberCode) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(27, 35, 140, 10, numberCode);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
}
//右边打印
function printRightContent(item) {
    const { numberCode } = item;
    //SN码和底部文字
    if (numberCode && numberCode !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 175, 140, 20, '<img src="' + codeSN(numberCode) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(27, 195, 140, 10, numberCode);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
}
