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

    var url = "/statistics/employee/order/data?data=chek_count_show"
    post_common_service(url, "", function(data) {
            isshownumber = false;
            if (typeof(data) == "undefined" || data == "") {
                $("#show_check_text").html("隐藏");
                $("#show_check").prop("checked", false)
            } else {
                if (data == "true") {
                    isshownumber = true;
                    $("#show_check_text").html("显示");
                    $("#show_check").prop("checked", true)
                } else {
                    $("#show_check_text").html("隐藏");
                    $("#show_check").prop("checked", false)
                }
            }
            if (hasRole(ROLE_JICHA_ADMIN) || hasRole(ROLE_ADMIN)) {
                $("#show_check_div").removeClass("hidden");
            }
        },
        function(error) {

        });
    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 show_check_fun() {
    var isshow = $("#show_check").prop("checked");
    if (isshow) {
        $("#show_check_text").html("显示");
    } else {
        $("#show_check_text").html("隐藏");
    }

    var url = "/statistics/employee/order/data?data=chek_count_show$" + isshow
    post_common_service(url, "", function(data) {

    }, function(error) {});
}

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/employee/order";
    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/employee/order";
    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 userChangeStations = data["userChangeStations"];
        var userChangeStationMap = new HashMap();
        for (var i in userChangeStations) {
            var s = userChangeStations[i];
            userChangeStationMap.set(s.userid, s);
        }

        data = data["lists"];

        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;

        }

        //console.log(data)

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

                var userChangeStation = userChangeStationMap.get(data[i].user.id);
                var ischage = false;
                var tiptext = "";
                if (userChangeStation != null) {
                    ischage = true;
                    tiptext = userChangeStation.old_dept_name + ":" + userChangeStation.old_dept_check_num + "次," +
                        userChangeStation.new_dept_name + ":" + userChangeStation.new_dept_check_num + "次";
                }

                var exuserHtml = "";

                $("#th_14").html("备注")

                var showNumberHtml = (isshownumber ? (filter(data[i].checked_num, '0') + (data[i].checked_num < 6 ? html : "")) : "-");
                if (hasRole(ROLE_ADMIN) || hasRole(ROLE_JICHA_ADMIN)) {
                    showNumberHtml = (ischage ? ("<span style='color:red;' data-toggle='tooltip' data-placement='right' title=" + tiptext + ">" + userChangeStation.old_dept_check_num + "+" + userChangeStation.new_dept_check_num + "</span>") : filter(data[i].checked_num, '0')) + html;
                    exuserHtml = '<div class="switch"><input type="checkbox" onchange="exUserfun(this,' + data[i].user.id + ',' + data[i].period_id + ')" ' + (data[i].isEx ? 'checked="checked"' : '') + ' > <label>排除</label></div>';
                    $("#th_14").html("是否排除统计");
                }




                strTbody += "<tr><td width='4%;'>" + (count + 1) + "</td><td width='6%;'>" + data[i].user.fee_station_name + "</td>" +
                    "<td width='6%;'><a href='JavaScript:void(0)' onclick='jumpuser(\"" + data[i].user.id + "\",\"" + data[i].user.organid + "\")'>" + data[i].user.truename + "</a></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%;'>" + getItemScore(data[i], '环境') + "</td>" +
                    "<td width='4%;'>" + getItemScore(data[i], '仪容仪表') + "</td><td width='4%;'>" + getItemScore(data[i], '表情') + "</td>" +
                    "<td width='4%;'>" + getItemScore(data[i], '动作') + "</td><td width='4%;'>" + getItemScore(data[i], '文明用语') + "</td>" +
                    "<td width='4%;'>" + getItemScore(data[i], '工作纪律') + "</td><td width='4%'>" + getItemScore(data[i], '便民服务') + "</td><td width='4%;'>" +
                    getItemScore(data[i], '安全管理') + "</td><td width='4%;'>" + data[i].check_all_score + "</td>" +
                    "<td width='6%;'>" + showNumberHtml + "</td><td width='4%;'>" + filterSlash(filterByZeroHandle(data[i].check_score_avg, '0'), filter(data[i].checked_num + data[i].checked_m_num, '0')) + "</td>" +
                    "<td width='4%;'>" + filterSlash(score, filter(data[i].checked_num + data[i].checked_m_num, '0')) + "</td><td width='4%;'>" + "</td><td width='4%;'>" + (exuserHtml) + "</td></tr>";
                count++;
            }
        }
        $(".table-tbody").append(strTbody);
        $('[data-toggle="tooltip"]').tooltip();
        $("tr:gt(1):odd").css("background", "#EFEFEF");
    }, function(error) {
        return "";
    });
}

