var cur_status;
var cur_page;

function initAppealPage() {
    initQueryParams();
    initTableRowBtn();
    queryAppeal();
}

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

    });
    $("#start_time").val(getCurrentTimeFormat());
    //去上次查询保留的参数
    var store_params = $.zui.store.get('store_param_' + current_url);
    var checked_dept = null;

    if (store_params) {
        checked_dept = store_params.checked_dept;
        $("#start_time").val(moment(store_params.start_time).add(1, "month").format("YYYY/MM"));
        cur_page = store_params.currentpage;
    }

    setSASelect("#fsList", checked_dept);

    //获取页面参数
    var page_params = $.zui.store.get("page_params");
    if (page_params && page_params.status) {
        cur_status = page_params.status;
    }

    if (cur_status == 14 || cur_status == 15 || cur_status == 16) {
        $("#appealResultDiv").show();
    }
}
//初始化表行按钮
function initTableRowBtn() {
    $.zui.store.set("task_table_btn_11", [{ func: 'seeCheckAudit', text: '处理', icon_class: 'icon-tasks' }]);
    $.zui.store.set("task_table_btn_12", [{ func: 'showAppealCheck', text: '开始稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_13", [{ func: 'showAppealCheck', text: '开始稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_17", [{ func: 'showAppealContinueCheck', text: '继续稽查', icon_class: 'icon-eye-open' }]);
    $.zui.store.set("task_table_btn_14", [{ func: 'seeCheckAudit', text: '详情', icon_class: 'icon-tasks' },
        { func: 'dispatchAppealTaskById', text: '下发', icon_class: 'icon-signin' }
    ]);
    // $.zui.store.set("task_table_btn_14",[{func:'showTaskDetail',text:'详情',icon_class:'icon-tasks'}]);
    $.zui.store.set("task_table_btn_15", [{ func: 'seeCheckAudit', text: '详情', icon_class: 'icon-tasks' }]);
    $.zui.store.set("task_table_btn_16", [{ func: 'seeCheckAudit', text: '详情', icon_class: 'icon-tasks' }]);
}

function initAppealList() {
    queryTable();
}

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

    var data = {
        "start_time": getMomentTimeFormat(t.starttime),
        "end_time": getMomentTimeFormat(t.endtime),
        "checked_dept": $("#fsList").val(),
        "check_status": cur_status,
        "appeal_result": $("#appealResultSelect").val()
    }
    var hide = false;
    if (hasRole(ROLE_FWQ_ADMIN) || hasRole(ROLE_FWQ) || hasRole(ROLE_JICHA) || roleContains('FWQ')) {
        hide = true;
    }

    var appeal_result_hide = true;
    if (cur_status == 14 || cur_status == 15 || cur_status == 16) {
        appeal_result_hide = false;
    }

    var cols = [
        { width: 30, text: '序号', flex: true, colClass: 'text-center', field: 'num' },
        { width: 100, text: '任务名称', flex: true, colClass: '', field: 'name' },
        { width: 100, text: '申诉部门', flex: true, colClass: '', sort: 'down', field: 'checked_dept_name' },
        { width: 80, text: '稽查人员', type: 'string', flex: true, hide: hide, field: 'checkman_name' },
        { width: 80, text: '复核人员', type: 'string', flex: true, hide: hide, field: 'recheckman_name' },
        { width: 120, text: '稽查时间段', type: 'string', flex: true, colClass: '', field: 'check_period' },
        { width: 80, text: '申诉结果', type: 'string', flex: true, hide: appeal_result_hide, field: 'appeal_result_name' },
        // {width: 80, 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);
    if (cur_status == 12 || cur_status == 13) {
        data.check_status_arr = [12, 13];
    }
    appealTableObj = $('#appeal_list_table').mytable({
        'cols': cols,
        'url': "/f/checkAppeal/getTaskList",
        'param': data,
        'pager': {
            'p_cur': cur_page
        }
    });
    //保存本次查询参数
    $.zui.store.set('store_param_' + current_url, data);
}
//查看稽查结果
function seeCheckAudit(id, task_id) {
    // alert("task_id:"+task_id+"---id:"+id)
    // function showTaskDetail1(id){
    var rowData = getItemByIdFromArr(id, $('#appeal_list_table').mytable('getTableData'));
    post_common_service("/f/checkAppeal/getByTaskId", { "task_id": rowData.id }, function(data) {
        if (!data) {
            layer.msg('未获取到申诉内容！', {
                time: 2000 //20s后自动关闭
            });
            return;
        }
        $.checkTask = rowData;
        $.checkTask.appeal = data;
        changePage("/fwq/view/task/taskDetail.html", appealTableObj);
    });
    // }
}
//再分配
function distributionAgain(id, task_id) {
    alert("task_id:" + task_id + "---id:" + id)
}

//详情
function seeDetail(id, task_id) {
    alert("task_id:" + task_id + "---id:" + id)
}

function queryUnAssignedTable() {
    var data = {
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        "checked_dept": $("#dept_select").val(),
    }
    var hide = false;
    if (hasRole(ROLE_FWQ_ADMIN) || hasRole(ROLE_FWQ)) {
        hide = true;
    }
    var cols = [
        // {width: 30, text: '序号', flex: false, colClass: 'text-center',field:'num'},
        // {width: 100, text: '任务名称', flex: false,colClass: '',field:'name'},
        // {width: 80, text: '考核人员',  flex: false, colClass: '',field:'appeal_man_name'},
        // {width: 100, text: '考核部门',  flex: false, colClass: '',sort: 'down',field:'appeal_dept_name'},
        // {width: 80, text: '稽查人员', type: 'string', flex: false, hide:true,colClass: '',field:'verify_person_name'},
        // {width: 120, text: '稽查时间段', type: 'string', flex: false, colClass: '',field:'start_time'},
        // {width: 80, text: '状态', type: 'string', flex: false, colClass: '',field:'code_name'},
        // {width: 120, text: '上次稽查时间', type: 'string', flex: false, colClass: '',field:'start_time'},
        { 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: 80, text: '状态', type: 'string', flex: true, colClass: '', field: 'check_status_name' },
        {
            width: 100,
            text: '操作',
            flex: false,
            colClass: '',
            field: 'id',
            field_other: 'task_id',
            oper: [
                { func: 'seeCheckAudit1', text: '查看稽查结果', col_class: '' }, { func: 'seeDetail', text: '详情', col_class: '' }
            ]
        },
    ];

    $('#appeal_list_table').mytable({
        'cols': cols,
        'url': "/f/checkAppeal/getList",
        'param': data
    });
}
//进入稽查页面
function showAppealCheck(id) {
    var rowData = getItemByIdFromArr(id, $('#appeal_list_table').mytable('getTableData'));
    post_common_service("/f/checkAppeal/getByTaskId", { "task_id": rowData.id }, function(data) {
        if (!data) {
            layer.msg('未获取到申诉内容！', {
                time: 2000 //20s后自动关闭
            });
            return;
        }
        $.checkTask = rowData;
        $.checkTask.appeal = data;
        // console.log(data);
        // console.log("申诉："+$.checkTask.appeal.id);
        no_return_common_service('/f/task/update', { "id": id, "check_status": 13 })
        changePage("/fwq/view/task/check.html", appealTableObj);
    });
}
//进入继续稽查页面
function showAppealContinueCheck(id) {
    // $('#myModal').modal({name:"dd"});
    var rowData = getItemByIdFromArr(id, $('#appeal_list_table').mytable('getTableData'));
    post_common_service("/f/checkAppeal/getByTaskId", { "task_id": rowData.id }, function(data) {
        if (!data) {
            layer.msg('未获取到申诉内容！', {
                time: 2000 //20s后自动关闭
            });
            return;
        }
        $.checkTask = rowData;
        $.checkTask.appeal = data;
        changePage("/fwq/view/mytask/check.html", appealTableObj);
    });
}

//再分配
function distributionAgain(id, task_id) {
    alert("task_id:" + task_id + "---id:" + id)
}

//详情
function seeDetail(id, task_id) {
    alert("task_id:" + task_id + "---id:" + id)
}

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

}