//@ sourceURL=pharmacy-cell-number.js
//待取药队列
var currentList = new Array();
//过号名单队列
var pastList = new Array();
//正在配药队列
var dispenseList = new Array();
//正在取药
var rightPerson = null;
//临时待取药队列
var tempCurrentList = new Array();
//临时过号名单
var tempPastList = new Array();
//临时正在配药队列
var tempDispenseList = new Array();
//药房编号
var groupNo = null;
var timeInterval;
var tableInterval;
var getTableDateInterval;
$(function () {
    clearInterval(timeInterval);
    clearInterval(tableInterval);
    clearInterval(getTableDateInterval);
    openSocket("JH");
    $("input").each(function (){
        this.onkeydown = function (e) { // 监听键盘事件
            let theEvent = window.event || e;
            let code = theEvent.keyCode || theEvent.which;
            if (code == 13) {//回车事件
                $("#btn_init").click(); //触发搜索按钮点击事件
            }
        }
    });
    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    var yesNo = '<option value="71">门诊西药房</option><option value="81">门诊中药房</option><option value="82">颗粒剂药房</option>';
    $('#fyClass').html(yesNo);
    $('#fyClass').selectpicker('refresh');
    $("#editModal").modal("show");
    //进入页面就加载一次
    CurentTime();
    intClock();
    //每秒获取当前时间
    timeInterval = setInterval(CurentTime, 1000);
    //设置页面宽度
    setHeightForTable();
    $(window).resize(function () {
        //设置页面宽度
        setHeightForTable();
    });
});


/**
 * 设置页面宽度
 */
function setHeightForTable() {
    var height = $(window).height() - 320;
    var realHeigt = height / 7;
    if (realHeigt < 65) {
        realHeigt = 65;
    }
    $("#tableList tr").css("height", realHeigt + "px");
}


/**
 * 取当前时间 格式 yyyy年mm月dd日 hh:mm:ss weekday
 * @constructor
 */
function CurentTime() {
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    // var ss=now.getSeconds();            //秒
    var weekday = new Array(7)			//周
    weekday[0] = "星期日"
    weekday[1] = "星期一"
    weekday[2] = "星期二"
    weekday[3] = "星期三"
    weekday[4] = "星期四"
    weekday[5] = "星期五"
    weekday[6] = "星期六"
    var date = year + "年";
    if (month < 10) date += "0";
    date += month + "月";
    if (day < 10) date += "0";
    date += day + "日 ";
    date += "  " + weekday[now.getDay()];
    $("#dateNow").html(date);
    var time = "";
    if (hh < 10) time += "0";
    time += hh + ":";
    if (mm < 10) time += '0';
    time += mm;
    $("#timeNow").html(time)
}


/**
 * 初始化时钟
 */
function intClock() {
    var audioElement = new Audio("");
    //clock plugin constructor
    $('#myclock').thooClock({
        //size:$(document).height()/1.4,
        size: 80,
        onAlarm: function () {
            //all that happens onAlarm
            //$('#alarm1').show();
            // alarmBackground(0);
            //audio element just for alarm sound
            document.body.appendChild(audioElement);
            var canPlayType = audioElement.canPlayType("audio/ogg");
            if (canPlayType.match(/maybe|probably/i)) {
                audioElement.src = 'alarm.ogg';
            } else {
                audioElement.src = 'alarm.mp3';
            }
            // erst abspielen wenn genug vom mp3 geladen wurde
            audioElement.addEventListener('canplay', function () {
                audioElement.loop = true;
                audioElement.play();
            }, false);
        },
        showNumerals: true,
        brandText: 'THOOYORK',
        brandText2: 'Germany',
        onEverySecond: function () {
            //callback that should be fired every second
        },
        //alarmTime:'15:10',
        offAlarm: function () {
            //$('#alarm1').hide();
            audioElement.pause();
            clearTimeout(intVal);
            $('body').css('background-color', '#FCFCFC');
        }
    });
}


/**
 * 实例化列表
 */
