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

    setSASelect("#fsList");


    var page_params = $.zui.store.get("page_params");
    if (page_params && page_params.status) {
        cur_status = page_params.status;
    }
    if (cur_status == 21) {
        $("#agreePatchBtn").show();
    }
    $("#check_zc").attr("checked", true);
    $("#check_ss").attr("checked", true);

    queryUnexaminedTask();
}
//初始化表行按钮
function initTableRowBtn() {
    $.zui.store.set("task_table_btn_21_" + ROLE_JICHA_ADMIN, [{ func: 'showCheck', text: '复查', icon_class: 'icon-eye-open' },
        { func: 'showApplyDetail', text: '处理', icon_class: 'icon-eye-open' }
    ]);
    $.zui.store.set("task_table_btn_21_" + ROLE_JICHA, [{ func: 'showApplyDetail1', text: '详情', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_22", [{ func: 'showApplyDetail1', text: '详情', icon_class: 'icon-tasks' }]);
    $.zui.store.set("task_table_btn_23", [{ func: 'showApplyDetail1', text: '详情', icon_class: 'icon-tasks' }]);
}

function queryUnexaminedTask() {
    initTableRowBtn();
    var t = getTimeByMonth($("#start_time").val());


    var check_ss = $("#check_ss").prop("checked");
    var check_zc = $("#check_zc").prop("checked");



    var data = {
        "start_time": getMomentTimeFormat(t.starttime),
        "end_time": getMomentTimeFormat(t.endtime),
        "checked_dept": $("#fsList").val(),
        "checked_person": $("#feePersonList").val(),
        "check_status": cur_status,
        "invalidFrom": (check_ss && check_zc) ? -1 : (check_ss ? 1 : (check_zc ? 0 : -2))
    };


    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, 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 colFunc = { width: 120, text: '操作', type: 'string', flex: true, field: 'id' };
    if (cur_status == 21) {
        if (hasRole(ROLE_JICHA)) {
            colFunc.oper = $.zui.store.get("task_table_btn_" + cur_status + "_" + ROLE_JICHA);
        } else if (ROLE_JICHA_ADMIN) {
            colFunc.oper = $.zui.store.get("task_table_btn_" + cur_status + "_" + ROLE_JICHA_ADMIN);
        }
    } else {
        colFunc.oper = $.zui.store.get("task_table_btn_" + cur_status);
    }
    cols.push(colFunc);
    var checkable = false;
    if (cur_status == 21 && hasRole(ROLE_JICHA_ADMIN)) {
        checkable = true;
    }
    $('#unexamined_datatable').mytable({
        'cols': cols,
        'checkable': checkable,
        'url': "/f/task/getall/",
        'param': data
    });
}
//进入稽查页面
function showCheck(id) {
    $.checkTask = getItemByIdFromArr(id, $('#unexamined_datatable').mytable('getTableData'));
    if (typeof($.checkTask) != "undefined" && typeof($.checkTask.appeal) != "undefined") {
        $.checkTask.appeal = null;
    }

    //开始稽查
    no_return_common_service('/f/task/update', { "id": $.checkTask.id, "check_status": 21, "record_status": 1, "check_status_name": "复查" });
    changePage("/fwq/view/task/check.html");
}

function showApplyDetail1(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) {
            layer.close(index);
        }
    });
    var param = {
        'id': id
    };
    $('#apply_man').val("");
    $('#apply_time').val("");
    $('#apply_reason').val("");
    $('#apply_id').val("");
    post_common_service('/f/checkApply/getByTaskId', param, function(data) {
        $('#apply_man').val(data.apply_man_name);
        $('#apply_time').val(data.apply_time);
        $('#apply_reason').val(data.apply_reason);
        $('#apply_id').val(data.id);
    });
}

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
    };
    $('#apply_man').val("");
    $('#apply_time').val("");
    $('#apply_reason').val("");
    $('#apply_id').val("");
    post_common_service('/f/checkApply/getByTaskId', param, function(data) {
        $('#apply_man').val(data.apply_man_name);
        $('#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, '/f/checkApply/agree', queryUnexaminedTask);
}

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

function agreeApplyTaskPatch() {
    var param = {
        'apply_ids': $('#unexamined_datatable').mytable('getTableCheckedIds')
    };
    addOrUpdateItem4Common(param, '/f/checkApply/agreePatch', queryUnexaminedTask);
}