#@layout()
#define script()
<script>

    var elems = Array.prototype.slice.call(document.querySelectorAll('.myswitchery'));
    elems.forEach(function (elem) {
        var switchery = new Switchery(elem, {size: 'small'});
        var roleId = elem.getAttribute("data-rid");
        var permissionId = elem.getAttribute("data-pid");
        var groupId = elem.getAttribute("data-gid");
        if(permissionId!=null){
            elem.onchange = function () {
                if (elem.checked) {
                    addRolePermission(roleId, permissionId);
                } else {
                    delRolePermission(roleId, permissionId);
                }
            }
        }else{
            //组事件
            elem.onchange = function () {
                if (elem.checked) {
                    addGroupRolePermission(groupId,roleId);
                } else {
                    delGroupRolePermission(groupId,roleId);
                }
            }
        }
    });

    function addRolePermission(roleId, permissionId) {
        ajaxGet("#(CPATH)/admin/user/doAddRolePermission?roleId=" + roleId + "&permissionId=" + permissionId,
            function () {
                toastr.success('操作成功。');
            }
        )
    }

    function delRolePermission(roleId, permissionId) {
        ajaxGet("#(CPATH)/admin/user/doDelRolePermission?roleId=" + roleId + "&permissionId=" + permissionId,
            function () {
                toastr.success('操作成功。');
            }
        )
    }

    var type = getPara("type");
    if ("menu" == type) {
        $("#group-menu").addClass("active");
    } else if ("action" == type) {
        $("#group-action").addClass("active");
    } else {
        $("#group-all").addClass("active");
    }

    function addGroupRolePermission(groupId,roleId) {
        ajaxGet("#(CPATH)/admin/user/doAddGroupRolePermission?roleId=" + roleId  + "&groupId=" + groupId,
            function (data) {
                //返回permissionid的list,用于改变页面样式
                /**
                var permissionIds=data.permissionIds;
                permissionIds.forEach(function (permissionId) {
                    var selectId = "[data-pid='"+permissionId+"']";
                    $(selectId).attr("checked",true);
                    //$(this).parent().find('span').addClass("checked");
                    $(this).parent().find("span").attr("style","box-shadow: rgb(100, 189, 99) 0px 0px 0px 11px inset; border-color: rgb(100, 189, 99); background-color: rgb(100, 189, 99); transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s, background-color 1.2s ease 0s;");
                    $(this).parent().find("small").attr("style", "left: 13px; background-color: rgb(255, 255, 255); transition: background-color 0.4s ease 0s, left 0.2s ease 0s;");
                });**/

                toastr.options.onHidden = function () {
                    location.reload();
                }
                toastr.success('操作成功。');
            }
        )
    }

    function delGroupRolePermission(groupId,roleId) {
        ajaxGet("#(CPATH)/admin/user/doDelGroupRolePermission?roleId=" + roleId  + "&groupId=" + groupId,
            function (data) {
                //返回permissionid的list,用于改变页面样式
                /**
                var permissionIds=data.permissionIds;
                permissionIds.forEach(function (permissionId) {
                    debugger;
                    var selectId = "[data-pid='"+permissionId+"']";
                    $(selectId).removeAttr("checked");
                    //$(this).parent().find('span').attr("class",'switchery switchery-small');
                    //$(this).parent().find('span').removeClass("checked");
                    $(this).parent().find("span").attr("style","box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset;border-color: rgb(223, 223, 223);background-color: rgb(255, 255, 255);transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s;");
                    $(this).parent().find("small").attr("style", "left: 0px; transition: background-color 0.4s ease 0s, left 0.2s ease 0s;");
                });**/

                toastr.options.onHidden = function () {
                    location.reload();
                }
                toastr.success('操作成功。');
            }
        )
    }


</script>
#end

#define css()
<style>
    .switchery-small {
        margin-top: 0px;
    }

    .page-header {
        padding-bottom: 9px;
        padding-left: 20px;
        margin: 40px 0 20px;
        border-bottom: 1px solid #eee;
        font-size: 18px;
    }
</style>
#end


#define content()
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            角色权限管理
            <small>Role Permission Management</small>
        </h1>
    </section>


    <section class="content">

        <div class="row">
            <div class="col-xs-12">
                <div class="box box-primary">

                    <div class="box-header  ">
                        <div class="row">
                            <div class="col-xs-12">
                                <h2 class="page-header">
                                    <i class="fa fa-globe"></i>
                                    #if(role.isSuperAdmin())
                                    <span style="color: red">#(role.name ??)</span> 是超级管理员，默认拥有所有权限，无需设置。
                                    #else
                                    您正在设置角色 <span style="color: red">#(role.name ??)</span> 的权限
                                    #end


                                </h2>
                            </div>

                            <div class="col-xs-12" style="margin-left: 15px">
                                <div class="btn-group">
                                    <a href="#(CPATH)/admin/user/rolePermissions/#(role.id)" id="group-all"
                                       class="btn btn-default">全部</a>
                                    <a href="#(CPATH)/admin/user/rolePermissions/#(role.id)?type=menu" id="group-menu"
                                       class="btn btn-default">菜单</a>
                                    <a href="#(CPATH)/admin/user/rolePermissions/#(role.id)?type=action" id="group-action"
                                       class="btn btn-default">URL</a>
                                </div>
                            </div>
                            <!-- /.col -->
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body waterfall-container">
                        #for(x : permissionGroup)
                        <div class="waterfall-item">
                            <div class="box box-dashboard">
                                <div class="box-header with-border">
                                    <div style="float: left;margin-right: 15px;">
                                        #if(role.isSuperAdmin())
                                        <input  type="checkbox" class="myswitchery"
                                                data-gid="#(x.key ??)" data-rid="#(role.id ??)" checked disabled>
                                        #else
                                        <input  type="checkbox" class="myswitchery"
                                                data-gid="#(x.key ??)" data-rid="#(role.id ??)" #for(group:groupCheck)  #if(x.key==group.key && group.value) checked #end  #end>
                                        #end
                                    </div>
                                    <h3 class="box-title">#(x.key ??)</h3>
                                </div>
                                <!-- /.box-header -->
                                <div class="box-body no-padding">
                                    <table class="table table-striped">
                                        <tbody>
                                        <tr>
                                            <th style="width: 10px">#</th>
                                            <th>ActionKey</th>
                                            <th>描述</th>
                                            <th style="width: 80px">类型</th>
                                        </tr>
                                        #if(role.isSuperAdmin())

                                        #for(permission : x.value)
                                        <tr>
                                            <td><input type="checkbox" checked class="myswitchery"
                                                       disabled></td>
                                            <td>#(permission.action_key ??)</td>
                                            <td>#(permission.text ??)</td>
                                            <td>#(permission.typeText ??)</td>
                                        </tr>
                                        #end

                                        #else
                                        #for(permission : x.value)
                                        <tr>
                                            <td><input type="checkbox" class="myswitchery"
                                                       data-pid="#(permission.id ??)"
                                                       data-rid="#(role.id ??)"
                                                       #checkedIf(hasPermission(role,permission.id))></td>
                                            <td>#(permission.action_key ??)</td>
                                            <td>#(permission.text ??)</td>
                                            <td>#(permission.typeText ??)</td>
                                        </tr>
                                        #end
                                        #end
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        #end

                    </div>
                </div>
            </div>

        </div>

    </section>


</div>
#end
