var lianyungang_Station = new Array;
var yancheng_Station = new Array;
var nantong_Station = new Array;
var r1 = new Array();
var r2 = new Array();
var r3 = new Array();
var rank = new Array();
/**
 * 点击查询
 */
function queryTypeClick() {
    //每次点击前清空表格数据
    $(".table-tbody").empty('');
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    if (start_date == null || start_date == '' || start_date == undefined ||
        end_date == null || end_date == '' || end_date == undefined) {
        tip("请选择起止日期");
        return;
    } else {
        start_date += " 23:00:00";
        end_date += " 23:00:00";
    }
    var startStr = start_date.replace(/-/g, '/');
    var endStr = end_date.replace(/-/g, '/');
    var start = new Date(startStr);
    //开始日期取前一天的23:00
    start.setDate(start.getDate() - 1);
    start_date = start.Format("yyyy-MM-dd HH:mm:ss");
    var end = new Date(endStr);
    if (start > end) {
        tip("开始时间不能比结束时间大");
        return;
    }
    var param = {
        "dept_id": $("#fsList").val(),
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/operation/manage/integration";
    var strTbody = '';
    post_common_service(url, param, function(data) {
        // 运营管理综合检查情况
        check_operation_avg(data);
    }, function(error) {
        return "";
    });
}

/**
 * 表格初始化
 */
function initQueryType() {
    // 默认初始化前一周查所有
    var tdaytime = moment();
    var start_time = "";
    var end_time = "";
    if (tdaytime.get('D') >= 26) {
        start_time = tdaytime.set('date', 26).format("YYYY-MM-DD");
        tdaytime = moment();
        end_time = tdaytime.set('date', 25).add(1, 'M').format("YYYY-MM-DD");
    } else {
        start_time = tdaytime.set('date', 26).subtract(1, 'M').format("YYYY-MM-DD");
        tdaytime = moment();
        end_time = tdaytime.set('date', 25).format("YYYY-MM-DD");

    }
    // 默认初始化时间页面回显
    $("#start-time").val(start_time);
    $("#end-time").val(end_time);
    var start_date = start_time + " 23:00:00";
    var end_date = end_time + " 23:00:00";
    var start = new Date(start_date);
    //开始日期取前一天的23:00
    start.setDate(start.getDate() - 1);
    start_date = start.Format("yyyy-MM-dd HH:mm:ss");
    var param = {
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/operation/manage/integration";
    var strTbody = '';

    post_common_service(url, param, function(data) {
        // 运营管理综合检查情况
        check_operation_avg(data);
    }, function(error) {
        return "";
    });

}

/**
 * 表格拼接及数据传入
 * @param {*} data
 * @param {*} i
 * @param {*} avg
 */
function gethtml(data, i, avg, lists) {
    var strTbody = "";

    // if(i==0 || i==7 || i==13){
    //    // debugger;
    //     var ranking;
    //     var name = "";
    //     var rowspan = 0;
    //       order = 1;
    //     if(i==0){
    //         name = "连云港";
    //         rowspan = 7;
    //         for(var i=0;i<lists.length;i++){
    //             if(avg > lists[i]){
    //                 order = order + 1;
    //             }
    //         }
    //     }else if(i==7){
    //         name = "盐城";
    //         rowspan = 6;
    //         for(var i=0;i<lists.length;i++){
    //             if(avg > lists[i]){
    //                 order = order + 1;
    //             }
    //         }
    //     }else if(i==13){
    //         name = "南通";
    //         rowspan = 8;
    //         for(var i=0;i<lists.length;i++){
    //             if(avg > lists[i]){
    //                 order = order + 1;
    //             }
    //         }
    //     }
    //     strTbody += "<tr><td rowspan="+rowspan+" width='6%'>"+order+"</td>"+"<td rowspan="+rowspan+" width='6%'>"+name+"</td>";
    //     strTbody+="<tr><td width='6%'>"+data.company_ranking2+"</td>"
    //                +"<td width='6%'>"+data.fee_station_ranking2+"</td>"
    //                +"<td width='8%'>"+data.name+"</td><td width='6%'>"+filter(data.people_num,'0')
    //                +"</td><td width='54px'>"+data.company_check_num
    //                +"</td><td width='54px'>"+data.company_total_score+"</td><td width='54px'>"+data.third_check_num
    //                +"</td><td width='54px'>"+data.third_total_score+"</td><td width='54px'>"+data.special_check_num
    //                +"</td><td width='54px'>"+data.special_total_score+"</td><td width='54px'>"+data.onsite_check_num
    //                +"</td><td width='54px'>"+data.onsite_total_score+"</td><td width='54px'>"+data.data_check_num
    //                +"</td><td width='54px'>"+data.data_total_score+"<td width='6%'>"
    //                +data.check_total_avg
    //                +"</td><td width='6%'>"+data.score_avg+"</td>";
    //     strTbody += "</tr>";
    // }else{
    //     strTbody += "<tr><td width='6%'>"+data.company_ranking2+"</td><td>"+data.fee_station_ranking2+"</td>";
    //     strTbody+="<td>"+data.name+"</td><td>"+data.people_num+"</td><td>"+data.company_check_num
    //               +"</td><td>"+data.company_total_score+"</td><td>"+data.third_check_num
    //               +"</td><td>"+data.third_total_score+"</td><td>"+data.special_check_num
    //               +"</td><td>"+data.special_total_score+"</td><td>"+data.onsite_check_num
    //               +"</td><td>"+data.onsite_total_score+"</td><td>"+data.data_check_num
    //               +"</td><td>"+data.data_total_score+"</td><td>"
    //               +data.check_total_avg+"</td>"
    //               +"<td>"+data.score_avg+"</td>";
    //     strTbody += "</tr>";

    // }
    strTbody += "<tr><td width='8%'>" + data.company_ranking2 + "</td>";
    strTbody += "<td width='8%'>" + data.name + "</td><td width='8%'>" + data.people_num + "</td><td width='6%'>" + data.company_check_num +
        "</td><td width='6%'>" + data.company_total_score + "</td><td width='6%'>" + data.third_check_num +
        "</td><td width='6%'>" + data.third_total_score + "</td><td width='6%'>" + data.special_check_num +
        "</td><td width='6%'>" + data.special_total_score + "</td><td width='6%'>" + data.onsite_check_num +
        "</td><td width='6%'>" + data.onsite_total_score + "</td><td width='6%'>" + data.data_check_num +
        "</td><td width='6%'>" + data.data_total_score + "</td><td width='8%'>" +
        data.check_total_avg + "</td>" +
        "<td width='8%'>" + data.score_avg + "</td>";
    strTbody += "</tr>";
    return strTbody;
}

function getTotalHtml(data) {
    var html = "<tr><td>合计</td><td></td><td>" + data.totalMan + "</td><td>" + data.totalCompanyNum + "</td><td>" + data.totalCompanyScore + "</td><td>" +
        data.totalThirdNum + "</td><td>" + data.totalThirdScore + "</td><td>" + data.totalSpecialNum + "</td><td>" + data.totalSpecialScore +
        "</td><td>" + data.totalOnsiteNum + "</td><td>" + data.totalOnsiteScore + "</td><td>" + data.totalDataNum +
        "</td><td>" + data.totalDataScore + "</td><td>-</td><td>-</td></tr>";
    return html;
}

/**
 * 管道中心受检人平均每人次扣分
 * @param {*} data
 */
function check_operation_avg(data) {
    var list = data.list;
    var dataArray = new Array();
    var sum = 0;
    var sum1 = 0;
    for (k = 0; k < list.length; k++) {
        sum = parseFloat(sum) + parseFloat(list[k].check_total_avg);
        sum1++;
        if (k == 6) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
        if (k == 12) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
        if (k == 20) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
    }

    for (i = 0; i < list.length; i++) {
        if (i == 0) {
            $(".table-tbody").append(gethtml(list[i], i, dataArray[0], dataArray));
        } else if (i == 7) {
            $(".table-tbody").append(gethtml(list[i], i, dataArray[1], dataArray));
        } else if (i == 13) {
            $(".table-tbody").append(gethtml(list[i], i, dataArray[2], dataArray));
        } else {
            $(".table-tbody").append(gethtml(list[i], i));
        }
    }
    $(".table-tbody").append(getTotalHtml(data));
}

/**
 * 过滤undefined
 * @param {*} value
 * @param {*} default_display_value
 */
function filter(value, default_display_value) {
    if (value != '' && value != undefined) {
        return value;
    } else if (default_display_value != undefined) {
        return default_display_value;
    } else {
        return "";
    }
}

/**
 * 过滤除数为0和保留二位小数时出现的0.00
 * @param {*} value
 * @param {*} default_display_value
 */
function filterByZeroHandle(value, default_display_value) {
    if (isNaN(value)) {
        return default_display_value;
    } else {
        if ((value + '').endsWith('.00')) {
            return value.substring(0, value.length - 3);
        }
        return value;
    }
}

function filterJudge(num1, num2) {
    if (num2 == 0) {
        return 0;
    } else {
        return (num1 / num2).toFixed(2);
    }
}

function exportExcel() {
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    if (start_date == '' || end_date == '') {
        tip("请选择起止日期");
        return;
    } else {
        start_date += " 23:00:00";
        end_date += " 23:00:00";
        var start = new Date(start_date);
        //开始日期取前一天的23:00
        start.setDate(start.getDate() - 1);
        start_date = start.Format("yyyy-MM-dd HH:mm:ss");
    }
    window.open(getserveraddr() + "/file/operation/manage/integration?start_date=" + start_date + "&end_date=" + end_date);
}