var imgSrc =
    "data:image/png;base64, ";
/**
 * @type {import("../../lib/dtpweb").DTPWeb}
 */
var api = dtpweb.getInstance({
    // jsonMode: false,
});
//
window.onload = function () {
    // 检测打印接口是否可用
    api.checkPlugin(function (success) {
        if (success) {
            updatePrinterList();
        } else {
            api = undefined;
            alert("未检测到打印机插件！");
        }
    });
    // 相关UI的响应处理函数
    document.getElementById("select-printlist").onchange = function () {
        onPrinterChanged();
    };
    document.getElementById("select-gaptype").onchange = function () {
        onGapTypeChanged();
    };
    document.getElementById("select-printspeed").onchange = function () {
        onPrintSpeedChanged();
    };
    document.getElementById("select-printdarkness").onchange = function () {
        onPrintDarknessChanged();
    };
    //
    document.getElementById("test-open-printer").onclick = function () {
        onOpenPrinter();
    };
    document.getElementById("test-close-printer").onclick = function () {
        onClosePrinter();
    };
    document.getElementById("test-is-printer-opened").onclick = function () {
        onIsPrinterOpened();
    };
    document.getElementById("test-get-printer-name").onclick = function () {
        onGetPrinterName();
    };
    document.getElementById("test-get-printer-dpi").onclick = function () {
        onGetPrinterDpi();
    };
    document.getElementById("test-show-property").onclick = function () {
        onShowProperty();
    };
    //
    document.getElementById("test-draw-text").onclick = function () {
        drawTextTest();
    };
    document.getElementById("test-alignment").onclick = function () {
        drawItemAlignmentTest();
    };
    document.getElementById("test-rotation").onclick = function () {
        drawItemRotationTest();
    };
    document.getElementById("test-draw-barcode").onclick = function () {
        drawBarcodeTest();
    };
    document.getElementById("test-draw-qrcode").onclick = function () {
        drawQRCodeTest();
    };
    document.getElementById("test-draw-pdf417").onclick = function () {
        drawPDF417Test();
    };
    document.getElementById("test-draw-dtmx").onclick = function () {
        drawDataMatrixTest();
    };
    document.getElementById("test-draw-rect").onclick = function () {
        drawRectTest();
    };
    document.getElementById("test-draw-ellipse").onclick = function () {
        drawEllipseTest();
    };
    document.getElementById("test-draw-circle").onclick = function () {
        drawCircleTest();
    };
    document.getElementById("test-draw-line").onclick = function () {
        drawLineTest();
    };
    document.getElementById("test-draw-image-url").onclick = function () {
        drawImageUrlTest();
    };
    document.getElementById("test-draw-image-data").onclick = function () {
        drawImageDataTest();
    };
    document.getElementById("test-print-image-data").onclick = function () {
        printImageDataTest();
    };
    document.getElementById("test-print-json").onclick = function () {
        printJsonTest();
    };
    // document.getElementById("test-draw-ticket").onclick = function () {
    //     drawTicketTest();
    // };
};

//#region 获取打印机列表

/**
 * 更新打印机列表。
 */
function updatePrinterList() {
    var printerElements = document.getElementById("select-printlist");

    // 为了避免打印的时候，数据打印不完全的问题，js接口中采用的是ajax同步请求方式；
    // 为了避免服务未打开的时候，调用接口时出现假死状态，在合适的地方调用皆苦前最好先检测下url是否可用；
    if (api) {
        var printers = api.getPrinters({ onlyLocal: false });
        // 先清空当前打印机列表
        printerElements.innerHTML = "";
        // 重新添加打印机列表
        if (printers instanceof Array && printers.length > 0) {
            for (var i = 0; i < printers.length; i++) {
                var item = printers[i];
                var shownName = item.deviceName || item.name;
                // 如果检测到局域网内的其他打印机，则可以获取ip和hostname，如果是本地打印机，则参数中只有name属性，表示打印机名称；
                var name = api.isLocalPrinter(item) ? shownName : shownName + "@" + item.hostname;
                var value = api.isLocalPrinter(item) ? item.name : item.name + "@" + item.ip;
                printerElements.options.add(new Option(name, value));
            }
        } else {
            printerElements.options.add(new Option("未检测到打印机", ""));
        }
        // 链接默认打印机，并更新选中打印机打印参数；
        // onPrinterChanged();
    }
}

/**
 * 获取当前选中的打印机；
 */
function getCurrPrinter() {
    var printerElement = document.getElementById("select-printlist");
    if (!printerElement.value) return {};

    var arr = printerElement.value.split("@");
    return {
        name: arr[0],
        ip: arr[1],
    };
}

