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

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

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


/**
 * 初始化下拉选
 */
function initSelect() {
    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    initDeptSelect();
    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>';
            });
            $('#execDept').empty();
            $('#execDept').html(html);
            $('#execDept').selectpicker('refresh');
        }
    });
}


/**
 * 科室核算报表
 */
function initkshsTable() {
    var rePortRangeArr = getRePortRangeArr();
    $("#nucleicAcidModal").modal();
    $.ajax({
        type: "POST",
        url: '/thmz/getDepetBusinessReport',
        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(),
            "execDept": $("#execDept").val()
        }),
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view';
                return;
            }
            $("#nucleicAcidModal").modal("hide");
            if (res.code == 0) {
                if (res.depetList != null && res.depetList.length > 0 && res.parentList != null && res.parentList.length > 0) {
                    var html = "<thead><tr><th class='text-center' >父类码</th><th class='text-center'>核算码</th>";
                    var totalTrHtml = "<tr><td>总计</td><td></td>";
                    for (var i = 0; i < res.depetList.length; i++) {
                        var execDeptName = res.depetList[i].exec_dept;
                        if (execDeptName == null) {
                            execDeptName = "";
                        }
                        html += "<th class='text-center'>" + execDeptName + "</th>";
                        totalTrHtml += "<td>" + res.depetList[i].amount.toFixed(2) + "</td>";
                    }
                    totalTrHtml += "<td>" + res.totalAmount.toFixed(2) + "</td></tr>";
                    html += "<td>金额</td></tr></thead>";
                    html += "<tbody>";
                    //当前迭代数据下标
                    var index = 0;
                    //当前迭代的父类码的小计的下标
                    var xjIndex = 0;
                    for (var i = 0; i < res.parentList.length; i++) {
                        var hsmList = res.parentList[i].hsmList;
                        var rows = hsmList.length;
                        xjIndex = index;
                        var xjData = res.realDatas[xjIndex];
                        var xjAmount = 0;
                        for (var j = 0; j < hsmList.length; j++) {
                            html += "<tr>";
                            if (j == 0) {
                                html += "<td rowspan='" + rows + "' style='vertical-align: middle !important;'>" + res.parentList[i].parentName + "</td>";
                            }
                            html += "<td>" + hsmList[j] + "</td>";
                            var realData = res.realDatas[index];
                            for (var t = 0; t < realData.length; t++) {
                                var temp = realData[t];
                                if (temp == null) {
                                    html += "<td></td>";
                                } else {
                                    if (index > xjIndex) {
                                        xjData[t] = xjData[t] + temp;
                                    }
                                    html += "<td>" + temp.toFixed(2) + "</td>";
                                }
                            }
                            xjAmount = xjAmount + res.hsmAmounts[index];
                            html += "<td>" + res.hsmAmounts[index].toFixed(2) + "</td>";
                            index++;
                            html += "</tr>";
                        }
                        html += "<tr><td>小计：</td><td></td>";
                        for (var l = 0; l < xjData.length; l++) {
                            if (xjData[l] == null) {
                                html += "<td>0.00</td>";
                            } else {
                                html += "<td>" + xjData[l].toFixed(2) + "</td>";
                            }
                        }
                        html += "<td>" + xjAmount.toFixed(2) + "</td>";
                        html += "</tr>";
                    }
                    html += totalTrHtml;
                    html += "</tbody>";
                    $("#tb_table").html(html);
                }
            } 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');
    $("#execDept").selectpicker('val', null);//默认选中
    $("#execDept").selectpicker('refresh');
}

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