//@ sourceURL=sfy_config.js
var LODOP; //声明为全局变量
var printSucess=false;
var deptNoSucess=false;
$(function (){



    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    //延迟触发列表加载，因为一开始打印程序还没有加载成功
    setTimeout('createPrinterList()',500);


    $.ajax({
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/getMzZdDeptNos",
        headers:{'Accept': 'application/json','Authorization':'Bearer '+ localStorage.getItem("token")},
        dataType: 'json',
        success: function(result) {
            if(result=='401' || result==401){
                window.location.href='/thmz/login/view'
                return;
            }
            if(result.code == 0) {
                var arr = result.data;
                if(arr!=null && arr.length>0){
                    var html="";
                    for(var i=0;i<arr.length;i++){
                        html += "<option value='" + arr[i].code + "'>" + arr[i].name + "</option>";
                    }
                    $('#deptNoSelect').html(html);
                    $('#deptNoSelect').selectpicker('refresh');
                    deptNoSucess=true;
                }
            } else {
                errorMesage(result);
            }
        }
    });

   delay(getLastWindowsByCurrentUser);


    /**
     * 提交按钮点击事件
     */
    $("#send").on("click",function (t) {
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: "/thmz/saveWindows",
            dataType: 'json',
            data: JSON.stringify({"deptNo": $("#deptNoSelect").val(),"windowsNo":$("#winNo").val(),"printIndex": $("#printSelect").val(),"cgPrintIndex": $("#printSelect_cg").val(),"zyPrintIndex": $("#printSelect_zy").val()}),
            headers:{'Accept': 'application/json','Authorization':'Bearer '+ localStorage.getItem("token")},
            success: function(result) {
                if(result=='401' || result==401){
                    window.location.href='/thmz/login/view'
                    return;
                }
                if(result.code == 0) {
                    successMesage(result);
                } else {
                    errorMesage(result);
                }
            }
        });
    });

});


/**
 * 查询当前用户的基础设置
 */
function getLastWindowsByCurrentUser() {
    /**
     * 查询收费员参数信息
     */
    $.ajax({
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/getLastWindowsByCurrentUser",
        dataType: 'json',
        headers:{'Accept': 'application/json','Authorization':'Bearer '+ localStorage.getItem("token")},
        success: function(result) {
            if(result=='401' || result==401){
                window.location.href='/thmz/login/view'
                return;
            }
            if(result.code == 0) {
                $("#winNo").val(result.data.windowsNo);
                if(result.data.printIndex!=null){
                    $('#printSelect').selectpicker('val', result.data.printIndex);
                    $('#printSelect').selectpicker('refresh');
                }
                if(result.data.zyPrintIndex!=null){
                    $('#printSelect_zy').selectpicker('val', result.data.zyPrintIndex);
                    $('#printSelect_zy').selectpicker('refresh');
                }
                if(result.data.cgPrintIndex!=null){
                    $('#printSelect_cg').selectpicker('val', result.data.cgPrintIndex);
                    $('#printSelect_cg').selectpicker('refresh');
                }
                if(result.data.deptNo!=null) {
                    $('#deptNoSelect').selectpicker('val', result.data.deptNo);
                    $('#deptNoSelect').selectpicker('refresh');
                }
            } else {
                errorMesage(result);
            }
        }
    });
}

//延迟加载方法
 function delay(callback){
    if(deptNoSucess && printSucess){
        callback();
        return;
    }else{setTimeout(function(){delay(callback)}, 100)}
}







/**
 * 加载打印机列表
 * @constructor
 */
function createPrinterList() {
    LODOP = getLodop();
    LODOP.PRINT_INITA(6, 0, "92mm", "120mm", "泰和门诊发票打印");
    //获取打印机设备数，用于进行遍历
    var count = LODOP.GET_PRINTER_COUNT();
    var html = "";
    for (var i = 0; i < count; i++) {
        //根据设备序号获取设备名
        var msg = LODOP.GET_PRINTER_NAME(i);
        html += "<option value='" + i + "'>" + msg + "</option>";
    }
    $('#printSelect').html(html);
    $('#printSelect').selectpicker('refresh');
    $('#printSelect_zy').html(html);
    $('#printSelect_zy').selectpicker('refresh');
    $('#printSelect_cg').html(html);
    $('#printSelect_cg').selectpicker('refresh');
    printSucess=true;
}