//#endregion

/**
 * 打开当前打印机；
 */
function openPrinter(callback) {
    var printer = getCurrPrinter();
    if (printer.name && api) {
        api.openPrinter(printer, callback);
    } else if (callback) {
        callback(false);
    }
}

function onOpenPrinter() {
    var printer = getCurrPrinter();
    if (!printer.name) {
        alert("未检测到打印机");
        return false;
    }
    api.openPrinter(printer, function (success) {
        if (success) {
            alert("打印机打开成功");
        } else {
            alert("打印机打开失败");
        }
    });
}

/**
 * 关闭已连接打印机；
 */
function onClosePrinter() {
    api.closePrinter();
}

/**
 * 当打印机更新后，同步的更新当前打印机的相关打印参数；
 */
function onPrinterChanged() {
    openPrinter(function (success) {
        if (success) {
            var gapTypeSelect = document.getElementById("select-gaptype");
            gapTypeSelect.value = api.getGapType();

            var printSpeed = document.getElementById("select-printspeed");
            printSpeed.value = api.getPrintSpeed();

            var printDarkness = document.getElementById("select-printdarkness");
            printDarkness.value = api.getPrintDarkness();
            // 使用完毕后，关闭打印机，避免占用打印机，影响其他用户的使用；
            api.closePrinter();
        }
    });
}

/**
 * 修改当前打印机的纸张类型；
 * 打印机打开成功后调用有效；
 */
function onGapTypeChanged() {
    // var gapTypeSelect = document.getElementById("select-gaptype");
    // api.setGapType(gapTypeSelect.value * 1);
}
function getGapType() {
    var gapTypeSelect = document.getElementById("select-gaptype");
    return gapTypeSelect.value * 1;
}

/**
 * 修改当前打印机的打印速度；
 * 打印机打开成功后调用有效；
 */
function onPrintSpeedChanged() {
    // var printSpeed = document.getElementById("select-printspeed");
    // api.setPrintSpeed(printSpeed.value * 1);
}
function getPrintSpeed() {
    var printSpeed = document.getElementById("select-printspeed");
    return printSpeed.value * 1;
}

/**
 * 修改当前选中打印机的打印浓度；
 * 打印机打开成功后调用有效；
 */
function onPrintDarknessChanged() {
    // var printDarkness = document.getElementById("select-printdarkness");
    // api.setPrintDarkness(printDarkness.value * 1);
}
function getPrintDarkness() {
    var printDarkness = document.getElementById("select-printdarkness");
    return printDarkness.value * 1;
}

/**
 * 获取当前选中的任务类型值；
 * 0 ：表示当前的打印任务为打印任务；
 * 1 ： 表示当前的打印任务为白底预览任务；
 * 2 ： 表示当前的打印任务为透明底色的预览任务；
 */
function getJobTypeValue() {
    return document.getElementById("select-print-preview").value;
}

/**
 * 获取当前打印任务名称；
 */
function getJobName(jobTypeValue, defJobName) {
    var value = typeof jobTypeValue === "number" ? jobTypeValue : getJobTypeValue();
    if (value === "1")
        // 白色底色
        return "#!#Preview#!#";
    else if (value === "2")
        // 透明底色
        return "#!#Transparent#!#";
    else return defJobName || "DTPWeb"; // 打印任务名称，随便写；
}
function getJobAction() {
    var value = typeof jobTypeValue === "number" ? jobTypeValue : getJobTypeValue();
    if (value === "1") return 0x02;
    else if (value === "2") return 0x82;
    else return 0x1000;
}
/**
 * 判断当前任务是不是打印任务。
 * true : 表示当前任务是打印任务；
 * false: 表示当前任务是预览任务；
 */
function isPrintJob() {
    var jobType = getJobTypeValue();
    return jobType !== "1" && jobType !== "2";
}

/**
 * 打印线条相关对象。
 */
function drawLineTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var lineSpace = 5;
            var height = lineSpace * 4;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var lineWidth = 0.5;

            // 开始打印任务；
            api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName });
            //
            api.drawLine({ y1: 5, x2: 45, y2: 5, lineWidth: lineWidth });
            api.drawLine({ y1: 10, x2: 45, y2: 10, lineWidth: lineWidth, dashLens: [0.5] });
            api.drawLine({ y1: 15, x2: 45, y2: 15, lineWidth: lineWidth, dashLens: [0.75, 0.5] });
            // 提交打印任务；
            api.commitJob({
                gapType: getGapType(),
                darkness: getPrintDarkness(),
                speed: getPrintSpeed(),
                callback: function (res) {
                    // 则显示预览效果
                    previewJobResult(res);
                },
            });
        }
    });
}

