//@ sourceURL=mzhbtj.js
$(function () {
    init_daterangepicker();
    //daterangepicker(getAllOperator);
    initMzsrmxTable();
    //重置查询参数
    $('#btn_clean').click(function () {
        cleanParams();
    });
    //初始化页面上面的按钮事件
    $("#btn_query").click(function (t) {
        initMzsrmxTable();
    });

    $(".selectpicker").selectpicker({
        dropuAuto: false
    });

    //初始化页面上面的按钮事件
    $("#btn_excel").click(function (t) {
        exportExcel();
    });
    initSelect();
});


/**
 * 初始化下拉选
 */
function initSelect() {
    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    initDeptSelect();
    initDoctorSelect();
    var type = '<option value="0">结算</option><option value="1">收费</option>';
    $('#timeParam').html(type);
    $('#timeParam').selectpicker('refresh');
    $("#timeParam").selectpicker('val', 0);//默认选中
    $("#timeParam").selectpicker('refresh');
}




/**
 * 挂号列表中的科室列表
 */
function initDeptSelect() {
    //科室列表
    $.ajax({
        type: "GET",
        url: '/thmz/allMzUnitCode',
        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 = '';
            $.each(data.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '(' + comment.pyCode + ')</option>';
            });
            $('#jzSqDept').empty();
            $('#jzSqDept').html(html);
            $('#jzSqDept').selectpicker('refresh');
        }
    });
}


/**
 * 挂号列表中的医生列表初始化
 */
function initDoctorSelect() {
    //医生列表
    $.ajax({
        type: "GET",
        url: '/thmz/listEmployeeByDepts?depts=' + $('#jzSqDept').val(),
        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 = '';
            $.each(res.data, function (commentIndex, comment) {
                html += '<option value="' + comment.employeeCode + '">' + comment.employeeName + '</option>';
            });
            $('#jzSqDoctor').empty();
            $('#jzSqDoctor').html(html);
            $('#jzSqDoctor').selectpicker('destroy').selectpicker('refresh');
        }
    });
}


/**
 * 门诊号别统计
 */
function initMzsrmxTable() {
    var rePortRangeArr = getRePortRangeArr();
    $.ajax({
        type: "POST",
        url: '/thmz/getMzhbCount',
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        data: JSON.stringify({
            "beginDate": rePortRangeArr[0],
            "endDate": rePortRangeArr[1],
            "caseType": $('#timeParam').val(),
            "jzSqDept": $("#jzSqDept").val(),
            "jzSqDoctor": $("#jzSqDoctor").val(),
            "hisData": $('#hisData').is(':checked') ? 0 : 1
        }),
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            if (res.code == 0) {
                if (res.depetList != null && res.depetList.length > 0 && res.chargeTypeList != null && res.chargeTypeList.length > 0) {
                    var html = "<thead><tr><th rowspan='2' class='text-center' style='vertical-align: middle !important;' >科室</th>";
                    var secondRow = '<tr>';
                    var totalTr = '<tr><th class="text-center">总计</th>';
                    for (var i = 0; i < res.chargeTypeList.length; i++) {
                        var charge_type_name=res.chargeTypeList[i].charge_type_name;
                        if(charge_type_name==null){
                            charge_type_name="";
                        }
                        html += "<th colspan='2' class='text-center'>" + charge_type_name + "</th>";
                        secondRow += "<th class='text-center'>号数</th><th class='text-center'>金额</th>";
                        totalTr += "<th>"+res.chargeTypeList[i].chargeHshj+"</th><th>"+res.chargeTypeList[i].chargeHehj+"</th>";
                    }
                    html += "<th colspan='2' class='text-center'>合计</th></tr>";
                    secondRow += "<th class='text-center'>号数</th><th class='text-center'>金额</th></tr></thead>";
                    html += secondRow;
                    html += "<tbody>";
                    var allhsht=0;
                    var alljehj=0.0;
                    for (var i = 0; i < res.depetList.length; i++) {
                        html += "<tr><td>" + res.depetList[i].dept_name + "</td>";
                        for (var j = 0; j < res.chargeTypeList.length; j++) {
                            var id_after = res.chargeTypeList[j].charge_type.trim() + "_" + res.depetList[i].unit_code;
                            var hsid = "hs_" + id_after;
                            var jeid = "je_" + id_after;
                            html += "<td id='" + hsid + "'></td><td id='" + jeid + "'></td>";
                        }
                        html += "<td>" + res.depetList[i].hshj + "</td><td>" + res.depetList[i].jehj.toFixed(2) + "</td>";
                        html += "</tr>";
                        allhsht=allhsht+res.depetList[i].hshj;
                        alljehj=alljehj+res.depetList[i].jehj;
                    }
                    totalTr+="<th>"+allhsht+"</th><th>"+alljehj.toFixed(2)+"</th></tr>";
                    html+=totalTr;
                    html += "</tbody>";
                    $("#tb_table").html(html);

                    for (var i = 0; i < res.data.length; i++) {
                        var id_after = res.data[i].charge_type.trim() + "_" + res.data[i].unit_code;
                        var hsid = "hs_" + id_after;
                        var jeid = "je_" + id_after;
                        $("#" + hsid).text(res.data[i].patient_count);
                        if (res.data[i].req_fee != null) {
                            $("#" + jeid).text(res.data[i].req_fee.toFixed(2));
                        }
                    }
                }
            }else {
                $("#tb_table").find("tbody").html(null);
            }
        }
    });
}



/**
 * 获取时间选择器的时间数组
 * @returns {string[]}
 */
function getRePortRangeArr() {
    var rePortRange = $('#reportrange span').html();
    var rePortRangeArr = rePortRange.split(" - ");
    rePortRangeArr[0] = rePortRangeArr[0] + " 00:00:00"
    rePortRangeArr[1] = rePortRangeArr[1] + " 23:59:59"
    return rePortRangeArr;
}


/**
 * 清空查询条件
 */
function cleanParams() {
    $('#reportrange span').html(moment().format('YYYY-MM-DD') + ' - ' + moment().format('YYYY-MM-DD'));
    $("#timeParam").selectpicker('val', 0);//默认选中
    $("#timeParam").selectpicker('refresh');
    $("#jzSqDept").selectpicker('val', null);//默认选中
    $("#jzSqDept").selectpicker('refresh');
    $("#jzSqDoctor").selectpicker('val', null);//默认选中
    $("#jzSqDoctor").selectpicker('refresh');
    $("#hisData").removeAttr("checked");
}

/**
 * 导出excel
 */
function exportExcel() {
    var table2excel = new Table2Excel();
// 传入你的tableId即可导出
    table2excel.export($('#tb_table'), "门诊号别列表");
}