$(function(){
    var table = $('#table1');
    var token = $.cookie("token");
    laydate.render({
        elem: '#greetingTimes',
        range: '~',
        theme: '#00a1cb',
    });
    $('.search').on('click',function(){
        initTable(token);
    })
    //列表
    initTable(token);
    //部门
    getDepartment();

    function initTable(token) {
        //先销毁表格
        table.bootstrapTable('destroy');
        //初始化表格,动态从服务器加载数据
        table.bootstrapTable({
            method: "get", //使用get请求到服务器获取数据
            url: huayi.config.callcenter_url + "Appeal/GetList", //获取数据的Servlet地址
            contentType: 'application/x-www-form-urlencoded',
            //			url: "kehu.json", //获取数据的Servlet地址
            striped: true, //表格显示条纹
            pagination: true, //启动分页
            pageSize: 10, //每页显示的记录数
            pageNumber: 1, //当前第几页
            pageList: [10, 15, 20, 50, 100], //记录数可选列表
            search: false, //是否启用查询
            showColumns: false, //显示下拉框勾选要显示的列
            showRefresh: false, //显示刷新按钮
            sidePagination: "server", //表示服务端请求
            //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
            //设置为limit可以获取limit, offset, search, sort, order
            queryParamsType: "undefined",
            queryParams: function queryParams(params) { //设置查询参数
                var param = {
                    page: params.pageNumber,
                    pagesize: params.pageSize,
                    token: token,
                    startdate:$('#greetingTimes').val() && $('#greetingTimes').val().split(' ~ ')[0],//开始时间
                    enddate:$('#greetingTimes').val() && $('#greetingTimes').val().split(' ~ ')[1],//结束时间
                    deptid: $("#search-department").val(), //申诉部门id
                    //logindate: $('.loginTime').val() //登录时间
                };
                return param;
            },
            responseHandler: function (res) {
             return {
                 "total": res.total,
                 "rows": res.dt   
                 };
     
            },
            onLoadSuccess: function () { //加载成功时执行
                //layer.msg("加载成功");
            },
            onLoadError: function () { //加载失败时执行
                //layer.msg("加载数据失败", { time: 1500, icon: 2 });
            }
        });
    }
    //添加
    $('.add').click(function () {
        layer.open({
            type: 2,
            title: '添加申诉信息',
            maxmin: true, //开启最大化最小化按钮
            area: ['40%', '60%'],
            content: '../CommonHtml/addAppealOrder.html'
        });
    })
    //修改
    var did;
    $('.change').click(function () {
        var ids = $.map(table.bootstrapTable('getSelections'), function (row) {
            return row.F_Id;
        });
        did = ids.toString();
        if (ids.length !== 1) {
            layer.confirm('请选择一行进行修改！', {
                btn: ['确定']
            });
            return;
        } else {
            layer.open({
                type: 2,
                title: '修改申诉信息',
                maxmin: true, //开启最大化最小化按钮
                area: ['60%', '60%'],
                content: '../CommonHtml/addAppealOrder.html?ids=' + did
            });
        }
    })
    //删除
    var delid;
    $('.del').click(function () {
        var ids = $.map(table.bootstrapTable('getSelections'), function (row) {
            return row.F_Id;
        });
        delid = ids;
        if (ids.length <= 0) {
            layer.confirm('请选择要删除的行！', {
                btn: ['确定']
            });
            return;
        } else {
            var laye = layer.confirm('您确定要删除吗？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function () {
                //按钮【按钮一】的回调
                $.ajax({
                    type: "post",
                    url: huayi.config.callcenter_url + "Appeal/DelAppeal",
                    async: true,
                    dataType: "json",
                    data: {
                        token: token,
                        ids: delid
                    },
                    success: function (data) {
                        if (data.state.toLowerCase() == 'success') {
                            layer.msg("删除成功！");
                            table.bootstrapTable('refresh');
                        }
                    }
                });
            }, function (index) {
                //按钮【按钮二】的回调
                layer.close(laye)
            });
        }
    })


    //获取部门
    function getDepartment() {
        $.ajax({
            type: "get",
            url: huayi.config.callcenter_url + "Department/GetDeptList",
            dataType: 'json',
            async: true,
            data: {
                token: token,
            },
            success: function(result) {
                var data = result.data;
                $(data).each(function(i, n) {
                    $("#search-department").append("<option value='" + n.F_DeptId + "'>" + n.F_DeptName + "</option>")
                })
            }
        })
    }

})

//格式化 申诉通过
function formatterAppealState(val, row) {
    if (val === 0) {
        return "未处理";
    } else if (val === 1) {
        return "通过";
    } else if (val === 2) {
        return "退回";
    }
}

//格式化 申诉信息
function formatterAppealInformation(val, row) {
    if (val === 1) {
        return "办理情况";
    } else if (val === 2) {
        return "查收情况";
    } else if (val === 3) {
        return "办理规范";
    } else if (val === 4) {
        return "回访情况";
    } else if (val === 5) {
        return "退回情况";
    } else if (val === 6) {
        return "知识库索引更新不及时";
    } else if (val === 7) {
        return "知识库纠错情况";
    } else if (val === 8) {
        return "通报";
    } 
}

//操作
function formatterOperation(val, row) {
    if (row.F_State === 0) {
        return '<a href="javascript:;" class="operation-btn" authorize="yes" id="HY_dealWith_' + row.F_Id + '" onclick="btn_dealWith(\'' + row.F_Id + '\')" title="审核" style="margin-right: 10px">审核</a>' + '<a href="javascript:;" class="operation-btn" authorize="yes" id="HY_dealWith_' + row.F_Id + '" onclick="btn_checkDetails(\'' + row.F_Id + '\')" title="查看详情">查看详情</a>';
    } else {
        return '<a href="javascript:;" class="operation-btn" authorize="yes" id="HY_dealWith_' + row.F_Id + '" onclick="btn_checkDetails(\'' + row.F_Id + '\')" title="查看详情">查看详情</a>';
    }
}

function  btn_checkDetails (ids) {
    did = ids.toString();
    layer.open({
        type: 2,
        title: '查看详情',
        maxmin: true, //开启最大化最小化按钮
        area: ['40%', '75%'],
        content: '../CommonHtml/appealOrderDetails.html?ids=' + did
    });
}

//审核申诉
function btn_dealWith(ids) {
    did = ids.toString();
    layer.open({
        type: 2,
        title: '审核申诉',
        maxmin: true, //开启最大化最小化按钮
        area: ['40%', '50%'],
        content: '../CommonHtml/dealWithAppeal.html?ids=' + did
    });
}