$(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, true);
    // 初始化部门下的收费站
    getFsStationList(GV("#center_manage"), function(obj) {
        $("#fs_station").html(obj);
        var dept;
        if (roleContains("STATION")) {
            dept = getCurrentUser().organid;
        }
        $("#fs_station").val(dept);
        // 初始化收费站下的人数
        getFsEmployee(GV("#fs_station"), function(obj) {
            $("#fs_employee").html(obj);
            // 检索
            getFsWorkInfo();
        });
    });

    // 当前月份初始化
    // $("#sel_month").val(getM()-1);
    // $("#month_name").html( $("#sel_month").find("option:selected").text());
    // 时间初始化

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

    // 收费站中心选择
    $("#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);
            var dept;
            if (roleContains("STATION")) {
                dept = getCurrentUser().organid;
            }
            $("#fs_station").val(dept);
        });
        // }
    })

    // 查询
    $("#searchBtn").click(function() {
            // $("#month_name").html( $("#sel_month").find("option:selected").text()  )
            getFsWorkInfo();
        })
        // end
})

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

    var param = {
        "start_time": dateChange(new Date(GV("#start-time") + " 00:00:00"), 60 * 60 * 1000),
        "end_time": GV("#end-time") + " 23:00:00"
    }

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

    post_common_service("statistics/fs/unregular/work/person", param, function(data) {
        var seq = 1;
        $(".table-tbody").empty();
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].userName != undefined) {
                    $(".table-tbody").append(getTableContent(data[i], seq++));
                }
            }
        }
    }, function(error) {});
}

/**
 * 拼接表格内容
 * @param {*} obj
 * @param {*} seq
 */
function getTableContent(obj, seq) {

    var map_class = {};
    var workdays = 0;
    for (var z = 0; z < obj.subStatisticsFsWork.length; z++) {
        map_class[obj.subStatisticsFsWork[z].class_type] = obj.subStatisticsFsWork[z].work_days;
        workdays += GDV(obj.subStatisticsFsWork[z].work_days, 0);
    }

    var content = "<tr><td width='8%'>" + seq + "</td> <td  width='16%'>" + obj.feeStationName + "</td> <td  width='16%'>" + obj.userName +
        "</td> <td  width='16%'>" + obj.positionName + "</td><td width='50px'>" + GDV(map_class[3], 0) + "</td><td width='50px'>" + GDV(map_class[1], 0) + "</td><td width='50px'>" + GDV(map_class[2], 0) + "</td><td width='16%'>" + workdays + "</td><td>" + "</td> </tr>";

    return content;
}

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