//打印序列号
import { getLodop } from "../print.js";
import { useMsg } from "@/hooks/useMsg";
import { codeSN, dateCreate, set69Code, isInvalidEAN13 } from "./utils";

export const twoCodePrint = async (printData, callBack) => {
    // 仅当69码存在且格式非法时，弹窗并终止 "6942229761912"
    if (isInvalidEAN13(printData[0].barCode)) {
        useMsg("warning", `69码【${printData[0].barCode}】格式非法（校验码错误），无法打印！`);
        return; // 终止后续打印逻辑
    }

    let LODOP; //声明为全局变量
    LODOP = getLodop();
    LODOP.PRINT_INIT("打印序列号");
    LODOP.SET_PRINT_PAGESIZE(0, 800, 280, "");
    // let barCodeClone = "6954301173908";
    LODOP.SET_PRINT_STYLE("Bold", 1);
    LODOP.SET_PRINT_STYLE("FontName", "黑体");
    LODOP.SET_PRINT_STYLE("FontSize", 7); //list[0].barCode
    let code69Img = set69Code(printData[0].barCode);

    const filteredData = [];
    const usedSerialNumbers = new Set();

    for (let i = 0; i < printData.length; i++) {
        const current = printData[i];
        if (usedSerialNumbers.has(current.serialNumber)) {
            continue;
        }
        let foundMatch = false;
        for (let j = 0; j < printData.length; j++) {
            if (i !== j && current.twoSerialNumber === printData[j].serialNumber) {
                filteredData.push(current);
                usedSerialNumbers.add(printData[j].serialNumber);
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            filteredData.push(current);
        }
    }

    console.log(filteredData);

    console.log(filteredData, "=filteredData=");
    // 循环打印条码，每页打印两个
    for (let i = 0; i < filteredData.length; i += 2) {
        // 添加一页
        LODOP.NewPage();
        // 打印第一个条码
        printLeftContent(filteredData[i], code69Img);
        // 打印第二个条码，如果有的话
        if (i + 1 < filteredData.length) {
            printRightContent(filteredData[i + 1], code69Img);
        }
    }
    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) {
    const { serialNumber, specifications, twoSerialNumber } = item;
    //69码
    if (code69Img) {
        const { image, str1, str2, str3 } = code69Img;
        //去空
        LODOP.ADD_PRINT_IMAGE(13, 10, 130, 32, image); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //69文字
        LODOP.ADD_PRINT_TEXT(35, 9, 10, 10, `${str1}`);
        LODOP.ADD_PRINT_TEXT(35, 24, 74, 10, `${str2}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
        LODOP.ADD_PRINT_TEXT(35, 81, 74, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }
    //规格型号 左边
    if (specifications) {
        //上,左,内容宽度,内容高度
        LODOP.ADD_PRINT_TEXT(6, 11, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2);
        //border:1px solid red
        // const HTMLL = `<div
        // style="text-align:center;width:136px;white-space: nowrap; overflow: hidden;font-size:11px;font-family:黑体;font-weight:900;">${specifications}</div>`;
        // LODOP.ADD_PRINT_HTM(4, 4, "100%", 10, HTMLL);
    }
    //第一件SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(44, 8, 130, 20, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(65, 9, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(65, 110, 42, 10, dateCreate());
    }
    //第二件SN码和底部文字
    if (twoSerialNumber && twoSerialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(75, 8, 130, 20, '<img src="' + codeSN(twoSerialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(95, 9, 100, 10, twoSerialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(94, 110, 42, 10, dateCreate());
    }
}

//右边打印
function printRightContent(item, code69Img) {
    const { serialNumber, specifications, twoSerialNumber } = item;
    //69码
    if (code69Img) {
        const { image, str1, str2, str3 } = code69Img;
        //去空
        LODOP.ADD_PRINT_IMAGE(13, 169, 130, 32, image); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //69文字
        LODOP.ADD_PRINT_TEXT(35, 168, 10, 10, `${str1}`);
        LODOP.ADD_PRINT_TEXT(35, 185, 74, 10, `${str2}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
        LODOP.ADD_PRINT_TEXT(35, 242, 74, 10, `${str3}`);
        LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", 4);
    }

    //规格型号 右边
    if (specifications) {
        //上,左,内容宽度,内容高度
        LODOP.ADD_PRINT_TEXT(6, 168, "100%", 10, specifications);
        LODOP.SET_PRINT_STYLEA(0, "TextOverflow", 2); //border:1px solid red
        // const HTMLR = `<div
        // style="text-align:center;width:136px;white-space: nowrap; overflow: hidden;font-size:11px;font-family:黑体;font-weight:900;">${specifications}</div>`;
        // LODOP.ADD_PRINT_HTM(4, 158, "100%", 10, HTMLR);
    }

    //第一件SN码和底部文字
    if (serialNumber && serialNumber !== " ") {
        //SN码;
        LODOP.ADD_PRINT_IMAGE(44, 168, 130, 20, '<img src="' + codeSN(serialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(65, 169, 100, 10, serialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(65, 270, 40, 10, dateCreate());
    }
    //第二件SN码和底部文字
    if (twoSerialNumber && twoSerialNumber !== " ") {
        //SN码
        LODOP.ADD_PRINT_IMAGE(75, 168, 130, 20, '<img src="' + codeSN(twoSerialNumber) + '">'); // 添加图片
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        //底部文字(SN码)
        LODOP.ADD_PRINT_TEXT(95, 169, 100, 10, twoSerialNumber);
        //底部文字(日期)
        LODOP.ADD_PRINT_TEXT(95, 270, 40, 10, dateCreate());
    }
}
