$(function() {
    setVisit();
    var road_manager;
    if (roleContains("STATION")) {
        road_manager = getCurrentUser().road_manager_id;
    }
    if (roleContains("ROAD_MANAGER")) {
        road_manager = getCurrentUser().organid;
    }
    setRoadManagerSelect("#center_manage", road_manager);
    // 初始化部门下的收费站
    getFsStationList(GV("#center_manage"), function(obj) {
        $("#fs_station").html(obj);
        //默认选中第一个
        $("#fs_station option:first").attr("selected", 'selected');
        if (roleContains("STATION")) {
            var dept = getCurrentUser().organid;
            $("#fs_station").val(dept);
        }
        var userId;
        if (hasRole("ROLE_STATION")) {
            userId = getCurrentUser().id;
        }
        // 初始化收费站下的人数
        getFsEmployee(GV("#fs_station"), function(obj) {
            $("#fs_employee").html(obj);
            // 检索
            getFsWorkInfo(0);
        }, userId);
    },true);

    $.jeDate("#sel_month", {
        isinitVal: true,
        format: 'YYYY/MM' // 分隔符可以任意定义，该例子表示只显示年月
    });
    // 当初始化月份值
    var months = month_method_2();
    // for(var i=0; i < months.length;i++){
    //     $("#sel_month").append('<option value= "'+ months[i]  +'">'+ months[i] +'</option>');
    // }

    // // 当前月份初始化
    // $("#sel_month").val(months[months.length-1 ]);

    // 根据月份确定天数
    monthChange();
    $("#month_name").html(GV("#sel_month"));


    // 收费站中心选择
    $("#fs_station").change(function() {
        var fee_station_id = GV(this);
        if (fee_station_id == '') {
            $("#fs_employee").html('');
        } else {
            getFsEmployee(fee_station_id, function(obj) {
                $("#fs_employee").html(obj);
            });
        }
    })

    // 导管中心选择
    $("#center_manage").change(function() {
        var center_manage_id = GV(this);
        // if (center_manage_id == '') {
        //     $("#fs_station,#fs_employee").html('');
        // } else {
            getFsStationList(center_manage_id, function(obj) {
                $("#fs_station").html(obj);
                //默认选中第一个
                $("#fs_station option:first").attr("selected", 'selected');
                if (roleContains("STATION")) {
                    var dept = getCurrentUser().organid;
                    $("#fs_station").val(dept);
                }
            },true);
        // }
    })

    // 查询
    $("#searchBtn").click(function() {
            $("#month_name").html(GV("#sel_month"))
            getFsWorkInfo(1);
        })
        // end
})

/**
 * 检索出勤明细数据
 * @param {*} param
 */
function getFsWorkInfo(flag) {

    var param = {
        "start_time": timeTranslate(GV("#sel_month")),
        "end_time": (GV("#sel_month") + "/25 23:00:00").replace("/", "-").replace("/", "-")
    }

    if (GV("#fs_employee") != '') {
        param['user_id'] = GV("#fs_employee");
    } else if (GV("#fs_station") != '') {
        param['dept_id'] = GV("#fs_station");
    } else if (GV("#center_manage") != '') {
        param['parent_dept_id'] = GV("#center_manage");
    }
    //加载层
    var index = layer.load(2, { shade: false }); //0代表加载的风格，支持0-2
    post_common_service("statistics/fs/unregular/work/info", param, function(data) {
        layer.close(index);
        if (flag == 0 && $("#ca-a-table").children().length > 0) {
            return;
        }
        // 月份变化
        monthChange();
        $("#ca-a-table").empty();
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].userName != undefined) {
                    $("#ca-a-table").append(getTableContent(data[i], i + 1));
                }
            }
        }
    }, function(error) {
        layer.close(index);
    });
}

/**
 * 拼接表格内容
 * @param {*} obj
 * @param {*} seq
 */
function getTableContent(obj, seq) {
    var days = getDaysByDate1(GV("#sel_month") + "/01");
    var map_class = {};
    for (var z = 0; z < obj.subStatisticsFsWork.length; z++) {

        if (obj.subStatisticsFsWork[z].check_id != undefined) {
            if (obj.subStatisticsFsWork[z].status != undefined && obj.subStatisticsFsWork[z].status == 1) {
                map_class[getD(obj.subStatisticsFsWork[z].work_date) + "-" + obj.subStatisticsFsWork[z].class_type] = 3;
            } else {
                map_class[getD(obj.subStatisticsFsWork[z].work_date) + "-" + obj.subStatisticsFsWork[z].class_type] = 2;
            }

        } else {
            if (map_class[getD(obj.subStatisticsFsWork[z].work_date) + "-" + obj.subStatisticsFsWork[z].class_type] == null) {
                map_class[getD(obj.subStatisticsFsWork[z].work_date) + "-" + obj.subStatisticsFsWork[z].class_type] = 1;
            }
        }
    }
    var class_html = [];
    for (var d = 0; d < 3; d++) {
        var class_type = 3;
        if (d != 0) {
            class_type = d;
        }

        for (var i = 26; i <= days; i++) {
            if (map_class[i + "-" + class_type] != undefined) {
                if (map_class[i + "-" + class_type] == 1) {
                    class_html[d] += '<td>/</td width="20px">';
                } else {
                    if (map_class[i + "-" + class_type] == 3) {
                        class_html[d] += '<td class="b-grey" width="20px"></td>';
                    } else {
                        class_html[d] += '<td class="b-grey" width="20px">/</td>';
                    }

                }
            } else {
                class_html[d] += '<td width="20px"></td>';
            }
        }
        for (var i = 1; i <= 25; i++) {
            if (map_class[i + "-" + class_type] != undefined) {
                if (map_class[i + "-" + class_type] == 1) {
                    class_html[d] += '<td width="20px">/</td>';
                } else {
                    if (map_class[i + "-" + class_type] == 3) {
                        class_html[d] += '<td class="b-grey" width="20px"></td>';
                    } else {
                        class_html[d] += '<td class="b-grey" width="20px">/</td>';
                    }
                }
            } else {
                class_html[d] += '<td width="20px"></td>';
            }
        }
    }

    var content = "<tr><td rowspan='3' width='4%'>" + seq + "</td> <td rowspan='3' width='4%'>" + obj.feeStationName + "</td> <td rowspan='3' width='4%'>" + obj.userName +
        "</td> <td rowspan='3' width='4%'>" + obj.positionName + "</td><td rowspan='3' width='8%'>" + obj.workno + "</td><td width='4%'>夜班</td> " + class_html[0] + "<td rowspan='3' width='4%'>" +
        obj.work_days + "</td><td rowspan='3' width='4%'>" + convertT(obj.work_minutes) + "</td><td rowspan='3' width='4%'></td> </tr>\
        <tr><td width='4%'>早班</td> " + class_html[1] + " </tr>\
        <tr><td width='4%'>中班</td> " + class_html[2] + " </tr>";

    return content;
}

/**
 * 月份变化 , 天数变化
 */
function monthChange() {
    $(".m28").show();
    $(".m" + getDaysByDate1(GV("#sel_month") + "/01")).hide();
}

function setVisit() {
    if (roleContains("STATION")) {
        $("#center_manage").attr("disabled", "disabled");
        $("#fs_station").attr("disabled", "disabled");
    }
    if (roleContains("ROAD_MANAGER")) {
        $("#center_manage").attr("disabled", "disabled");
    }
    if (hasRole("ROLE_STATION")) {
        $("#fs_employee").attr("disabled", "disabled");
    }
}