/**
 * 打印矩形框相关对象。
 */
function drawRectTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var padding = 2;

            // 创建打印任务
            api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName });
            api.startPage();
            // 第一页，打印矩形框
            api.drawRectangle({ width: width, height: height });
            // 打印填充矩形
            api.drawRectangle({
                x: padding,
                y: padding,
                width: width - padding * 2,
                height: height - padding * 2,
                fill: true,
            });
            api.endPage();

            // 第二页，打印圆角矩形框
            api.startPage();
            api.drawRectangle({
                width: width,
                height: height,
                cornerWidth: 3.0,
                cornerHeight: 3.0,
                lineWidth: 0.5,
            });
            api.drawRectangle({
                x: padding,
                y: padding,
                width: width - padding * 2,
                height: height - padding * 2,
                cornerWidth: 2,
                cornerHeight: 2,
                fill: true,
            });
            api.endPage();

            // 提交打印任务
            api.commitJob({
                gapType: getGapType(),
                darkness: getPrintDarkness(),
                speed: getPrintSpeed(),
                callback: function (res) {
                    // 则显示预览效果
                    previewJobResult(res);
                },
            });
        }
    });
}

/**
 * 打印椭圆相关对象。
 */
function drawEllipseTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var padding = 2;

            // 创建打印任务。
            api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName });
            // 打印椭圆边框
            api.drawEllipse({ width: width, height: height, lineWidth: 0.5 });
            // 打印填充椭圆
            api.drawEllipse({
                x: padding,
                y: padding,
                width: width - padding * 2,
                height: height - padding * 2,
                fill: true,
            });

            // 提交打印任务。
            api.commitJob({
                gapType: getGapType(),
                darkness: getPrintDarkness(),
                speed: getPrintSpeed(),
                callback: function (res) {
                    // 则显示预览效果
                    previewJobResult(res);
                },
            });
        }
    });
}

/**
 * 打印椭圆相关对象。
 */
function drawCircleTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 30;
            var height = 30;
            var centerX = 15;
            var centerY = 15;
            var radius = 15;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var padding = 2;

            // 创建打印任务。
            api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName });
            // 打印椭圆边框
            api.drawCircle({ x: centerX, y: centerY, radius: radius });
            // 打印填充椭圆
            api.drawCircle({ x: centerX, y: centerY, radius: radius - padding, fill: true });

            // 提交打印任务。
            api.commitJob({
                gapType: getGapType(),
                darkness: getPrintDarkness(),
                speed: getPrintSpeed(),
                callback: function (res) {
                    // 则显示预览效果
                    previewJobResult(res);
                },
            });
        }
    });
}

/**
 * 打印文本相关对象。
 */
function drawTextTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 40;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var fontHeight = 5;
            var text = "@上海道臻信息技术有限公司#";
            // var text = "www.dothatnech.com";

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.drawText({ text: text, width: width, height: height, fontHeight: fontHeight });
                //
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

/**
 * 绘制一维码。
 */
function drawBarcodeTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var text = "1234567890";
            var margin = 5;

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.draw1DBarcode({
                    text: text,
                    x: margin,
                    y: margin,
                    width: width - margin * 2,
                    height: height - margin * 2,
                    textHeight: 5,
                });
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

/**
 * 绘制二维码。
 */
function drawQRCodeTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 45;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var margin = 5;
            var text = "上海道臻信息技术有限公司";
            // var text = "www.dothantech.com";

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.draw2DQRCode({
                    text: text,
                    x: margin,
                    y: margin,
                    width: width - margin * 2,
                });
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

/**
 * 打印PDF417。
 */
function drawPDF417Test() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var text = "上海道臻信息技术有限公司";
            // var text = "www.dothantech.com";
            var margin = 5;

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.draw2DPdf417({
                    text: text,
                    x: margin,
                    y: margin,
                    width: width - margin * 2,
                    height: height - margin * 2,
                });

                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

function drawDataMatrixTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 40;
            var height = 40;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            // var text = '上海道臻信息技术有限公司';
            var text = "www.dothantech.com";
            var margin = 5;

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.draw2DDataMatrix({
                    text: text,
                    x: margin,
                    y: margin,
                    width: width - margin * 2,
                    height: height - margin * 2,
                });

                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

/**
 * 打印网络图片。
 */
function drawImageUrlTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 30;
            var height = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var url = "http://www.detonger.com/img/QRCode_OfficialAccounts.png";
            var margin = 5;
            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                //
                api.drawImage({
                    imageFile: url,
                    x: margin,
                    y: margin,
                    width: width - margin * 2,
                    height: height - margin * 2,
                });
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}

function drawImageDataTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var labelWidth = 30;
            var labelHeight = 30;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();

            if (api.startJob({ width: labelWidth, height: labelHeight, orientation: orientation, jobName: jobName })) {
                //
                api.drawImageD({
                    data: imgSrc,
                    drawWidth: labelWidth,
                    drawHeight: labelHeight,
                });
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}
function drawItemAlignmentTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 40;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var fontHeight = 4;
            var itemWidth = width / 2;
            var itemHeight = height / 2;
            var text1 = "水平居左对齐，垂直居上对齐";
            var text2 = "水平居中对齐，垂直居中对齐";
            var text3 = "水平居右对齐，垂直居下对齐";
            var text4 = "水平拉伸对齐，垂直拉伸对齐";

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                // 将正张标签分成四个区域，分别对应不同的对齐方式
                api.drawRectangle({ width: width, height: height });
                api.drawLine({ x1: 0, x2: width, y1: itemHeight });
                api.drawLine({ y1: 0, y2: height, x1: itemWidth });
                // 左对齐，上对齐
                api.drawText({
                    text: text1,
                    x: 0,
                    y: 0,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    fontStyle: 0,
                    horizontalAlignment: 0,
                    verticalAlignment: 0,
                });
                // 水平居中，垂直居中
                api.drawText({
                    text: text2,
                    x: itemWidth,
                    y: 0,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    fontStyle: 1,
                    horizontalAlignment: 1,
                    verticalAlignment: 1,
                });
                // 水平居右，垂直居右
                api.drawText({
                    text: text3,
                    x: itemWidth,
                    y: itemHeight,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    fontStyle: 2,
                    horizontalAlignment: 2,
                    verticalAlignment: 2,
                });
                // 水平拉伸，垂直拉伸
                api.drawText({
                    text: text4,
                    x: 0,
                    y: itemHeight,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    fontStyle: 3,
                    horizontalAlignment: 3,
                    verticalAlignment: 3,
                });
                //
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}
function drawItemRotationTest() {
    // 打开打印机
    openPrinter(function (success) {
        if (!isPrintJob() || success) {
            var width = 45;
            var height = 40;
            var orientation = document.getElementById("select-orientation").value * 90;
            var jobName = getJobName();
            var fontHeight = 4;
            var text = "@上海道臻信息技术有限公司#";
            // var text = "www.dothatnech.com";
            var itemWidth = width / 2;
            var itemHeight = height / 2;

            if (api.startJob({ width: width, height: height, orientation: orientation, jobName: jobName })) {
                // 将正张标签分成四个区域，分别对应不同的对齐方式
                api.drawRectangle({ width: width, height: height });
                api.drawLine({ x1: 0, x2: width, y1: itemHeight });
                api.drawLine({ y1: 0, y2: height, x1: itemWidth });
                // 左对齐，上对齐
                api.drawText({
                    text: text,
                    x: 0,
                    y: 0,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    orientation: 0,
                });
                // 水平居中，垂直居中
                api.drawText({
                    text: text,
                    x: itemWidth,
                    y: 0,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    orientation: 90,
                });
                // 水平居右，垂直居右
                api.drawText({
                    text: text,
                    x: itemWidth,
                    y: itemHeight,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    orientation: 180,
                });
                // 水平拉伸，垂直拉伸
                api.drawText({
                    text: text,
                    x: 0,
                    y: itemHeight,
                    width: itemWidth,
                    height: itemHeight,
                    fontHeight: fontHeight,
                    orientation: 270,
                });
                //
                api.commitJob({
                    gapType: getGapType(),
                    darkness: getPrintDarkness(),
                    speed: getPrintSpeed(),
                    callback: function (res) {
                        // 则显示预览效果
                        previewJobResult(res);
                    },
                });
            }
        }
    });
}
/**
 * 小票纸打印场景演示。
 */
function drawTicketTest() {
    console.log("### print ticket ###");
}

/**
 * 直接打印BASE64图片测试。
 */
function printImageDataTest() {
    var printer = getCurrPrinter();
    var printerName = printer.name;
    //
    if (!api || !printerName) return;
    //
    var datas = getPreviewList();
    for (var i = 0; i < datas.length; i++) {
        if (datas[i].src) {
            api.printImage({
                data: datas[i].src,
                printerName: printerName,
            });
        }
    }
}
/**
 * JSON格式数据打印测试。
 */
