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

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

    var html = '<option value="71">门诊西药房</option><option value="81">门诊中药房</option><option value="82">颗粒剂药房</option>';
    $('#groupNoSelect').html(html);
    $('#groupNoSelect').selectpicker('refresh');

    delay(getLastDispensingWindowsByCurrentUser);


    /**
     * 提交按钮点击事件
     */
    $("#send").on("click",function () {
        var data = {
            "groupNo": $("#groupNoSelect").val(),
            "windowsNo":$("#winNo").val(),
            "mzCfPrintIndex": $("#printSelect_mzcf").val(),
            "ekCfPrintIndex": $("#printSelect_ekcf").val(),
            "jzCfPrintIndex": $("#printSelect_jzcf").val(),
            "zsdPrintIndex": $("#printSelect_zsd").val(),
            "bqPrintIndex": $("#printSelect_bq").val(),
            "fskPrintIndex": $("#printSelect_fsk").val()
        };
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: "/thmz/saveDispensingWindows",
            dataType: 'json',
            data: JSON.stringify(data),
            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 getLastDispensingWindowsByCurrentUser() {
    /**
     * 查询发药员参数信息
     */
    $.ajax({
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/getLastDispensingWindowsByCurrentUser",
        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.mzCfPrintIndex!=null){
                    $('#printSelect_mzcf').selectpicker('val', result.data.mzCfPrintIndex);
                    $('#printSelect_mzcf').selectpicker('refresh');
                }
                if(result.data.ekCfPrintIndex!=null){
                    $('#printSelect_ekcf').selectpicker('val', result.data.ekCfPrintIndex);
                    $('#printSelect_ekcf').selectpicker('refresh');
                }
                if(result.data.jzCfPrintIndex!=null){
                    $('#printSelect_jzcf').selectpicker('val', result.data.jzCfPrintIndex);
                    $('#printSelect_jzcf').selectpicker('refresh');
                }
                if(result.data.zsdPrintIndex!=null){
                    $('#printSelect_zsd').selectpicker('val', result.data.zsdPrintIndex);
                    $('#printSelect_zsd').selectpicker('refresh');
                }
                if(result.data.bqPrintIndex!=null){
                    $('#printSelect_bq').selectpicker('val', result.data.bqPrintIndex);
                    $('#printSelect_bq').selectpicker('refresh');
                }
                if(result.data.fskPrintIndex!=null){
                    $('#printSelect_fsk').selectpicker('val', result.data.fskPrintIndex);
                    $('#printSelect_fsk').selectpicker('refresh');
                }
                if(result.data.groupNo!=null) {
                    $('#groupNoSelect').selectpicker('val', result.data.groupNo);
                    $('#groupNoSelect').selectpicker('refresh');
                }
            } else {
                errorMesage(result);
            }
        }
    });
}

//延迟加载方法
function delay(callback){
    if(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_mzcf').html(html);
    $('#printSelect_mzcf').selectpicker('refresh');
    $('#printSelect_ekcf').html(html);
    $('#printSelect_ekcf').selectpicker('refresh');
    $('#printSelect_jzcf').html(html);
    $('#printSelect_jzcf').selectpicker('refresh');
    $('#printSelect_zsd').html(html);
    $('#printSelect_zsd').selectpicker('refresh');
    $('#printSelect_bq').html(html);
    $('#printSelect_bq').selectpicker('refresh');
    $('#printSelect_fsk').html(html);
    $('#printSelect_fsk').selectpicker('refresh');
    printSucess=true;
}






