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/score/item/info";
    var strTbody = '';
    post_common_service(url, param, function(data) {
        // 管道中心受检人平均每人次扣分
        check_people_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/score/item/info";
    var strTbody = '';

    post_common_service(url, param, function(data) {
        // 管道中心受检人平均每人次扣分
        check_people_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) {

        var ranking;
        var name = "";
        var rowspan = 0;
        order = 1;
        if (i == 0) {
            name = "连云港";
            rowspan = 7;
            ranking = rank[i];
            for (var i = 0; i < lists.length; i++) {
                if (avg > lists[i]) {
                    order = order + 1;
                }
            }
        } else if (i == 7) {
            name = "盐城";
            rowspan = 6;
            ranking = rank[i];
            for (var i = 0; i < lists.length; i++) {
                if (avg > lists[i]) {
                    order = order + 1;
                }
            }
        } else if (i == 13) {
            name = "南通";
            rowspan = 9;
            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='8%'>" + data.name + "</td><td width='6%'>" + filter(data.people_num, '0') +
            "</td><td width='6%'>" + filter(data.checked_people_num, '0') +
            "</td><td width='6%'>" + filter(data.checked_num, '0') + "</td><td width='54px'>" + getItemScore(data, '环境卫生') +
            "</td><td width='54px'>" + getItemScore(data, '仪容仪表') + "</td><td width='54px'>" + getItemScore(data, '表情') +
            "</td><td width='54px'>" + getItemScore(data, '动作') + "</td><td width='54px'>" + getItemScore(data, '文明用语') +
            "</td><td width='54px'>" + getItemScore(data, '工作纪律') + "</td><td width='54px'>" + getItemScore(data, '便民服务') +
            "</td><td width='54px'>" + getItemScore(data, '安全管理') + "</td><td>" + data.all_check_score + "</td><td width='6%'>" +
            filterByZeroHandle(filterJudge(data.all_check_score, data.checked_num), '0') +
            "</td><td rowspan=" + rowspan + " width='6%'>" + avg + "</td>";
        strTbody += "</tr>";
    } else {
        strTbody += "<tr><td>" + rank[i] + "</td>";
        strTbody += "<td>" + data.name + "</td><td>" + data.people_num + "</td><td>" +
            filter(data.checked_people_num, '0') + "</td><td>" + filter(data.checked_num, '0') +
            "</td><td>" + getItemScore(data, '环境卫生') + "</td><td>" + getItemScore(data, '仪容仪表') +
            "</td><td>" + getItemScore(data, '表情') + "</td><td>" + getItemScore(data, '动作') +
            "</td><td>" + getItemScore(data, '文明用语') + "</td><td>" + getItemScore(data, '工作纪律') +
            "</td><td>" + getItemScore(data, '便民服务') + "</td><td>" + getItemScore(data, '安全管理') +
            "</td><td>" + data.all_check_score + "</td><td>" +
            filterByZeroHandle(filterJudge(data.all_check_score, data.checked_num), '0') + "</td>";
        strTbody += "</tr>";

    }
    return strTbody;
}

function getTotalHtml(data) {
    var html = "<tr><td>合计</td><td></td><td></td><td></td><td>" + data.totalMan + "</td><td>" + data.totalcheckedMan + "</td><td>" + data.totalcheckedCount + "</td><td>" +
        getTotalScore(data, '环境卫生') + "</td><td>" + getTotalScore(data, '仪容仪表') + "</td><td>" + getTotalScore(data, '表情') + "</td><td>" + getTotalScore(data, '动作') +
        "</td><td>" + getTotalScore(data, '文明用语') + "</td><td>" + getTotalScore(data, '工作纪律') + "</td><td>" + getTotalScore(data, '便民服务') +
        "</td><td>" + getTotalScore(data, '安全管理') + "</td><td>" + data.totalAll + "</td><td>-</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 dataArray = new Array();
    var sum = 0;
    var sum1 = 0;
    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 == 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;
        }
    }
    // 道管下的收费站排名
    ranking_method(list);

    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));
}
/**
 * 道管下的收费站排名
 * @param {*} data
 */
function ranking_method(data) {
    //清空数组
    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 <= 6)
            lianyungang_Station.push(filterByZeroHandle(filterJudge(data[i].all_check_score, data[i].checked_num), '0'));
        else if (i > 6 && i <= 12)
            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 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/score/item/info?start_date=" + start_date + "&end_date=" + end_date);
}