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();
var itemArray = new Array();
/**
 * 点击查询
 */
function queryTypeClick() {
    //每次点击前清空表格数据
    $(".table-tbody").empty('');
    var t = getTimeByMonth($("#start-time").val());
    var start_date = getMomentTimeFormat(t.starttime);
    var end_date = getMomentTimeFormat(t.endtime);
    var param = {
        "dept_id": $("#fsList").val(),
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "f/statistics/score/item/info";
    var strTbody = '';
    post_common_service(url, param, function(data) {
        // 管道中心受检人平均每人次扣分
        check_people_avg(data);
    }, function(error) {
        return "";
    });
}

/**
 * 表格初始化
 */
function initQueryType() {
    var t = getTimeByMonth($("#start-time").val());
    var start_date = getMomentTimeFormat(t.starttime);
    var end_date = getMomentTimeFormat(t.endtime);

    var param = {
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "f/statistics/score/item/info";
    var strTbody = '';

    post_common_service(url, param, function(data) {
        // 管道中心受检人平均每人次扣分
        check_people_avg(data);
    }, function(error) {
        return "";
    });

}

/**
 * 表头拼接及数据传入
 * @param {*} data
 */
function getHeadHtml(data) {
    var strHead = "";
    itemArray = data.itemList;
    var colspanNum = data.itemList.length + 1;
    strHead += "<tr><th rowspan='2' width='6%'>道管整体排名</th><th rowspan='2' width='6%'>道管</th><th rowspan='2' width='6%'>管道内服务区排名</th>";
    strHead += "<th rowspan='2' width='10%'>服务区</th><th rowspan='2' width='6%'>受检次数</th>";
    strHead += "<th colspan=" + colspanNum + " height='40px'>存在问题扣分详情</th><th rowspan='2' width='6%'>管道中心受检服务区平均扣分（千分制）</th></tr><tr>";
    for(var i=0;i<data.itemList.length;i++){
        strHead += "<th width='55px'>"+data.itemList[i]+"</th>";
    }
    strHead += "<th width='55px'>扣分合计</th></tr>";
    return strHead;
}

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

        var ranking;
        var name = "";
        var rowspan = 0;
        order = 1;
        if (i == 0) {
            name = "连云港";
            rowspan = organList[0];
            ranking = rank[i];
            for (var i = 0; i < lists.length; i++) {
                if (avg > lists[i]) {
                    order = order + 1;
                }
            }
        } else if (i == organList[0]) {
            name = "盐城";
            rowspan = organList[1]-organList[0];
            ranking = rank[i];
            for (var i = 0; i < lists.length; i++) {
                if (avg > lists[i]) {
                    order = order + 1;
                }
            }
        } else if (i == organList[1]) {
            name = "南通";
            rowspan = organList[2]-organList[1];
            ranking = rank[i];
            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 += "<td width='6%'>" + ranking + "</td><td width='10%'>" + data.name + "</td><td width='6%'>" + filter(data.checked_num, '0');
        for (var i=0;i<itemArray.length;i++) {
            strTbody += "</td><td width='55px'>" + getItemScore(data, itemArray[i]);
        }
        strTbody += "</td><td width='55px'>" + data.all_check_score + "</td><td rowspan=" + rowspan + " width='6%'>" + avg + "</td>";
        strTbody += "</tr>";
    } else {
        strTbody += "<tr><td>" + rank[i] + "</td>";
        strTbody += "<td>" + data.name + "</td><td>" + filter(data.checked_num, '0');
        for (var i=0;i<itemArray.length;i++) {
            strTbody += "</td><td>" + getItemScore(data, itemArray[i]);
        }
        strTbody += "</td><td>" + data.all_check_score + "</td>";
        strTbody += "</tr>";

    }
    return strTbody;
}

function getTotalHtml(data) {
    var html = "<tr><td>合计</td><td></td><td></td><td></td><td>" + data.totalcheckedCount;
        for (var i=0;i<itemArray.length;i++) {
            html += "</td><td>" + getTotalScore(data, itemArray[i]);
        }
        html += "</td><td>" + data.totalAll + "</td><td>-</td></tr>";
    return html;
}

/**
 * 获取扣分合计
 * @param {*} obj
 * @param {*} check_item_name
 */
function getTotalScore(data, check_item_name) {
    for (var i in data) {
        // console.log(i);
        if (i.indexOf(check_item_name) > -1) {
            return data[i];
        }
    }
    return 0;
}
/**
 * 管道中心受检服务区平均扣分
 * @param {*} data
 */
function check_people_avg(data) {
    var list = data.list;
    var organList = data.organList;
    var organArray = new Array();
    var organCount = 0;
    for (h=0;h<organList.length;h++) {
        organCount += organList[h].sacount;
        organArray.push(organCount);
    }
    var dataArray = new Array();
    var sum = 0;
    var sum1 = 0;
    var id = 1;
    for (k = 0; k < list.length; k++) {
        sum = parseFloat(sum) + parseFloat(filterByZeroHandle(list[k].all_check_score, '0'));
        sum1 = parseFloat(sum1) + parseFloat(filterByZeroHandle(list[k].checked_num, '0'));
        if (k == organArray[0]-1) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
        if (k == organArray[1]-1) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
        if (k == organArray[2]-1) {
            dataArray.push(filterJudge(sum, sum1));
            sum = 0;
            sum1 = 0;
        }
    }

    // 组装表格头部
    $(".table-thead")[0].innerHTML = getHeadHtml(data);

    // 道管下的服务区排名
    ranking_method(list,organArray);
    for (i = 0; i < list.length; i++) {
        if (i == 0) {
            $(".table-tbody").append(gethtml(list[i], i, organArray, dataArray[0], dataArray));
        } else if (i == organArray[0]) {
            $(".table-tbody").append(gethtml(list[i], i, organArray, dataArray[1], dataArray));
        } else if (i == organArray[1]) {
            $(".table-tbody").append(gethtml(list[i], i, organArray, dataArray[2], dataArray));
        } else {
            $(".table-tbody").append(gethtml(list[i], i, organArray));
        }
    }
    $(".table-tbody").append(getTotalHtml(data));
}
/**
 * 道管下的服务区排名
 * @param {*} data
 */
function ranking_method(data,organArray) {
    //清空数组
    lianyungang_Station.length = 0;
    yancheng_Station.length = 0;
    nantong_Station.length = 0;
    r1.length = 0;
    r2.length = 0;
    r3.length = 0;
    rank.length = 0;
    // 把收费站得分分别添加到3个数组
    for (i = 0; i < data.length; i++) {
        if (i >= 0 && i <= organArray[0]-1)
            lianyungang_Station.push(filterByZeroHandle(filterJudge(data[i].all_check_score, data[i].checked_num), '0'));
        else if (i > organArray[0]-1 && i <= organArray[1]-1)
            yancheng_Station.push(filterByZeroHandle(filterJudge(data[i].all_check_score, data[i].checked_num), '0'));
        else
            nantong_Station.push(filterByZeroHandle(filterJudge(data[i].all_check_score, data[i].checked_num), '0'));
    }
    // 得分转化为3个排名数组
    for (var i = 1; i <= 3; i++) {
        station_rank(i);
    }
    // 将3个排名数组合并成一个数组
    rank = r1.concat(r2).concat(r3);
}

/**
 * 排名
 */
function station_rank_method(array, rank) {
    rank[0] = 1;
    for (var i = 1; i < array.length; i++) {
        if (array[i - 1] == array[i]) {
            rank[i] = rank[i - 1];
        } else {
            rank[i] = i + 1;
        }
    }
}

/**
 * 得分转化为排名
 * @param {*} type
 */
function station_rank(type) {
    if (type == 1) {
        station_rank_method(lianyungang_Station, r1);
    } else if (type == 2) {
        station_rank_method(yancheng_Station, r2);
    } else if (type == 3) {
        station_rank_method(nantong_Station, r3);
    }
}

/**
 * 过滤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 "";
    }
}

/**
 * 获取扣分详情
 * @param {*} obj
 * @param {*} check_item_name
 */
function getItemScore(obj, check_item_name) {
    var names = obj.checked_socre_name;
    var scores = obj.checked_score;

    for (var i = 0; i < names.length; i++) {
        if (names[i] != null && names[i].indexOf(check_item_name) > -1) {
            return scores[i];
        }
    }
    return 0;
}

/**
 * 过滤除数为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 t = getTimeByMonth($("#start-time").val());
    var start_date = getMomentTimeFormat(t.starttime);
    var end_date = getMomentTimeFormat(t.endtime);
    window.open(getExcelServeraddr() + "f/file/score/item/info?start_date=" + start_date + "&end_date=" + end_date);
}