var isshownumber = false;
$(function() {
    $('[data-toggle="tooltip"]').tooltip();

    var feeList = getOrganList();
    $("#select-dept").append("<option value='' >请选择清障单位</option>");
    for (var i = 0; i < feeList.length; i++) {
        $("#select-dept").append("<option value='" + feeList[i].deptid + "'>" + feeList[i].name + "</option>");
    }

    // 初始化查询
    initQueryEmp();
})

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 = {
        "positionid": $("#jobSelect").val(),
        "dept_id": $("#select-dept").val(),
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/check/wrecker/staff";
    getEmployeeInfosClick(url, param, start_score, end_score, start_num, end_num)
}

function initQueryEmp() {

    setJobSelect("#jobSelect");

    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 = {
        "positionid": $("#jobSelect").val(),
        "dept_id": $("#select-dept").val(),
        "start_date": start_date,
        "end_date": end_date,
    }

    var url = "statistics/check/wrecker/staff";
    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) {
        var strTbody = '';
        for (var i = 0; i < data.length; i++) {
            var agv = filterSlash(data[i].all_check_score, data[i].check_num);
            if (!(start_score == 0 && end_score == 1000)) {
                if (agv == '/' || agv < start_score || agv > end_score) {
                    continue;
                }
            }
            strTbody += "<tr><td width='4%;'>" + (i + 1) + "</td><td width='6%;'>" + data[i].dept_name + "</td>" +
                "<td width='5%;'>" + data[i].name + "</td><td width='5%;'>" + data[i].work_no + "</td>" +
                "<td width='5%;'>" + data[i].position_name + "</td>" +

                "</td><td width='4%;'>" + data[i].chu_jing + "</td>" +
                "<td width='4%;'>" + data[i].xian_chang + "</td><td width='4%;'>" + data[i].chang_tong + "</td>" +
                "<td width='4%;'>" + data[i].zuo_ye + "</td><td width='4%;'>" + data[i].xin_xi + "</td>" +
                "<td width='4%;'>" + data[i].zhuo_zhuang + "</td><td width='4%'>" + data[i].zuo_ye_xian_chang + "</td><td width='4%;'>" +
                data[i].wen_ming + "</td><td width='4%;'>" + data[i].cao_zuo + "</td>" +
                "<td width='4%;'>" + data[i].gong_zuo + "</td><td width='5%;'>" + data[i].private_check + "</td>" +
                "<td width='5%;'>" + data[i].in_check + "</td>" + "<td width='5%;'>" + data[i].other_check + "</td><td width='5%;'>" + data[i].all_check_score + "</td><td width='5%;'>" + agv + "</td><td width='5%;'></td><td width='5%;'>" + "</td></tr>";
        }
        $(".table-tbody").empty();
        $(".table-tbody").append(strTbody);
    }, function(error) {
        return "";
    });
}

function getOrganList() {
    var organList = $.zui.store.get("cache_wrecker_group_list");
    return organList;
}

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




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;
    }
}