function exUserfun(o, u, p) {
    var check = $(o).prop("checked");

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

    if (check) {
        //add

        var url = "statistics/userex/add";
        var param = {
            "user_id": u,
            "period_id": p,
            "start_date": start_date,
            "end_date": end_date
        }
        post_common_service(url, param, function(data) {
            $(o).prop("checked", true)
        }, function(e) {
            $(o).prop("checked", false)
        });
    } else {
        //delete
        var url = "statistics/userex/delete"
        var param = {
            "user_id": u,
            "period_id": p,
            "start_date": start_date,
            "end_date": end_date
        }
        post_common_service(url, param, function(data) {
            $(o).prop("checked", false)
        }, function(e) {
            $(o).prop("checked", true)
        });

    }
}


function jumpuser(userid, deptid) {


    var param = {
        "parent_dept_id": GV("#center-manager"),
        "dept_id": GV("#fsList"),
        "start_date": $("#start-time").val(),
        "end_date": $("#end-time").val(),
        "start_num": $("#start-num").val(),
        "end_num": $("#end-num").val(),
        "start_score": $("#start-score").val(),
        "end_score": $("#end-score").val(),
        "job_select": $("#jobSelect").val()
    }
    $.zui.store.set('store_param_' + current_url, param);

    url = "/view/statistics/personal_data_statistics.html?userid=" + userid + "&deptid=" + deptid + "&jump=1"

    isnew = false;
    changePage(url);

}


