/**
 *
 * @author BOGON
 * @version 1.0
 */

var page = {
    save: function (callback) {
        $('#A4_textarea_json').html(JSON.stringify(hiprintTemplate.getJson()))
        let load = topWin.layer.load(0);
        $.ajax({
            url: g.ctx + "pc/sys/printTemp/rest/saveOrUpdate",
            contentType: "application/json",
            data: JSON.stringify({id: $("#id").val(), designJson: $("#A4_textarea_json").val()}),
            type: "POST",
            complete: function () {
                topWin.layer.close(load);
            },
            success: function (data) {
                topWin.layer.msg('保存成功', {icon: 1});
                topWin.layer.close(load);
                callback()
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
var hiprintTemplate;
$(function () {
    //初始化打印插件
    hiprint.init({
        providers: [new customElementTypeProvider()]
    });

    //hiprint.PrintElementTypeManager.build('.hiprintEpContainer', 'testModule');
    //设置左侧拖拽事件
    hiprint.PrintElementTypeManager.buildByHtml($('.ep-draggable-item'));
    console.log($("#A4_textarea_json").val())
    hiprintTemplate = new hiprint.PrintTemplate({
        template: $("#A4_textarea_json").val() ? JSON.parse($("#A4_textarea_json").val()) : {},
        settingContainer: '#PrintElementOptionSetting',
        paginationContainer: '.hiprint-printPagination'
    });
    //打印设计
    hiprintTemplate.design('#hiprint-printTemplate');

    $('#A4_preview').click(function () {
        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml({}))
        $('#myModal').modal('show')
    });
    $('#A4_directPrint').click(function () {
        hiprintTemplate.print({});
    });
    $('#A4_printByHtml').click(function () {
        hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
    })
    $('#A4_getJson_toTextarea').click(function () {
        $('#A4_textarea_json').html(JSON.stringify(hiprintTemplate.getJson()))
    })
})
//调整纸张
var setPaper = function (paperTypeOrWidth, height) {
    hiprintTemplate.setPaper(paperTypeOrWidth, height);
}

//旋转
var rotatePaper = function () {
    hiprintTemplate.rotatePaper();
}
var clearTemplate = function () {
    hiprintTemplate.clear();
}