// var page_size=10;
function queryUncheckedTask(){
    var data = {
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        "checked_dept": $("#fsList").val(),
        "check_status":2
    }
    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, colClass: '',field: 'checkman_name'},
                {width: 160, text: '稽查时间段', type: 'string', flex: true, sort: 'down',field: 'check_period'},
                {width: 100, text: '状态', type: 'string', flex: true, colClass: '',field: 'check_status_name'},
                {width: 100, text: '操作', type: 'string', flex: true, field: 'id',
                    oper:[
                        {func:'showCheck',text:'稽查',icon_class:'icon-eye-open'}
                    ]
                }
            ];
    
    // var pager = {
    //     page_size:10
    // }
    $('.datatable').mytable({'cols':cols,
                            'url':"/task/getTaskList/",
                            'param':data}
                            );
}

function showCheck(id){
    // $('#myModal').modal({name:"dd"});
    $.checkTask =getItemByIdFromArr(id,$('.datatable').mytable('getTableData'));
    changePage("/view/mytask/check.html");
}

function queryUnexaminedTask(){
    var data = {
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        "checked_dept": $("#fsList").val(),
        "check_status":21
    }
    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, 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'},
                {width: 100, text: '操作', type: 'string', flex: true, field: 'id',
                    oper:[
                        {func:'showApplyDetail',text:'处理',icon_class:'icon-hand-up'}
                    ]
                }
            ];
    
    // var pager = {
    //     page_size:10
    // }
    $('#unexamined_datatable').mytable({'cols':cols,
                            'url':"/task/getTaskList/",
                            'param':data}
                            );
}

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',queryUnexaminedTask);
}

function refuseApply(taskId){
     var param = {
        'id':$('#apply_id').val(),
        'task_id':taskId
    };
    addOrUpdateItem4Common(param,'/checkApply/refuse',queryUnexaminedTask);
}

function queryUndispatchedTask(){
    var data = {
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        "checked_dept": $("#fsList").val(),
        "check_status":4
    }
    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, colClass: '',field: 'checkman_name'},
                {width: 160, text: '稽查时间段', type: 'string', flex: true, sort: 'down',field: 'check_period'},
                {width: 80, text: '状态', type: 'string', flex: true, colClass: '',field: 'check_status_name'},
                {width: 160, text: '操作', type: 'string', flex: true, field: 'id',
                    oper:[
                        {func:'showTaskDetail',text:'任务详情',icon_class:'icon-tasks'},
                        {func:'dispatchTaskById',text:'下发',icon_class:'icon-signin'}
                    ]
                }
            ];
    
    // var pager = {
    //     page_size:10
    // }
    $('#undispatched_datatable').mytable({'cols':cols,
                            'url':"/task/getTaskList/",
                            'param':data}
                            );
}
function showTaskDetail(id){
    var rowData=getItemByIdFromArr(id,$('#undispatched_datatable').mytable('getTableData'));
    $.checkTask = rowData;
    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
        }

        no_return_common_service('/task/dispatch', param, function(data){
            queryUndispatchedTask();
            layer.msg(data, {
                time: 2000//20s后自动关闭
            });
        });
    }, function(index){
        layer.close(index);
    });
   
}

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

function queryDispatchedTask(){
    var data = {
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        "checked_dept": $("#fsList").val(),
        "check_status":5
    }
    var hide = false;
    if(hasRole(ROLE_STATION_ADMIN) || hasRole(ROLE_STATION)) {
        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: 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: 80, text: '状态', type: 'string', flex: true, colClass: '',field: 'check_status_name'},
                {width: 160, text: '操作', type: 'string', flex: true, field: 'id',
                    oper:[
                        {func:'showTaskDetail1',text:'任务详情',icon_class:'icon-tasks'}
                    ]
                }
            ];
    
    // var pager = {
    //     page_size:10
    // }
    $('#dispatched_datatable').mytable({'cols':cols,
                            'url':"/task/getTaskList/",
                            'param':data}
                            );
}

function showTaskDetail1(id){
    var rowData=getItemByIdFromArr(id,$('#dispatched_datatable').mytable('getTableData'));
    $.checkTask = rowData;
    changePage("/view/mytask/taskDetail.html");
}