function showAddTaskListView(data, widthtemp, heighttemp, start_date, end_date, checknum) {

    var tasklist = data.teamClasseList;
    var userclasslist = data.userClassCountList;

    var allheight = heighttemp;
    var height = allheight - 150 - 45;
    var allwidth = widthtemp;


    //计算日期
    var satrtday = moment(start_date).toDate();
    var endday = moment(end_date).toDate();

    var taskmap = new Map();
    var userClassmap = new Map();


    for (var i = 0; i < tasklist.length; i++) {
        var task = tasklist[i];
        if (!taskmap.get(task.work_date)) {
            taskmap.put(task.work_date, []);
        }
        var arr = taskmap.get(task.work_date);
        arr.push(task);
        taskmap.put(task.work_date, arr);
    }
    for (var i = 0; i < userclasslist.length; i++) {
        var usercount = userclasslist[i];

        var daytime = moment(usercount.start_time).toDate()
        if (daytime.getHours() >= 23) {
            daytime = new Date(daytime.valueOf() + (1000 * 60 * 60 * 24))
        }
        var work_date = daytime.Format("yyyy-MM-dd") + " 00:00";


        if (!userClassmap.get(work_date)) {
            userClassmap.put(work_date, []);
        }
        var arr = userClassmap.get(work_date);
        arr.push(usercount);
        userClassmap.put(work_date, arr);
    }






    var days = (endday.valueOf() - satrtday.valueOf()) / (1000 * 60 * 60 * 24);
    var header = "<td style='background:#e5e5e5'></td>"
    var nighttd = "<td style='background:#e5e5e5'>夜班</td>"
    var middletd = "<td style='background:#e5e5e5'>中班</td>"
    var morningtd = "<td style='background:#e5e5e5'>早班</td>"

    for (var i = 1; i <= days; i++) {
        var daytime = new Date(satrtday.valueOf() + i * 1000 * 60 * 60 * 24 - 1000 * 60 * 60 * 23);
        var workday = daytime.Format("yyyy-MM-dd HH:mm");


        var html1 = ""
        var html2 = ""
        var html3 = ""

        if (taskmap.get(workday)) {
            var userclassarry = null;
            if (userClassmap.get(workday)) {
                userclassarry = userClassmap.get(workday);
            }


            var arry = taskmap.get(workday);

            for (var j = 0; j < arry.length; j++) {
                var hasin = false;
                var tasktemp = arry[j];
                var hasmin = false;

                var taskfail = false;

                if (userclassarry != null) {
                    var d = hasinList(tasktemp, userclassarry)
                    hasin = d != null

                    if (hasin) {
                        if (d.check_status == 22) {
                            taskfail = true;
                        }
                    }
                }
                if ((moment(tasktemp.end_time).toDate()).valueOf() - (moment(tasktemp.start_time).toDate()).valueOf() < 1000 * 60 * 15) {
                    hasmin = true;
                }

                var name = (moment(tasktemp.start_time).toDate()).Format("HH:mm") + "--" + (moment(tasktemp.end_time).toDate()).Format("HH:mm")
                var classname = "btn-success";
                if (hasmin) {

                    //放开15分钟限制
                    classname = "btn-success";
                    //  classname = "btn-success disabled";
                    name += "<span class=\"label label-badge\">未</span>";
                }
                if (hasin) {
                    classname = "btn-danger disabled";
                }
                if (taskfail) {
                    classname = "btn-info disabled";
                }


                var teamid = tasktemp.class_id + "_" + (typeof(tasktemp.unregular_id) == "undefined" ? "" : tasktemp.unregular_id)

                if (tasktemp.class_type == 1) {

                    html2 += "<div class='row'><button class=\"btn " + classname + " classbtn\" id=" + teamid + "  data=" + workday + " onclick='selectTeamClass(this)'  type=\"button\">" + name + "</button></div>"
                        //  html1 +='<div class="row"><label class="btn '+classname+'"><input type="radio" name="options" id="option2">'+name+'</label></div>'
                    continue;
                }
                if (tasktemp.class_type == 2) {
                    html1 += "<div class='row'><button class=\"btn " + classname + " classbtn\" id=" + teamid + " data=" + workday + " onclick='selectTeamClass(this)'  type=\"button\">" + name + "</button></div>"
                    continue;
                }
                if (tasktemp.class_type == 3) {
                    html3 += "<div class='row'><button class=\"btn " + classname + " classbtn\" id=" + teamid + "  data=" + workday + " onclick='selectTeamClass(this)'   type=\"button\">" + name + "</button></div>"
                    continue;
                }
            }

        }

        if (userClassmap.get(workday)) {
            var teamclassarry = null;

            if (taskmap.get(workday)) {
                teamclassarry = taskmap.get(workday);
            }
            var arry = userClassmap.get(workday);

            for (var j = 0; j < arry.length; j++) {
                var hasin = false;
                var usercounttemp = arry[j];
                var taskfail = false;

                if (teamclassarry != null) {
                    var d = hasinList(usercounttemp, teamclassarry)
                    hasin = d != null

                    if (usercounttemp.check_status == 22) {
                        taskfail = true;
                    }
                }
                if (usercounttemp.check_status == 22) {
                    taskfail = true;
                }
                var classname = "btn-danger disabled";
                var name = (moment(usercounttemp.start_time).toDate()).Format("HH:mm") + "--" + (moment(usercounttemp.end_time).toDate()).Format("HH:mm") + "<span class=\"label label-badge\">删</span>";
                if (hasin) {
                    continue;
                }
                if (taskfail) {
                    classname = "btn-info disabled";
                    // name+="<span class=\"label label-badge\">无效</span>";
                }


                if (usercounttemp.class_type == 1) {
                    html2 += "<div class='row'><button class=\"btn " + classname + " classbtn\" type=\"button\">" + name + "</button></div>"
                    continue;
                }
                if (usercounttemp.class_type == 2) {
                    html1 += "<div class='row'><button class=\"btn " + classname + " classbtn\"  type=\"button\">" + name + "</button></div>"
                    continue;
                }
                if (usercounttemp.class_type == 3) {
                    html3 += "<div class='row'><button class=\"btn " + classname + " classbtn\"  type=\"button\">" + name + "</button></div>"
                    continue;
                }
            }

        }




        nighttd += "<td>" + (html3 == "" ? "无" : html3) + "</td>"
        middletd += "<td>" + (html1 == "" ? "无" : html1) + "</td>"
        morningtd += "<td>" + (html2 == "" ? "无" : html2) + "</td>"



        header += "<td style='background:#ddd'>" + daytime.Format("dd") + "</td>"
    }








    var css = '<style lang="css">' +
        '.row{' +
        '     margin-right: 0px; ' +
        '     margin-left: 0px;' +
        '}' +
        '.selecttag{' +
        '     line-height:32px' +
        '}' +
        '.table td {' +
        '     vertical-align: middle;' +
        '     text-align: center;' +
        '     border-left: 1px solid #ddd;' +
        '     border-right: 1px solid #ddd;' +
        '     border-top: 1px solid #ddd;' +
        '}' +
        '.classbtn{' +
        '     font-size:10px;' +
        '     text-align: center;' +
        '     padding: 0;' +
        '}' +
        '.spanclass{' +
        '     width:10px;' +
        '     height: 10px;' +
        '}' +
        '.gimportant{' +
        '    background:#BD7B46 !important;' +
        '    color:#ffffff !important' +
        '}' +

        '</style>';

    var table1 = '<div class="row scrollbar" style="height:' + height + 'px;width:' + (allwidth - 21) + 'px;overflow:scroll;padding-top:10px;padding-bottom:10px"><table class="table"  style="height:' + height + 'px;margin:0px">' +
        '  <tbody>' +
        '    <tr style="height:10px" >' +
        header +
        '    </tr>' +
        '    <tr>' +
        nighttd +
        '    </tr>' +
        '    <tr  style="background:#f1f1f1">' +
        morningtd +
        '    </tr>' +
        '    <tr>' +
        middletd +
        '    </tr>' +
        '  </tbody>' +
        '</table></div>';


    var tips = '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#ea644a"/><div class="col-xs-10">排班已被稽查</div></div>' +
        //    '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#F1A325"/><div class="col-xs-10">已稽查但排班被删除</div></div>'+
        '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#03B8CF"/><div class="col-xs-10">排班已稽查但任务无效</div></div>' +
        //    '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#BD7B46"/><div class="col-xs-10">已稽查但任务无效且排班已删除</div></div>'+
        '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#38B03F"/><div class="col-xs-10">排班可被稽查</div></div>' +
        //    '<div class="row" style="text-align: left;"><div class="spanclass col-xs-2 " style="background:#7EC377"/><div class="col-xs-10">排班未满15分钟</div></div>'+
        '<div class="row" style="text-align: left;"><span class=\"label label-badge\">删</span> 排班已删除</div>' +
        '<div class="row" style="text-align: left;"><span class=\"label label-badge\">未</span> 排班未满15分钟</div>';


    var selectdiv = '<div class="row" style="text-align: left;"><div class="col-xs-10">已选择的排班：</div></div>' +
        '<div class="row" style="text-align: left;"><div class="col-xs-2"/><div class="col-xs-10"><span id="selectedclass" style="font-size:10px"></span></div></div>';

    var optlist = "";
    var userlist = data.userList;
    for (var q = 0; q < userlist.length; q++) {
        var user = userlist[q];
        optlist += '  <option value="' + user.id + '">' + user.truename + '</option>'

    }

    var btn_html = "";
    if (checknum < 6) {
        btn_html = '<button class="btn btn-primary" style="margin:20px;margin-right:10px" onclick="creatTask()" type="button">生成任务</button>';
    }
    var html = css + '<div class="row row_alert" style="padding-left:10px;padding-right:10px;">' + table1 + '</div><div class="row row_alert" style="bottom: 0;position: absolute;width: 100%;text-align: center;}">' +
        '<div class="col-xs-4">' + tips + '</div><div class="col-xs-3">' + selectdiv + '</div><div class="col-xs-3"><span class="selecttag" style="text-align: right;">请分配对应的稽查人员：</span></div><div class="col-xs-2  class="selecttag""><select data-placeholder="选择稽查员" id="selectusers" class="chosen-select form-control" >' +
        optlist +
        '</select></div><div class="row row_alert">' + btn_html + '</div></div>';

    return html;
}

