<!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>
    var LODOP = null;
    var printState=0;
    var host="http://"+window.location.host;
    var printData = {
        id:'[[${printData.id}]]',
        xm:'[[${printData.xm}]]',
        photo:'[[${printData.photo}]]',
        address:'[[${printData.address}]]',
        nation:'[[${printData.nation}]]',
        idnum:'[[${printData.idnum}]]',
        fzrq:'[[${printData.fzrq}]]',
        zsbh:'[[${printData.zsbh}]]',
        securityPic:'[[${printData.securityPic}]]',
        qrcode:'[[${printData.qrcode}]]'
    };
    $(function(){
        LODOP = getLodop();
        if(LODOP != null){
            setTimeout("lodopPrint()", 500);
        }
    });

    function lodopPrint(){
        console.log("=====:"+printData.photo.split("\n").length)
        document.body.innerText="打印中";
        printState = 0,
        LODOP.PRINT_INITA("-2.38mm", -7, "169.99mm", "144.99mm", "北京保安员证书打印V3.5.1");
        LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
        LODOP.ADD_PRINT_SETUP_BKIMG("<img border='0' src=" + host + printData.securityPic + "/>");
        LODOP.SET_SHOW_MODE("BKIMG_LEFT", -50);
        LODOP.SET_SHOW_MODE("BKIMG_TOP", -50);
        LODOP.SET_SHOW_MODE("BKIMG_WIDTH", "205.32mm");
        LODOP.SET_SHOW_MODE("BKIMG_HEIGHT", "144.99mm");
        LODOP.SET_SHOW_MODE("BKIMG_PRINT", true);
        //照片
        if (printData.photo) {
            LODOP.ADD_PRINT_IMAGE(30, 104, "26.01mm", "31.99mm", printData.photo);//+1
            LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
        } else {
            LODOP.ADD_PRINT_IMAGE(30, 104, "26.01mm", "31.99mm", "");//+1
            LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
        }
        //二维码50
        LODOP.ADD_PRINT_IMAGE(91, 501, 116, 111, printData.qrcode);

        LODOP.SET_PRINT_STYLEA(0, "Stretch", 2);
        //姓名
        LODOP.ADD_PRINT_TEXT(193, 109, 133, 20, printData.xm);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
        // LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        //地址
        LODOP.ADD_PRINT_TEXT(300, 110, 171, 56, printData.address == null ? '' : printData.address.substring(0, 9) + "\r\n" + printData.address.substring(9, printData.address.length));
        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(227, 110, 163, 20, printData.nation);
        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(266, 110, 163, 20, changeBirthFormat(getBirthdayFromIDNo(printData.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(391, 111, 163, 20, printData.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(325, 461, 200, 20, (printData.fzrq == null ? "" : changeBirthFormatFZRQBJ(printData.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(383, 496, 186, 20, printData.zsbh.replace("京", ""));
        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(34, 415, 120, 20, printData.xm);
        // LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);

        //谷歌浏览器
        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 changeBirthFormatFZRQBJ(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) {
        $.ajax({
            type: 'POST',
            url: host + "/manage/cer/recordsPerGenerated",
            dataType: "json",
            data: {
                "id":printData.id,
                "isPrint":isprint
            },
            success: function (json) {
                if (json == true || json == "true") {
                    console.log("更新打印状态成功！");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console && console.log(errorThrown);
            }
        });
    }
    

</script>
</html>
