var year = new Date().getFullYear();
$(function() {
    // 公用方法，根据当前年月动态将13个月push数组
    month_method();
    // 公用方法，月份下拉框
    // select_option();
    // 回显当前年月份
    // $(".div-month select").val(initDate);
    $.jeDate("#month_date_from", {
        isinitVal: true,
        format: 'YYYY/MM' // 分隔符可以任意定义，该例子表示只显示年月
    });
    $.jeDate("#month_date_to", {
        isinitVal: true,
        format: 'YYYY/MM' // 分隔符可以任意定义，该例子表示只显示年月
    });

    $("#month_date_from").val("2017/07");
    $("#month_date_to").val(moment(moment().format("YYYY/MM")).subtract(1, "month").format("YYYY/MM"));


    $("#searchbtn").on("click", function() {
        getdeptscore($("#month_date_from").val(), $("#month_date_to").val());

    });


    getdeptscore($("#month_date_from").val(), $("#month_date_to").val());

});




function exportExcel() {
    var start_date = moment($("#month_date_from").val() + "-25 23:00:00").valueOf();
    var end_date = moment($("#month_date_to").val() + "-25 23:00:00").valueOf();
    if (start_date == '' || end_date == '') {
        tip("请选择起止日期");
        return;
    }
    window.open(getserveraddr() + "/file/dept/static/excel?start_date=" + start_date + "&end_date=" + end_date);
}




function getdeptscore(starttime, endtime) {

    $("#table_title").html("(" + starttime + "---" + endtime + ")");

    var time_m = moment(endtime).diff(moment(starttime), 'month')

    var data_p = {
        "start_date": moment(starttime + "-25 23:00:00").valueOf(),
        "end_date": moment(endtime + "-25 23:00:00").valueOf(),

    };

    //获取该人员排班
    var url = "/statistics/dept";

    post_common_service(url, data_p, function(data) {

        //     var table = '';

        //先确定表格标题
        var h_html = "<tr><th rowspan=2 style='width:30px'>序号</th><th rowspan=2 style='width:95px'>站名</th>";
        var h_html_t = "<tr>"
        for (var i = 0; i <= time_m; i++) {
            h_html += "<th colspan=4 style='width:240px'>" + (moment(starttime).add(i, "month").format("MM月")) + "</th>";
            h_html_t += "<th style='width:60px'>考核分</th><th style='width:60px'>积分</th><th style='width:60px'>累计积分</th><th style='width:60px'>排名</th>"
        }
        h_html += "</tr>";
        h_html_t += "</tr>";
        h_html += h_html_t;

        $("#table_h").html(h_html)
        $(".tb").css("width", (time_m + 1) * 4 * 60 + 125)
            // $("#table").css("width",time_m*4*30+80)

        var b_html = ""


        //再确定表格列
        if (Object.keys(data).length > 0) {
            var cc = 0;
            for (var i in data) {
                var dd = data[i];
                b_html += "<tr><td style='width:30px'>" + (++cc) + "</td><td style='width:95px'>" + dd[0].dept_name + "</td>"
                var tt = moment(dd[0].time).diff(moment(starttime), 'month')
                for (var op = 0; op < tt; op++) {
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                }
                for (var p in dd) {
                    var ss = dd[p];
                    b_html += "<td style='width:60px'>" + ss.score + "</td>"
                    b_html += "<td style='width:60px'>" + ss.integral + "</td>"
                    b_html += "<td style='width:60px'>" + ss.total_integral + "</td>"
                    b_html += "<td style='width:60px'>" + ss.total_ranking + "</td>"
                }

                var tt1 = moment(endtime).diff(moment(dd[dd.length - 1].time), 'month')
                for (var op = 0; op < tt1; op++) {
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                    b_html += "<td style='width:60px'>-</td>"
                }
            }
            b_html += "</tr>"



        }


        $("#table").html(b_html);


        //     $("#totlapeople").html(data.length);
        //     for (var i in data) {
        //         var ss = data[i];
        //         var dat = "";
        //         for (var j in ss.userStatistics) {
        //             var po = ss.userStatistics[j];
        //             dat += po.p_str + "月、";
        //         }
        //         dat = dat.substring(0, dat.length - 1);
        //         table += "<tr><td width='5%;'>" + (parseInt(i) + 1) + "</td><td width='10%;'>" + ss.user.workno + "</td><td width='15%;'>" + ss.dept + "</td><td width='10%;'><a href='JavaScript:void(0)' onclick='jumpuser(\"" + ss.user.id + "\",\"" + ss.user.organid + "\")'>" + ss.user.truename + "</a></td><td width='60%;'>" + dat + "</td></tr>";


        //     }



        //     $(".table-tbody").html(table);
        //     $('[data-toggle="tooltip"]').tooltip();
        //     $("tr:gt(0):odd").css("background", "#EFEFEF");



    });




}