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

import { codeSN, dateCreate } from "./utils";
export const serialNumberPrint = async (list, callBack) => {
    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列号");
    LODOP.SET_PRINT_PAGESIZE(0, 875, 100, "");
    // LODOP.SET_PRINT_MARGIN(10, 10, 10, 10);
    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 { serialNumber } = item;
    // //SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        console.log("走到了这里,开新");
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 16, 140, 20, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(27, 26, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(27, 125, 40, 10, dateCreate());
    }
}
//右边打印
function printRightContent(item) {
    const { serialNumber } = item;
    //SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(7, 175, 140, 20, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        // 底部文字(SN码);
        LODOP.ADD_PRINT_TEXT(27, 182, 100, 10, serialNumber);
        //底部文字(日期);
        LODOP.ADD_PRINT_TEXT(27, 285, 40, 10, dateCreate());
    }
}
