var layer1 = null;
var datatable = null;

var p_deptid = null;
var p_name = null;
var p_status = null
var pageindex = 1
$(function() {
    // 设置滚动表格的高度
    $("#scrollTable").height($(window).height() - 360);
    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);
    // 当前月份初始化
    // $("#sel_month").val(getM()-1);
    $.jeDate("#sel_month", {
        isinitVal: true,
        format: 'YYYY/MM' // 分隔符可以任意定义，该例子表示只显示年月
    });

    $("#month_name").html($("#sel_month").val());

    var pdata = $.zui.store.get('store_param_' + current_url);
    if (pdata != null) {
        $.zui.store.set('store_param_' + current_url, null);

        $("#sel_month").val(pdata.sel_month);
        $("#fs_station").val(pdata.fs_station);
        $("#center_manage").val(pdata.center_manage);

        p_deptid = pdata.deptid;
        p_name = pdata.name;
        p_status = pdata.status;
        pageindex = pdata.p_cur;

        getappleTaskList(p_deptid, p_name, p_status)

    }


    // if (road_manager) {
    getFsStationList(road_manager, function(obj) {
        $("#fs_station").html(obj);
        var dept;
        if (roleContains("STATION")) {
            dept = getCurrentUser().organid;
        }
        $("#fs_station").val(dept);
        // 初始化查询
        getFsWorkInfo();
    });
    // } else {
    //     // 初始化查询
    //     getFsWorkInfo();
    // }
    // 导管中心选择
    $("#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").val())
            getFsWorkInfo();
        })
        // 导出excel
    $("#exportExcel").click(function() {
        var param = '';
        if (GV("#fs_station") != '' && GV("#fs_station") != null) {
            param = "&deptId=" + GV("#fs_station");
        } else if (GV("#center_manage") != '') {
            param = "&centerId=" + GV("#center_manage");
        }
        window.location.href = getserveraddr() + "/file/appeal/info?month=" + GV("#sel_month") + param;
    })



    // end
})

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

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

    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/check/appeal", param, function(data) {

        $(".table-tbody").empty();
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                $(".table-tbody").append(getTableContent(data[i], i + 1));
            }
        }
    }, function(error) {});
}

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



    var content = "<tr><td width='8%'>" + seq + "</td> <td  width='16%'>" + obj.fee_station_name + "</td> <td  width='16%'>" + GDV(obj.appeal_num, 0) + "&nbsp;&nbsp;&nbsp;" + (GDV(obj.appeal_num, 0) > 0 ? ("<a href='javascript:void(0)' onclick='getappleTaskList(" + obj.dept_id + ",\"" + obj.fee_station_name + "\",0)'>" + "<i class=\"icon icon-th-list\"></i>" + "</a>") : "") +
        "</td> <td >" + GDV(obj.appeal_success_num, 0) + "&nbsp;&nbsp;&nbsp;" + (GDV(obj.appeal_success_num, 0) > 0 ? ("<a href='javascript:void(0)' onclick='getappleTaskList(" + obj.dept_id + ",\"" + obj.fee_station_name + "\",1)'>" + "<i class=\"icon icon-th-list\"></i>" + "</a>") : "") + "</td><td>" +
        GDV(obj.appeal_fail_num, 0) + "&nbsp;&nbsp;&nbsp;" + (GDV(obj.appeal_fail_num, 0) > 0 ? ("<a href='javascript:void(0)' onclick='getappleTaskList(" + obj.dept_id + ",\"" + obj.fee_station_name + "\",2)'>" + "<i class=\"icon icon-th-list\"></i>" + "</a>") : "") +
        "</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");
    }
}


//获取任务列表  status 为检索条件 0 为全部 1 为成功 2 为失败
function getappleTaskList(deptid, name, status) {

    p_deptid = deptid;
    p_name = name;
    p_status = status


    var title = name + " " + $("#sel_month").val() + "月份" + (status == 0 ? "所有" : status == 1 ? "成功" : "失败") + "申诉任务列表"
    var isshownumber = false;

    if (hasRole(ROLE_JICHA_ADMIN) || hasRole(ROLE_ADMIN)) {
        isshownumber = true;
    }

    var cols = [
        { width: 80, text: '序号', type: 'number', flex: true, colClass: 'text-center', field: 'num' },
        { width: 160, text: '任务名称', type: 'href_link', flex: true, sort: 'down', field: '<a href=\"javascript:void(0);\" onclick=\"jumptodetail(\'$field=id$\')\">$field=name$</a>' },
        { width: 80, text: '考核人员', type: 'string', flex: true, colClass: '', field: 'checked_person_name' },
        { width: 160, text: '考核部门', type: 'string', flex: true, sort: 'down', field: 'checked_dept_name' },
        { width: 80, text: '稽查人员', type: 'href_link', flex: true, hide: false, colClass: '', field: isshownumber ? '$field=checkman_name$' : '-' },
        { width: 160, text: '稽查时间段', type: 'string', flex: true, sort: 'down', field: 'check_period' }
    ];

    var data = {
        "deptId": deptid,
        "start_time": timeTranslate(GV("#sel_month")),
        "end_time": (GV("#sel_month") + "/25 23:00:00").replace("/", "-").replace("/", "-"),
        "status": status == 0 ? null : status
    }

    layer1 = layer.open({
        area: ['50%', '50%'],
        type: 1,
        title: title,
        closeBtn: 1,
        shadeClose: true,
        content: "<div id='tasktable' style='padding:10px'></div>",
        success: function() {
            datatable = $('#tasktable').mytable({
                'cols': cols,
                'url': "/task/gettasklistbydept",
                'param': data,
                'pager': {
                    'page_size': 10,
                    'p_cur': pageindex
                }
            });
            pageindex = 1;

            console.log(datatable)
        }

    });

}



function jumptodetail(id) {
    if (layer1 != null) {
        layer.close(layer1)
    }


    post_common_service("task/getById", { "id": id }, function(data) {

        $.checkTask = data;
        if (typeof($.checkTask) != "undefined" && typeof($.checkTask.appeal) != "undefined") {
            $.checkTask.appeal = null;
        }

        var sel_month = $("#sel_month").val();
        var fs_station = $("#fs_station").val();
        var center_manage = $("#center_manage").val();

        var pdata = {
            "sel_month": sel_month,
            "fs_station": fs_station,
            "center_manage": center_manage,
            "deptid": p_deptid,
            "name": p_name,
            "status": p_status,
            "p_cur": datatable.options.pager.p_cur
        }

        $.zui.store.set('store_param_' + current_url, pdata);

        url = "/view/mytask/taskDetail.html"

        post_common_service("/checkAppeal/getByTaskId", { "task_id": id }, function(data) {
            $.checkTask.appeal = data;
            changePage(url);
        });

    }, function(error) {

    });
}