var isshownumber = false;
$(function() {
    $('[data-toggle="tooltip"]').tooltip();
    setVisit();
    var road_manager;
    if (roleContains("STATION")) {
        road_manager = getCurrentUser().road_manager_id;
    }
    if (roleContains("ROAD_MANAGER")) {
        road_manager = getCurrentUser().organid;
    }
    setRoadManagerSelect("#center-manager", road_manager, true);
    if (road_manager) {
        var dept;
        if (roleContains("STATION")) {
            dept = getCurrentUser().organid;
        }
        var feeList = cache_road_manager_fee_list_map.get(road_manager);
        // $("#fsList").html(obj);
        setRoadManagerValue(feeList, "#fsList", dept, true);
        // $("#fsList").val(dept);
        // 初始化查询
        initQueryEmp(road_manager, dept);
    } else {
        // 初始化查询
        initQueryEmp(null, null);
    }
    // 道管中心选择变化
    $("#center-manager").change(function() {
            $("#fsList").empty();
            if (GV(this) != '') {
                getFsStationList(GV(this), function(options) {
                    $("#fsList").append(options);
                    var dept;
                    if (roleContains("STATION")) {
                        dept = getCurrentUser().organid;
                    }
                    $("#fsList").val(dept);
                });
            }
        })
        // end
})