var data_p = null;
var layerindex = null;

function addTask(userid, username, stationname, checknum, start_date, end_date) {

    data_p = {
        "userid": userid,
        "username": username,
        "stationname": stationname,
        "checknum": checknum,
        "start_date": start_date,
        "end_date": end_date
    }

    //获取该人员排班
    var url = "teamClass/getUserClasss"
    var param = {
        "user_id": userid,
        "start_time": start_date,
        "end_time": end_date
    }

    post_common_service(url, param, function(data) {

        var allheight = 700;
        var height = allheight - 150 - 45;
        var allwidth = 1200;


        var html = showAddTaskListView(data, allwidth, allheight, start_date, end_date, checknum);



        layerindex = layer.open({
            type: 1,
            title: "生成稽查任务：" + stationname + "--" + username + "   已稽查：" + checknum + "次",
            skin: 'layui-layer-rim', //加上边框
            area: [allwidth + 'px', allheight + 'px'], //宽高
            scrollbar: false,
            content: "<div id ='layerhtml'>" + html + "</div>",
            cancel: function(index, layero) {
                layer.close(index);
                //  queryEmpClick();
                return false;
            }
        });

        $('#selectusers').chosen({
            // width:'200px',
            height: '100px',
            no_results_text: '没有找到', // 当检索时没有找到匹配项时显示的提示文本
            disable_search_threshold: 10, // 10 个以下的选择项则不显示检索框
            search_contains: true // 从任意位置开始检索
        });
    });




}

