//@ sourceURL=request-config.js
$(function () {
    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    initDeptSelect();

    //告警阈值
    $.ajax({
        type: "GET",
        url: '/thmz/getConfigByKey?key=alarm_num',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            $("#alarmNum").val(res.data.configValue);
        }
    });

    $('#send').click(function () {
        //修改告警设置
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: '/thmz/setConfig',
            dataType: "json",
            data: JSON.stringify({"configKey": "alarm_num","configValue": $("#alarmNum").val()}),
            headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
            success: function (res) {
                if (res == '401' || res == 401) {
                    window.location.href = '/thmz/login/view'
                    return;
                }
                if (res.code == 0) {
                    //修改告警人员
                    var doctor=$("#doctor").val();
                    var doctorStr=null;
                    if(doctor!=null && doctor.length>0){
                        for (var i=0;i<doctor.length;i++){
                            if(doctorStr==null){
                                doctorStr=doctor[i];
                            }else {
                                doctorStr+=","+doctor[i];
                            }

                        }
                    }
                    $.ajax({
                        type: "POST",
                        contentType: "application/json;charset=UTF-8",
                        url: '/thmz/setConfig',
                        dataType: "json",
                        data: JSON.stringify({"configKey": "alarm_user","configValue": doctorStr}),
                        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
                        success: function (res) {
                            if (res == '401' || res == 401) {
                                window.location.href = '/thmz/login/view'
                                return;
                            }
                            if (res.code == 0) {
                                successMesage(res);
                            } else {
                                errorMesage(res);
                            }
                        }
                    });
                } else {
                    errorMesage(res);
                }
            }
        });
    });


});






/**
 * 挂号列表中的科室列表
 */
function initDeptSelect() {
    //科室列表
    $.ajax({
        type: "GET",
        url: '/thmz/allUnitCode',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (data) {
            if (data == '401' || data == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '<option value=""></option>';
            $.each(data.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '(' + comment.pyCode + ')</option>';
            });
            $('#deptNo').empty();
            $('#deptNo').html(html);
            $('#deptNo').selectpicker('refresh');
            $("#deptNo").selectpicker('val', '1400002');
            $("#deptNo").selectpicker('refresh');
            initDoctorSelect();
        }
    });
}


/**
 * 挂号列表中的医生列表初始化
 */
function initDoctorSelect() {
    //医生列表
    $.ajax({
        type: "GET",
        url: '/thmz/listEmployeeByDepts?depts=' + $('#deptNo').val(),
        //url: '/thmz/listEmployeeByDepts?depts=null',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '<option value=""></option>';
            $.each(res.data, function (commentIndex, comment) {
                html += '<option value="' + comment.employeeCode + '">' + comment.employeeName + ' (' + comment.deptName + ') </option>';
            });
            $('#doctor').empty();
            $('#doctor').html(html);
            $('#doctor').selectpicker('destroy').selectpicker('refresh');

            //告警人员设置
            $.ajax({
                type: "GET",
                url: '/thmz/getConfigByKey?key=alarm_user',
                dataType: "json",
                headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
                success: function (res) {
                    if (res == '401' || res == 401) {
                        window.location.href = '/thmz/login/view'
                        return;
                    }
                    var doctorStr=res.data.configValue;
                    if(doctorStr!=null && doctorStr!=''){
                        var arr =doctorStr.split(",");
                        $("#doctor").selectpicker('val', arr);
                        $("#doctor").selectpicker('refresh');
                    }

                }
            });
        }
    });


}