function queryEmpClick() {

    //每次点击前清空表格数据
    $(".table-tbody").empty('');
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    var start_score = $("#start-score").val();
    var end_score = $("#end-score").val();
    var start_num = $("#start-num").val() != '' ? $("#start-num").val() : 0;
    var end_num = $("#end-num").val() != '' ? $("#end-num").val() : 100;
    var flag = param_method(start_score, end_score);
    if (flag == false) {
        return;
    }
    if (start_date == '' || end_date == '') {
        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 = {
        "parent_dept_id": GV("#center-manager"),
        "dept_id": GV("#fsList"),
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/business/feedback";
    getEmployeeInfosClick(url, param, start_score, end_score, start_num, end_num)
}

function initQueryEmp(parent_dept_id, dept_id) {
    setJobSelect("#jobSelect");
    var p_m = $.zui.store.get('store_param_' + current_url)
    $.zui.store.set('store_param_' + current_url, null);
    if (p_m) {

        $("#center-manager").val(p_m.parent_dept_id);
        $("#fsList").val(p_m.dept_id);
        $("#start-time").val(p_m.start_date);
        $("#end-time").val(p_m.end_date);
        $("#start-num").val(p_m.start_num);
        $("#end-num").val(p_m.end_num);
        $("#start-score").val(p_m.start_score);
        $("#end-score").val(p_m.end_score);
        $("#jobSelect").val(p_m.job_select);


        parent_dept_id = p_m.parent_dept_id;
        dept_id = p_m.dept_id;
        setJobSelect("#jobSelect", p_m.job_select);

        getFsStationList(GV("#center_manage"), function(options) {
            $("#fsList").append(options);
            var dept;
            if (roleContains("STATION")) {
                dept = getCurrentUser().organid;
            }
            $("#fsList").val(p_m.dept_id);
        });
    }

    //默认初始化前一周查所有
    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");

    }
    var start_score = $("#start-score").val();
    var end_score = $("#end-score").val();
    var start_num = $("#start-num").val() != '' ? $("#start-num").val() : 0;
    var end_num = $("#end-num").val() != '' ? $("#end-num").val() : 100;

    //默认初始化时间页面回显
    $("#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 = moment(start_date).subtract(1, "day");
    //开始日期取前一天的23:00

    start_date = start.toDate().Format("yyyy-MM-dd HH:mm:ss");

    var param = {
        "parent_dept_id": parent_dept_id,
        "dept_id": dept_id,
        "start_date": start_date,
        "end_date": end_date,
    }



    var url = "statistics/business/feedback";
    getEmployeeInfosClick(url, param, start_score, end_score, start_num, end_num);
}
/**
 * click
 * @param {*} url
 * @param {*} param
 */
function getEmployeeInfosClick(url, param, start_score, end_score, start_num, end_num) {
    post_common_service(url, param, function(data) {

        // console.log(data)

        var strTbody = '';
        var count = 0;
        var start_date_time = moment(param.end_date + "Z");
        var now_date_time = moment();
        var current_start_date = null;

        if (now_date_time.date() >= 26) {
            current_start_date = moment(now_date_time.format("YYYY-MM") + "-25 23:00:00Z").subtract(1, "month");
        } else {
            current_start_date = moment(now_date_time.format("YYYY-MM") + "-25 23:00:00Z").subtract(2, "month");
        }

        if (start_date_time.valueOf() <= current_start_date.valueOf()) {
            isshownumber = true;

        }

        for (var i = 0; i < data.length; i++) {
            var score = 1000 - filterByZeroHandle(data[i].check_score_avg, '0');
            var check_number = filter(data[i].checked_num, '0');
            var flag = judgeParamFilter(data, i, score, start_score, end_score, check_number, start_num, end_num)
            if (flag) {
                html = "";
                if (hasRole(ROLE_JICHA_ADMIN) || hasRole(ROLE_ADMIN) || hasRole(ROLE_ROAD_MANAGER)) {
                    isshownumber = true;
                    if (hasRole(ROLE_JICHA_ADMIN) || hasRole(ROLE_ADMIN)) {
                        // html += "<button id=\"user_" + data[i].user.id + "\" onClick='addTask(" + data[i].user.id + ",\"" + data[i].user.truename + "\",\"" + data[i].user.fee_station_name + "\",\"" + data[i].checked_num + "\",\"" + param.start_date + "\",\"" + param.end_date + "\")'  class=\"btn\" style='margin-left: 10px;padding: 0px;border: 0;background: none;' type=\"button\"><i class=\"icon icon-plus-sign\"></i></button>"
                    }
                }

                strTbody += "<tr><td width='4%;'>" + (count + 1) + "</td><td width='6%;'>" + data[i].user.fee_station_name + "</td>" +
                    "<td width='6%;'>" + data[i].user.truename + "</td><td width='6%;'>" + data[i].user.position_name + "</td>" +
                    "<td width='7%;'>" + data[i].user.workno + "</td>" +
                    //<td width='4%;'>" + filter(data[i].company_ranking, '/') + "</td>"
                    // "<td width='4%;'>" + filter(data[i].center_ranking, '/') + "</td><td width='4%;'>" + filter(data[i].fee_station_ranking, '/') +
                    "</td><td width='4%;'>" + filter(data[i].checked_num, '0') + "</td>" +
                    "<td width='4%;'>" + filter(data[i].score, '0') + "</td><td width='4%;'>" + filter(data[i].check_t_num, '0') + "</td>" +
                    "<td width='4%;'>" + filter(data[i].check_t_score, '0') + "</td><td width='4%;'>" + filter(data[i].check_x_num, '0') + "</td>" +
                    "<td width='4%;'>" + filter(data[i].check_x_score, '0') + "</td><td width='4%'>" + filter(data[i].check_s_num, '0') + "</td><td width='4%;'>" +
                    filter(data[i].check_s_score, '0') + "</td><td width='4%;'>" + filter(data[i].check_all_num, '0') + "</td>" +
                    "<td width='4%;'>" + filter(data[i].check_all_score, '0') + "</td><td width='4%;'>" + filterSlash(filterByZeroHandle(data[i].check_score_avg, '0'), filter(data[i].checked_num + data[i].check_t_num + data[i].check_x_num + data[i].check_s_num, '0')) + "</td>" +
                    "<td width='4%;'>" + filterSlash(score.toFixed(2), filter(data[i].check_all_num, '0')) + "</td>" + "<td width='4%;'>" + "</td></tr>";
                count++;
            }
        }
        $(".table-tbody").append(strTbody);
        $("tr:gt(1):odd").css("background", "#EFEFEF");
    }, function(error) {
        return "";
    });
}


/**
 * 对于受检次数为0，扣分和得分都为 /
 * @param {*} value , check_num 受此次数
 * @return value or /
 */
function filterSlash(value, check_num) {
    if (check_num == 0) {
        return '/';
    } else {
        return value;
    }
}

function judgeParamFilter(data, i, score, start_score, end_score, check_number, start_num, end_num) {
    var flag1 = false;
    if (score >= parseFloat(start_score) && score <= parseFloat(end_score)) {
        flag1 = true;
    } else {
        flag1 = false;
    }
    var flag2 = false;
    if (check_number >= parseFloat(start_num) && check_number <= parseFloat(end_num)) {
        flag2 = true;
    } else {
        flag2 = false;
    }

    var jobSelect = $("#jobSelect").val();
    if (jobSelect) {
        if (data[i].user.positionid == jobSelect) {
            flag3 = true;
        } else {
            flag3 = false;
        }
    } else {
        flag3 = true;
    }
    return flag1 & flag2 & flag3;
}

function average(obj) {
    if (filter(obj.checked_num + obj.checked_m_num, '0') == 0) {
        return 0;
    } else {
        return (obj.check_all_score + obj.check_all_m_score) / filter(obj.checked_num + obj.checked_m_num, '1')
    }
}

/**
 * 过滤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].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 exportExcel() {
    var dept_id = $("#fsList").val();
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    var start_score = $("#start-score").val();
    var end_score = $("#end-score").val();
    var parent_dept_id = $("#center-manager").val();
    var max_score = $("#end-score").val();
    var min_score = $("#start-score").val();
    var min_check_num = $("#start-num").val();
    var max_check_num = $("#end-num").val();
    var position_id = $("#jobSelect").val();
    var flag = param_method(start_score, end_score);
    if (flag == false) {
        return;
    }
    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");
    }
    if (!dept_id) dept_id = "";
    window.open(getserveraddr() + "/file/business/feedback?dept_id=" + dept_id +
        "&start_date=" + start_date + "&end_date=" + end_date + "&start_score=" + start_score + "&end_score=" + end_score + "&parent_dept_id=" + parent_dept_id +
        "&max_score=" + max_score + "&min_score=" + min_score + "&min_check_num=" + min_check_num + "&max_check_num=" + max_check_num + "&position_id=" + position_id);
}


function param_method(start_score, end_score) {

    if (start_score == '' || end_score == '') {
        tip("请输入得分区间");
        return false;
    } else if (!(/^[0-9]*$/).test(start_score) || !(/^[0-9]*$/).test(end_score)) {
        tip("数字不合法！");
        return false;
    } else if (parseInt(start_score) > parseInt(end_score)) {
        tip("开始得分不能比结束得分大");
        return false;
    } else if (start_score < 0 || start_score > 1000 || end_score < 0 || end_score > 1000) {
        tip("得分只能在0-1000之间");
        return false;
    } else {
        return true;
    }
}

function setVisit() {
    // if (roleContains("STATION")) {
    //     $("#center-manager").attr("disabled", "disabled");
    //     $("#fsList").attr("disabled", "disabled");
    // }
    // if(roleContains("ROAD_MANAGER")){
    // 	$("#center-manager").attr("disabled","disabled");
    // }
}