var cur_status;
function initTaskPage() {
    initQueryParams();
    initTableRowBtn();
    queryTask();
}
function initQueryParams() {
    // 仅选择日期
    $("#start_time").datetimepicker(
    {
        language:  "zh-CN",
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: "yyyy-mm-dd"
    });
    // 仅选择日期
    $("#end_time").datetimepicker(
    {
        language:  "zh-CN",
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: "yyyy-mm-dd"
    });
    setFeSelect("#fsList");
    //获取页面参数
    var page_params = $.zui.store.get("page_params");
    if(page_params && page_params.status) {
        cur_status = page_params.status;
    }
    if(cur_status==4){
        $("#dispatchPatchBtn").show();
        $("#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 start_time = "";
    if($("#start_time").val()) start_time = $("#start_time").val()+" 00:00:00";
    var end_time = "";
    if($("#end_time").val()) end_time = $("#end_time").val()+" 23:59:59"; 
    var data = {
        "start_time": start_time,
        "end_time": end_time,
        "checked_dept": $("#fsList").val(),
        "check_status":cur_status,
        "hasScore":$("#hasScoreList").val(),
    }
    var hide = false;
    if(hasRole(ROLE_STATION_ADMIN) || hasRole(ROLE_STATION) ||  hasRole(ROLE_JICHA)|| roleContains('STATION')) {
        hide = true;
    }
    console.log("fffffff"+hide);
    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: 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: '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){
        checkable = true;
    }
    if(cur_status==2 || cur_status==3){
        data.check_status_arr = [2,3];
    }
    $('.datatable').mytable({'cols':cols,
                            'checkable':checkable,
                            'url':"/task/getTaskList/",
                            'param':data}
                            );
}
//进入稽查页面
function showCheck(id){
    // $('#myModal').modal({name:"dd"});
    $.checkTask =getItemByIdFromArr(id,$('.datatable').mytable('getTableData'));
    $.checkTask.appeal = null;
    //开始稽查
    no_return_common_service('/task/update', {"id":$.checkTask.id,"check_status":3});
    changePage("/view/mytask/check.html");
}
//进入继续稽查页面
function showContinueCheck(id){
    // $('#myModal').modal({name:"dd"});
    $.checkTask =getItemByIdFromArr(id,$('.datatable').mytable('getTableData'));
    $.checkTask.appeal = null;
    changePage("/view/mytask/check.html");
}
//处置无效任务申请
function showApplyDetail(id){
    layer.open({
        type: 1,
        area: ['400px','500px'],
        title: '申请处理',
        closeBtn: 1,
        shadeClose: true,
        skin: 'layui-layer-lan',
        content: $('#apply_detail_div'),
        btn: ['同意', '拒绝'],
        btnAlign: 'c', //按钮居中
        shade: 0 ,//不显示遮罩
        yes: function(index){
            aggreeApply(id);
            layer.close(index);
        },
        btn2: function(index){
            refuseApply(id)
            layer.close(index);
        }
    });
    var param = {
        'id':id
    };
    post_common_service('/checkApply/getByTaskId', param, function(data) {
        $('#apply_man').val(data.apply_man);
        $('#apply_time').val(data.apply_time);
        $('#apply_reason').val(data.apply_reason);
        $('#apply_id').val(data.id);        
    });
}
//同意申请
function aggreeApply(taskId){
    var param = {
        'id':$('#apply_id').val(),
        'task_id':taskId
    };
    addOrUpdateItem4Common(param,'/checkApply/agree',queryTask);
}
//拒绝申请
function refuseApply(taskId){
     var param = {
        'id':$('#apply_id').val(),
        'task_id':taskId
    };
    addOrUpdateItem4Common(param,'/checkApply/refuse',queryTask);
}
//任务详情页面
function showTaskDetail(id){
    var rowData=getItemByIdFromArr(id,$('.datatable').mytable('getTableData'));
    $.checkTask = rowData;
    $.checkTask.appeal = null;
    changePage("/view/mytask/taskDetail.html");
}
//下发所有任务
function dispatchTaskPatch(){
    layer.confirm('确定下发全部任务？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var param = {
            // "start_time": $("#start_time").val(),
            // "end_time": $("#end_time").val(),
            // "checked_dept": $("#dept").val(),
            "check_status":4,
            "dispatch_ids":$('.datatable').mytable('getTableCheckedIds')
        }

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