var cur_status;
var cur_page;

function initTaskPage() {
    initQueryParams();
    initTableRowBtn();
    queryTask();
}

function initQueryParams() {
    // 仅选择日期
    $.jeDate("#start_time", {
        isinitVal: true,
        format: 'YYYY/MM' // 分隔符可以任意定义，该例子表示只显示年月
    });

    $("#start_time").val(getCurrentTimeFormat());
    var page_params = $.zui.store.get("page_params");
    if (page_params && page_params.status) {
        cur_status = page_params.status;
    }

    var checked_dept = null;
    if (roleContains("SERVICEAREA")) {
        checked_dept = getCurrentUser().organid;
    }
    setSASelect("#fsList", checked_dept);


    if (hasRole(ROLE_JICHA_ADMIN) && cur_status == 4) {
        $("#dispatchPatchBtn").show();
        $("#dispatchAllNoScoreBtn").show();
    }
    if (roleContains("STATION") && cur_status == 5) {
        $("#confirmPatchBtn").show();
    }
    $("#checkPersonDiv").show();

    if (cur_status == 4 || cur_status == 5 || cur_status == 6) {
        $("#hasScoreDiv").show();
    }

}
//初始化表行按钮
function initTableRowBtn() {
    $.zui.store.set("task_table_btn_2", [{ func: 'showCheck', text: '开始稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_3", [{ func: 'showCheck', text: '开始稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_7", [{ func: 'showContinueCheck', text: '继续稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_4", [{ func: 'showTaskDetail', text: '详情', icon_class: 'icon-tasks' },
        { func: 'dispatchTaskById', text: '下发', icon_class: 'icon-signin' }
    ]);
    $.zui.store.set("task_table_btn_5", [{ func: 'showTaskDetail', text: '详情', icon_class: 'icon-tasks' }]);
    $.zui.store.set("task_table_btn_6", [{ func: 'showTaskDetail', text: '详情', icon_class: 'icon-tasks' }]);
    $.zui.store.set("task_table_btn_21", [{ func: 'showApplyDetail', text: '处理', icon_class: 'icon-hand-up' }]);
}
//查询任务
function queryTask() {

    var t = getTimeByMonth(GV("#start_time"));
    var data = {
        "start_time": getMomentTimeFormat(t.starttime),
        "end_time": getMomentTimeFormat(t.endtime),
        "checked_dept": $("#fsList").val(),
        "check_status": cur_status
    };
    // console.log(data);
    var hide = false;
    if (hasRole(ROLE_FWQ_ADMIN) || hasRole(ROLE_FWQ) || hasRole(ROLE_JICHA) || roleContains('FWQ')) {
        hide = true;
    }
    var cols = [
        { width: 80, text: '序号', type: 'number', flex: true, colClass: 'text-center', field: 'num' },
        { width: 160, text: '任务名称', type: 'string', flex: true, sort: 'down', field: 'name' },
        { width: 160, text: '考核部门', type: 'string', flex: true, sort: 'down', field: 'checked_dept_name' },
        { width: 80, text: '稽查人员', type: 'string', flex: true, hide: hide, colClass: '', field: 'checkman_name' },
        { width: 160, text: '稽查时间段', type: 'string', flex: true, sort: 'down', field: 'check_period' },
        // {width: 120, text: '状态', type: 'string', flex: true, colClass: '',field: 'check_status_name'}
    ];
    var colFunc = { width: 120, text: '操作', type: 'string', flex: true, field: 'id' };
    colFunc.oper = $.zui.store.get("task_table_btn_" + cur_status);
    cols.push(colFunc);
    //只有下发才显示复选框
    var checkable = false;
    if ((hasRole(ROLE_JICHA_ADMIN) && cur_status == 4) || (roleContains("STATION") && cur_status == 5)) {
        checkable = true;
    }
    // console.log("checkable:" + checkable);
    if (cur_status == 2 || cur_status == 3) {
        data.check_status_arr = [2, 3];
        data.check_status = null;
    }
    taskTableObj = $('#task_list_table').mytable({
        'cols': cols,
        'checkable': checkable,
        'url': "/f/task/getall/",
        'param': data,
        'pager': {
            'p_cur': cur_page
        }
    });
    $.zui.store.set('store_param_' + current_url, data);
}


//进入稽查页面
function showCheck(id) {
    // $('#myModal').modal({name:"dd"});
    $.checkTask = getItemByIdFromArr(id, $('#task_list_table').mytable('getTableData'));
    $.checkTask.appeal = null;
    //开始稽查
    no_return_common_service('/f/task/update', { "id": $.checkTask.id, "check_status": 3 });

    changePage("/fwq/view/task/check.html", taskTableObj);
}

//任务详情页面
function showTaskDetail(id) {
    var rowData = getItemByIdFromArr(id, $('#task_list_table').mytable('getTableData'));
    $.checkTask = rowData;
    if (typeof($.checkTask) != "undefined" && typeof($.checkTask.appeal) != "undefined") {
        $.checkTask.appeal = null;
    }

    changePage("/fwq/view/task/taskDetail.html", taskTableObj);
}

//进入继续稽查页面
function showContinueCheck(id) {
    // $('#myModal').modal({name:"dd"});
    $.checkTask = getItemByIdFromArr(id, $('#task_list_table').mytable('getTableData'));
    $.checkTask.appeal = null;
    changePage("/fwq/view/task/check.html", taskTableObj);
}

//下发所有任务
function dispatchTaskPatch() {
    var param = {
            "check_status": 4,
            "dispatch_ids": $('#task_list_table').mytable('getTableCheckedIds')
        }
        // console.log("放假啦:"+param.dispatch_ids);
    if (!param.dispatch_ids || param.dispatch_ids.length == 0) {
        layer.msg('请选择下发任务！', {
            time: 2000 //20s后自动关闭
        });
        return;
    }
    layer.confirm('确定下发选中任务？', {
        btn: ['确定', '取消'] //按钮
    }, function() {
        no_return_common_service('/f/task/dispatch', param, function(data) {
            queryTask();
            layer.msg(data, {
                time: 2000 //20s后自动关闭
            });
        });
    }, function(index) {
        layer.close(index);
    });
    var datas = $('#task_list_table').mytable('getTableData');
    //    console.log(datas);
}


//下发单条任务
function dispatchTaskById(id) {
    layer.confirm('确定下发任务？', {
        btn: ['确定', '取消'] //按钮
    }, function() {
        var param = {
            "id": id,
            "check_status": 5
        }
        no_return_common_service('/f/task/dispatchById', param, function(data) {
            queryTask();
            layer.msg(data, {
                time: 2000 //20s后自动关闭
            });
        });
    }, function(index) {
        layer.close(index);
    });

}

function dispatchAllNoScoreTask() {
    layer.confirm('确定下发选择时间内所有无扣分任务？', {
        btn: ['确定', '取消'] //按钮
    }, function() {
        var t = getTimeByMonth(GV("#start_time"));
        var param = {
            "start_time": getMomentTimeFormat(t.starttime),
            "end_time": getMomentTimeFormat(t.endtime),
        };
        no_return_common_service('/f/task/dispatchAllNoScoreTask', param, function(data) {
            queryTask();
            layer.msg(data, {
                time: 2000 //20s后自动关闭
            });
        });
    }, function(index) {
        layer.close(index);
    });
}

function confirmTaskPatch() {
    var param = {
        "check_status": 5,
        "dispatch_ids": $('#task_list_table').mytable('getTableCheckedIds')
    }
    if (!param.dispatch_ids) {
        layer.msg("请选择需确认任务！", {
            time: 2000 //20s后自动关闭
        });
        return;
    }
    layer.confirm('确定确认选中任务？', {
        btn: ['确定', '取消'] //按钮
    }, function() {
        no_return_common_service('/f/task/confirm', param, function(data) {
            queryTask();
            layer.msg(data, {
                time: 2000 //20s后自动关闭
            });
        });
    }, function(index) {
        layer.close(index);
    });
}