function initTable() {
    var winNo = $("#winNo").val();
    groupNo = $("#fyClass").val();
    if (groupNo == null || groupNo == "") {
        errorMesageSimaple("未选择药房！");
        return;
    }
    if (winNo == null || winNo == "") {
        errorMesageSimaple("窗口号未录入！");
        return;
    }
    var yfName = "门诊西药房";
    if (groupNo == 81) {
        yfName = "门诊中药房";
    } else if (groupNo == 82) {
        yfName = "颗粒剂药房";
    }
    $("#yfName").html(yfName);
    $("#winNum").text(winNo);
    $("#editModal").modal("hide");
    getTableDate();
}


/**
 * 获取后台最新数据
 */
function getTableDate() {
    $.ajax({
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/getToMedicineUser?groupNo=" + groupNo,
        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) {
                currentList = [];
                dispenseList = [];
                tempCurrentList = [];
                tempDispenseList = [];
                var list = result.currentList;
                for (let i = 0; i < list.length; i++) {
                    var current = list[i];
                    if (current.confirm_flag == 5) {//已配药待发药
                        currentList[currentList.length] = current;
                        tempCurrentList[tempCurrentList.length] = current;
                    } else if (current.confirm_flag == 0 || current.confirm_flag == 2) {
                        dispenseList[dispenseList.length] = current;
                        tempDispenseList[tempDispenseList.length] = current; //重置标志
                    }
                }
                if(tableInterval == null){
                    //10秒刷新一次队列里面的数据，应对数据很多，一页展示不完的情况
                    tableInterval = setInterval(refeshTable, 10000);
                }
                if(getTableDateInterval == null){
                    //每十分钟重新加载等待叫号和正在配药数据
                    getTableDateInterval = setInterval(getTableDate, 100000);
                }
            } else {
                errorMesage(result);
            }
        }
    });
}

/**
 * 刷新列表
 */
function refeshTable() {
    //console.log("方法一轮时间："+new Date().getSeconds());
    for (var i = 1; i < 7; i++) {
        if (tempPastList == null || tempPastList.length == 0) {
            $("#past" + i).text("");
            if (i == 6) {
                //深度拷贝数组
                tempPastList = $.extend(true, [], pastList);
            }
        } else {
            //删除并返回数组的第一个元素
            var name = tempPastList.shift().name;
            $("#past" + i).text(name);
        }
    }
    for (var i = 1; i < 13; i++) {
        if (tempCurrentList == null || tempCurrentList.length == 0) {
            $("#current" + i).text("");
            if (i == 12) {
                //深度拷贝数组
                tempCurrentList = $.extend(true, [], currentList);
            }
        } else {
            //删除并返回数组的第一个元素
            var name = tempCurrentList.shift().name;
            $("#current" + i).text(name);
        }
    }
    for (var i = 1; i < 13; i++) {
        if (tempDispenseList == null || tempDispenseList.length == 0) {
            $("#dispense" + i).text("");
            if (i == 12) {
                //深度拷贝数组
                tempDispenseList = $.extend(true, [], dispenseList);
            }
        } else {
            //删除并返回数组的第一个元素
            var name = tempDispenseList.shift().name;
            $("#dispense" + i).text(name);
        }
    }
}


/**
 * 清空过号重复病人数据
 */
function pastListClear(data) {
    for (let i = 0; i < pastList.length; i++) {
        if (pastList[i].patient_id == data.patient_id) {
            pastList.splice(i, 1);
            break;
        }
    }
}

/**
 * 清空等待取药重复病人数据
 */
function currentListClear(data) {
    for (let i = 0; i < currentList.length; i++) {
        if (currentList[i].patient_id == data.patient_id) {
            currentList.splice(i, 1);
            break;
        }
    }
}

/**
 * 清空正在配药重复病人数据
 */
function dispenseListClear(data) {
    for (let i = 0; i < dispenseList.length; i++) {
        if (dispenseList[i].patient_id == data.patient_id) {
            dispenseList.splice(i, 1);
        }
    }
}




