<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('保安员证书打印')"/>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: lodop"/>
</head>
<body style="text-align: center;padding-top: 20px;font-weight: bold;font-size: 20px">
</body>
<script th:inline="javascript">
    var LODOP = null;
    var printState = 0;
    var host = "http://" + window.location.host;
    var detail =[
            [# th:each="printData : ${printDatas}"]
        [[${printData}]],
        [/]
    ];
    console.log(detail.length);
    $(function () {
        LODOP = getLodop();
        if (LODOP != null) {
            setTimeout("lodopPrint()", 500);
        }
    });

    function lodopPrint() {
        document.body.innerText = "打印中...";
        //打印前变量赋值为0 add start yyx 2020-01-16
        printState = 0;
        LODOP.PRINT_INITA("0mm", 0, "210mm", "297mm", "陕西保安员证书打印");
        LODOP.SET_PRINT_MODE("BKIMG_IN_PREVIEW", 1);
        LODOP.ADD_PRINT_SETUP_BKIMG("<img border='0' src='" + host +  detail[0].securityPic + "'+>");
        LODOP.SET_SHOW_MODE("BKIMG_LEFT", 0);
        LODOP.SET_SHOW_MODE("BKIMG_TOP", 0);
        LODOP.SET_SHOW_MODE("BKIMG_WIDTH", "210mm");
        LODOP.SET_SHOW_MODE("BKIMG_HEIGHT", "297mm");
        LODOP.SET_SHOW_MODE("BKIMG_IN_PREVIEW", true);
        LODOP.SET_SHOW_MODE("BKIMG_PRINT", true);

        for (i = 0; i < detail.length; i += 2) {
            if(detail[i] != null && typeof(detail[i] != 'undefined')) {
                LODOP.NewPage();
                LODOP.ADD_PRINT_TEXT(260 + 36, 517, 133, 20, detail[i].xm);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.ADD_PRINT_TEXT(370, 518, 171, 56, detail[i].address == null ? '' : detail[i].address.substring(0, 9) + "\r\n" + detail[i].address.substring(9, detail[i].address.length));
                LODOP.SET_PRINT_STYLEA(2,"ReadOnly",0);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", 3);
                LODOP.ADD_PRINT_TEXT(334, 517, 163, 20, changeBirthFormat(getBirthdayFromIDNo(detail[i].idnum)));
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(462, 541, 239, 20, detail[i].idnum);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(360, 223, 200, 20, (detail[i].fzrq == null ? "" : changeBirthFormat(detail[i].fzrq.substring(0, 10))));
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(462, 207, 186, 20, detail[i].zsbh);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -2);
                LODOP.ADD_PRINT_TEXT(145, 168, 111, 20, detail[i].xm);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
                //照片
                if(detail[i].photo != null && detail[i].photo !="") {
                    LODOP.ADD_PRINT_IMAGE(138, 488, "26.00mm", "32.00mm", detail[i].photo);//+1
                }
                LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
                //二维码50
                LODOP.ADD_PRINT_IMAGE(182, 282, 99, 99,detail[i].qrcode);
                LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
            }

            if (detail[i + 1] != null && typeof(detail[i + 1] != 'undefined')) {
                LODOP.ADD_PRINT_TEXT(778, 517, 133, 20, detail[i + 1].xm);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.ADD_PRINT_TEXT(816, 518, 163, 20, changeBirthFormat(getBirthdayFromIDNo(detail[i + 1].idnum)));
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(852, 518, 171, 56, detail[i + 1].address == null ? '' : detail[i + 1].address.substring(0, 9) + "\r\n" + detail[i + 1].address.substring(9, detail[i + 1].address.length));
                LODOP.SET_PRINT_STYLEA(12,"ReadOnly",0);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LineSpacing", 3);
                LODOP.ADD_PRINT_TEXT(945, 541, 239, 20, detail[i + 1].idnum);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(627, 168, 111, 20, detail[i + 1].xm);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.ADD_PRINT_TEXT(842, 223, 200, 20, (detail[i + 1].fzrq == null ? "" : changeBirthFormat(detail[i + 1].fzrq.substring(0, 10))));
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -1);
                LODOP.ADD_PRINT_TEXT(945, 204, 186, 20, detail[i + 1].zsbh);
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
                LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
                LODOP.SET_PRINT_STYLEA(0, "LetterSpacing", -2);
                //照片
                if(detail[i+1].photo != null && detail[i+1].photo !="") {
                    LODOP.ADD_PRINT_IMAGE(617, 488, "26.00mm", "32.00mm",detail[i + 1].photo);
                }
                LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);//二维码
                //二维码50
                LODOP.ADD_PRINT_IMAGE(666, 282, 99, 99,detail[i + 1].qrcode);
                LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
            }
        }
        //直接打印返回逻辑真假结果（1或true代表真，false或0代表假），表示是否成功（关于进一步捕获打印状态参考样例45）；
        //打印维护和打印预览返回打印次数，大于0表示打印成功的次数；
        //谷歌浏览器
        if (LODOP.CVERSION) {
            CLODOP.On_Return = function (TaskID, Value) {
                //打印状态
                printState = Value;
                if(printState >= 1) {
                    document.body.innerText="打印完成";
                    recordsGenerated(true);
                }else{
                    document.body.innerText="已取消打印";
                    recordsGenerated(false);
                }
            };
        }
        printState = LODOP.PRINT_SETUP();
        //非谷歌
        if (!LODOP.CVERSION) {
            if(printState >= 1) {
                document.body.innerText="打印完成";
                recordsGenerated(true);
            }else{
                document.body.innerText="已取消打印";
                recordsGenerated(false);
            }
        }
    }

    /***
     * 出生日期
     * @param birthday
     * @returns {string}
     */
    function changeBirthFormat(birthday) {

        var birthdayArr = birthday.split("-");
        if (birthdayArr[1].substring(0, 1) == "0") {
            birthdayArr[1] = birthdayArr[1].substring(1, birthdayArr[1].length);
        }
        if (birthdayArr[2].substring(0, 1) == "0") {
            birthdayArr[2] = birthdayArr[2].substring(1, birthdayArr[2].length);
        }
        return birthdayArr[0] + "年" + birthdayArr[1] + "月" + birthdayArr[2] + "日";
    }

    //根据身份证获取出生日期，格式YYYY-MM-DD
    function getBirthdayFromIDNo(IDNo) {
        var tmpStr = "";
        if (IDNo != null && IDNo != undefined) {
            if (IDNo.length == 15) {
                tmpStr = IDNo.substring(6, 12);
                tmpStr = "19" + tmpStr;
            } else if (IDNo.length == 18) {
                tmpStr = IDNo.substring(6, 14);
            }

            tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);
        }
        return tmpStr;
    }

    /***
     * 发证日期
     * @param birthday
     * @returns {string}
     */
    function changeBirthFormatFZRQ(birthday) {

        var birthdayArr = birthday.split("-");
        if (birthdayArr[1].substring(0, 1) == "0") {
            birthdayArr[1] = birthdayArr[1].substring(1, birthdayArr[1].length);
        }
        if (birthdayArr[2].substring(0, 1) == "0") {
            birthdayArr[2] = birthdayArr[2].substring(1, birthdayArr[2].length);
        }
        return birthdayArr[0] + "    " + birthdayArr[1] + "    " + birthdayArr[2];

    }
    function recordsGenerated(isprint) {
        var perList=[];
        for (i = 0; i < detail.length; i += 1) {
            perList.push(detail[i]);
        }
        $.ajax({
            type: 'POST',
            url: host + "/manage/cer/recordsGenerateds",
            dataType: "json",
            data: {
                "perList":JSON.stringify(perList),
                "isPrint":isprint
            },
            success: function (json) {
                if (json == true || json == "true") {
                    console.log("更新打印状态成功！");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console && console.log(errorThrown);
            }
        });
    }
</script>
</html>