function selectTeamClass(obj) {
    $("#selectedclass").html($(obj).attr('data') + " " + $(obj).html() + "的排班");
    $("#selectedclass").attr('data', obj.id);
}

function creatTask() {

    if (parseInt(data_p.checknum) >= 6) {
        tip("已经生成了6次");
        return;
    }

    //获取classid
    var id = $("#selectedclass").attr('data');

    //获取userid
    var userid = $("#selectusers").val();
    if (id != null && typeof(id) != "undefined") {
        var ids = id.split("_");
        var classid = ids[0];
        var unregular_id = ids[1];
        var url = task_server_url + "/task/add"
        if (unregular_id == null || unregular_id == "") {
            url += '?classid=' + classid + '&userid=' + userid;
        } else {
            url += '?classid=' + classid + '&unregularid=' + unregular_id + '&userid=' + userid;
        }
        $.ajax({
            type: 'GET',
            url: url,
            contentType: "application/json",
            dataType: "json",
            async: true,
            success: function(response) {
                var data = response;
                if (data.result_code == 0) {
                    tip("生成成功", null)
                    if ($("#layerhtml").length > 0 && data_p != null) {

                        //获取该人员排班
                        var url = "teamClass/getUserClasss"
                        var param = {
                            "user_id": data_p.userid,
                            "start_time": data_p.start_date,
                            "end_time": data_p.end_date
                        }
                        post_common_service(url, param, function(data) {

                            var allheight = 700;
                            var height = allheight - 150 - 45;
                            var allwidth = 1200;

                            var html = showAddTaskListView(data, allwidth, allheight, data_p.start_date, data_p.end_date, data_p.checknum);

                            $("#layerhtml").html(html);
                            data_p.checknum = parseInt(data_p.checknum) + 1;

                            var nanme = "生成稽查任务：" + data_p.stationname + "--" + data_p.username + "   已稽查：" + (data_p.checknum) + "次";

                            layer.title(nanme, layerindex)


                        });


                    }

                } else {
                    tip(data.result_desc, null)
                }
            },
            error: function(error) {
                tip("网络错误", null)
            }
        })



    } else {
        tip("未选择任务", null)
    }
}


function hasinList(task, list) {
    var hasin = false;
    for (var p = 0; p < list.length; p++) {
        var userclasscount = list[p];
        if (typeof(task.unregular_id) == "undefined") {
            if (typeof(userclasscount.unregular_id) == "undefined") {
                if (task.class_id == userclasscount.class_id) {
                    return userclasscount;
                } else {
                    continue;
                }
            } else {
                continue;
            }
        } else {
            if (typeof(userclasscount.unregular_id) == "undefined") {
                continue;
            } else {
                if (task.class_id == userclasscount.class_id && userclasscount.unregular_id == task.unregular_id) {
                    return userclasscount;
                }
            }

        }
    }
    return null;

}


/**
 * 对于受检次数为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/employee/order?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");
    // }
}