function printJsonTest() {
    var printer = getCurrPrinter();
    var printerName = printer ? printer.name : undefined;
    if (isPrintJob() && !printerName) return;
    //
    var printAction = getJobAction();
    var labelWidth = 40;
    var labelHeight = 30;
    // var url = 'http://www.detonger.com/img/QRCode_OfficialAccounts.png';
    // var text1 = '上海道臻信息技术有限公司';
    // var text2 = '1234567';
    //
    api.print({
        action: printAction,
        printerInfo: {
            printerName: printer.name,
        },
        jobInfo: { jobWidth: labelWidth, jobHeight: labelHeight, orientation: 0 },
        jobPages: [
            // 第一张标签，外边框里面套个二维码
            [
                { type: "rectangle", width: labelWidth, height: labelHeight, lineWidth: 0.4 },
                // { type: 'roundRectangle', width: labelWidth, height: labelHeight, lineWidth: 0.4, cornerWidth: 2 },
                // { type: 'text', text: text1, width: labelWidth, height: labelHeight, fontHeight: 4 },
                {
                    type: "barcode",
                    text: "12345678",
                    x: 5,
                    y: 5,
                    width: 30,
                    height: 20,
                    textHeight: 4,
                    textAlignment: 3,
                },
                // { type: 'qrcode', text: text1, x: 10, y: 5, width: 20, eccLevel: 1 },
                // { type: 'pdf417', text: text2, x: 5, y: 5, width: 30, height: 20 },
                // // 绘制图片url
                // { type: 'image', imageFile: url, x: 10, y: 5, width: 20, height: 20 },
                // // 绘制BASE64字符串
                // { type: 'image', imageFile: imgSrc, x: 10, y: 5, width: 20, height: 20 },
                // { type: "line", x1: 0, y1: 5, x2: labelWidth, y2: 5, lineWidth: 0.4 },
                // { type: "dashLine", x1: 0, y1: 10, x2: labelWidth, y2: 10, lineWidth: 0.4, dashLen: "1,0.5" },
            ],
        ],
        callback: function (results) {
            // 显示预览效果
            if (printAction !== 0x1000 && results) {
                previewJobResult(results);
            }
        },
    });
}

// 打印机属性判断

function onGetPrinterName() {
    if (!api.isPrinterOpened()) {
        alert("打印机未打开");
        return false;
    }
    //
    alert(api.getPrinterName());
}
function onGetPrinterDpi() {
    if (!api.isPrinterOpened()) {
        alert("打印机未打开");
        return false;
    }
    //
    var dpi = api.getPrinterDPI();
    alert(dpi ? JSON.stringify(dpi) : dpi);
}

function onIsPrinterOpened() {
    alert(api.isPrinterOpened());
}

var toggleTag = false;
/**
 * 打开打印机属性对框框测试；
 */
function onShowProperty() {
    var printer = getCurrPrinter();
    if (!printer) {
        alert("未检测到打印机");
        return;
    }
    api.showProperty({
        showDocument: toggleTag,
        printerName: printer.name,
    });
    // 切换 toggle状态，分别测试 showDocument为true和false；
    toggleTag = !toggleTag;
}

//#region 打印任务测试

/**
 * 获取当前任务的图片信息；
 */
function showJobPages(pages) {
    if (isPrintJob()) return;
    //
    if (pages && pages.length > 0) {
        for (var i = 0; i < pages.length; i++) {
            addPreview(pages[i]);
        }
    }
}
function previewJobResult(res) {
    if (res && res.previewData) {
        // 先清空预览区域；
        clearPreview();
        //
        showJobPages(res.previewData);
    }
}

/**
 * 清空预览区域；
 */
function clearPreview() {
    document.getElementById("preview-list").innerHTML = "";
}

/**
 * 项预览区域添加预览图片；
 */
function addPreview(data) {
    if (!data) return;

    var previewGroup = document.getElementById("preview-list");
    var img = document.createElement("img");
    img.src = data;
    previewGroup.appendChild(img);
    // 换行
    previewGroup.appendChild(document.createElement("br"));
}

/**
 * 获取 #preview-list中所有img的src；
 */
function getPreviewList() {
    var previewGroup = document.getElementById("preview-list");
    var pageList = [];
    for (var i = 0; i < previewGroup.children.length; i++) {
        var imgElement = previewGroup.children[i];
        pageList.push(imgElement);
    }

    return pageList;
}

//#endregion
