
//激活用户
function deleteRoleByIndex(index) {
    //先进行密码确认
    var username = $("#username").val();
    confirmIndentity(username, function () {
        var roleIndex = index;
        layer.confirm('确定删除？', function (index) {
            $.ajax({
                type: "POST",
                url: "/pqs9900/role/deleteRoleByIndex",
                dataType: "text",
                data: {
                    index: roleIndex
                },
                success: function (data) {
                    var info = $.parseJSON(data);
                    if (info.code === 500) {
                        //删除角色异常、或者失败
                        layer.msg(info.message, {
                            icon: 5,
                            time: 1000
                        }, function () {
                            refresh();
                        });
                    }else if (info.code === 400) {
                        //密码验证失败
                        layer.msg(info.message, {
                            icon: 5,
                            time: 1000
                        }, function () {
                            refresh();
                        });
                    } else {
                        //删除角色成功
                        layer.msg(info.message, {
                            icon: 6,
                            time: 1000
                        }, function () {
                            refresh();
                        });
                    }
                }
            });
            layer.close(index);
        });
    })
}


$(document).ready(function () {
    refresh();
});
$("#addRole").click(function () {
    //先进行密码确认
    var username = $("#username").val();
    confirmIndentity(username, function () {
        layer.open({
            type: 2,
            title: '新增角色',
            shadeClose: true,
            shade: 0.3,
            area: ['380px', '300px'],
            maxmin: false,
            content: 'addrole', //iframe的url
            scrollbar: false,
            end: function () {
                layer.close();
            }
        });
    })

});

function refresh() {
    $.ajax({
        type: "POST",
        url: "/pqs9900/role/getRoleList",
        dataType: "text",
        success: function (data) {
            var info = $.parseJSON(data);
            if (info.code === 500) {
                $('#example').DataTable({
                    columnDefs: [{
                        orderable: false,
                        targets: 3
                    }],
                    data: [],
                    destroy: true,
                    lengthMenu: [200],
                    language: {
                        url: '/pqs9900/json/chine.json'
                    }
                });
            } else {
                var dataTable =$('#example').DataTable({
                    columnDefs: [{
                        orderable: false,
                        targets: 3
                    }],
                    data: info.body,
                    destroy: true,
                    lengthMenu: [200],
                    language: {
                        url: '/pqs9900/json/chine.json'
                    }
                });
                new $.fn.dataTable.Buttons(dataTable, {
                    buttons: [
                        {
                            extend: 'csv',//导出格式
                            title: '角色列表',//导出csv名称
                            exportOptions: {
                                columns: [0, 1, 2],//导出需要的列
                                format: {
                                    body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                                        return data;
                                    }
                                }
                            }
                        }
                    ]
                });
                modifyCss(dataTable);
            }
        }
    });
}

function modifyCss(dataTable) {
    dataTable.buttons().container().css("display", "inline-block");
    dataTable.buttons().container().css("float", "right");
    dataTable.buttons().container().css("top", "4px");
    dataTable.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    dataTable.buttons().container().appendTo('.header');
    dataTable.buttons().container().after($("#fliteData")[0]);
}