<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>

    /* *************************** 模块初始化 ***************************** */
    var authority = {
        grid: null,
        $layout: $("#div_wrapper"),//布局对象
        $input: $("#div_wrapper_left_inp_search"),//搜索框对象
        $leftgrid: $("#div_left_grid"),//左边的datagrid
        $options: $("input[name='options']"),//导航切换
        $btnAllow: $("#div_allow"),//全部允许
        $btnForbid: $("#div_forbid"),//全部禁止
        $rightgrid: null,//右边的datagrid
        currentRoleId: null,//选中的当前角色ID

        initOption: function () {
            $("#buttons").css("margin-left", window.screen.width / 3 - 150);

            this.initMenuGrid();
        },
        initMenuGrid: function () {

            authority.$rightgrid = $("#div_right_grid").ligerGrid({
                url: '${contextRoot}/authority/getMenuList',
                columns: [
                    {display: '菜单功能', id: 'name', name: 'name', width: '70%'},
                    {
                        display: '操作', name: 'operator', width: '25%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        if (row.checked == "true") {
                            if (row.type == "menu") {
                                html += ' <a id="' + row.id + '" class=\"m-btn-off\" onclick="javascript:authority.change(\'' + row.id + '\',\'1\',\'false\')"></a>';
                            } else {
                                html += ' <a id="' + row.id + '" class=\"m-btn-off\" onclick="javascript:authority.change(\'' + row.id + '\',\'2\',\'false\')"></a>';
                            }
                        } else {
                            if (row.type == "menu") {
                                html += ' <a id="' + row.id + '" class=\"m-btn-on\"  onclick="javascript:authority.change(\'' + row.id + '\',\'1\',\'true\')"></a>';
                            } else {
                                html += ' <a id="' + row.id + '" class=\"m-btn-on\"  onclick="javascript:authority.change(\'' + row.id + '\',\'2\',\'true\')"></a>';
                            }
                        }
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                usePager: false,//不显示分页
                scrollToPage: false, tree: {
                    columnId: 'name',
                    idField: 'id',
                    parentIDField: 'pid'
                }
            });
        },

        change: function (id, type, flag) {

            var url = "";
            if (type == 1) {
                url = "${contextRoot}/authority/changeRoleMenu";
            } else {
                url = "${contextRoot}/authority/changeRoleAction";
            }
            $.ajax({ //ajax处理
                type: "POST",
                url: url,
                dataType: "json",
                data: {id: id, roleId: authority.currentRoleId, flag: flag},
                cache: false,
                success: function (data) {
                    if (data.successFlg) {
                        if (flag == "true") {
                            if (type == 1) {
                                $("#" + id).removeClass("m-btn-on").addClass("m-btn-off").attr("onclick", 'javascript:authority.change(\'' + id + '\',\'1\',\'false\')');
                            } else {
                                $("#" + id).removeClass("m-btn-on").addClass("m-btn-off").attr("onclick", 'javascript:authority.change(\'' + id + '\',\'2\',\'false\')');
                            }
                        } else {
                            if (type == 1) {
                                $("#" + id).removeClass("m-btn-off").addClass("m-btn-on").attr("onclick", 'javascript:authority.change(\'' + id + '\',\'1\',\'true\')');
                            } else {
                                $("#" + id).removeClass("m-btn-off").addClass("m-btn-on").attr("onclick", 'javascript:authority.change(\'' + id + '\',\'2\',\'true\')');
                            }
                        }
                    }
                    else {
                        $.ligerDialog.error(data.message);
                    }
                },
                error: function (data) {
                    $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                }
            });
        },
        initleftGrid: function () {

            var me= this;
            $("#div_wrapper_left_inp_search").ligerSearch({
                width:180,
                onClick:function(value){
                    debugger
                    me.grid.set({
                        parms: {name:$('#div_wrapper_left_inp_search').val()}
                    });

                    me.grid.reload();
                }
            });

           me.grid= authority.$leftgrid = this.$leftgrid.ligerGrid({
                url: '${contextRoot}/role/getRoleList',
                columns: [
                    {display: '角色名称', id: 'name', name: 'name', width: '90%', align: "center"}
                ],
                parms: {rows: 10000},
                usePager: false,
                onSelectRow: function (rowdata, rowid, rowobj) {
                    authority.currentRoleId = rowdata.id;
                    authority.$rightgrid.loadServerData({id: rowdata.id});
                }

            });

        },
        initlayout: function () {

            this.$layout.ligerLayout({
                leftWidth: 200,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false//左边区域不可以隐藏
            });
            $("#div_wrapper .l-layout-left").css({"border": "0px"});
            $("#div_wrapper .l-layout-right").css({"border": "0px"});
            $("#div_wrapper .l-layout-center").css({"border": "0px"});
            $("#div_wrapper .l-layout-top").css({"border": "0px"});
            $("#div_wrapper .l-layout-left .l-layout-header").hide();


            this.$btnAllow.click(function () {
                if (!authority.currentRoleId) {
                    $.ligerDialog.error("请选择角色");
                    return;
                }
                jQuery.ligerDialog.confirm("确认开启全部权限", function (confirm) {
                    if (confirm) {
                        $.ajax({ //ajax处理
                            type: "POST",
                            url: "${contextRoot}/authority/allowAll",
                            dataType: "json",
                            data: {roleId: authority.currentRoleId},
                            cache: false,
                            success: function (data) {
                                if (data.successFlg) {
                                    $.ligerDialog.success(data.message);
                                    authority.$rightgrid.loadServerData({id: authority.currentRoleId});
                                }
                                else {
                                    $.ligerDialog.error(data.message);
                                }
                            },
                            error: function (data) {
                                $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                            }
                        });
                    }
                });
            });
            this.$btnForbid.click(function () {
                if (!authority.currentRoleId) {
                    $.ligerDialog.error("请选择角色");
                    return;
                }
                jQuery.ligerDialog.confirm("确认关闭全部权限", function (confirm) {
                    if (confirm) {
                        $.ajax({ //ajax处理
                            type: "POST",
                            url: "${contextRoot}/authority/forbidAll",
                            dataType: "json",
                            data: {roleId: authority.currentRoleId},
                            cache: false,
                            success: function (data) {
                                if (data.successFlg) {
                                    $.ligerDialog.success(data.message);
                                    authority.$rightgrid.loadServerData({id: authority.currentRoleId});
                                }
                                else {
                                    $.ligerDialog.error(data.message);
                                }
                            },
                            error: function (data) {
                                $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                            }
                        });
                    }
                });
            });

        },
        init: function () {
            this.initlayout();
            this.initleftGrid();
            this.initOption();
        }
    };

    $(function () {
        authority.init();
    });
</script>