(function(){
    X.pageLogic['projectadmin'] = {
        init : function(parms){
            var role_id,datalist;
            updataroles();
            function updataroles(){
                X.api("authz/roles","get",{},function(d){
                    datalist = d;
                    if(d[0]){
                        role_id = d[0]['_id'];
                        $(".projectadmin-list-name").html(d[0]["name"]);
                        X.laytpldata("#projectadmin-con-hz-left-list-tit-xiangmu-dot",d,".projectadmin-con-hz-left-list-tit-xiangmu");
                        updataapiinfo(d[0]['_id']);
                    }else {
                        layer.msg('暂无数据');
                    }

                    // X.laytpldata("#projectadmin-info-box-dot",d.sys_role[0]['authority'],".projectadmin-info-box");
                });
            }

            $(".menu_dapax .item_kuvn").click(function(){
                $(".menu_dapax .item_kuvn").removeClass("selected___3ctlt");
                $(this).addClass("selected___3ctlt");
                var url = $(this).attr("data-url");
            });

            $(document).on("click",".roleinproject-tit",function(){
                var index = $(this).attr("data-index");
                $(".projectadmin-con-hz-left-list-con").removeClass("selected___3ctlt");
                $(this).addClass("selected___3ctlt");
                $(".projectadmin-list-name").html(datalist[index]['name']);
                var id = $(this).attr("data-id");
                updataapiinfo(id);
                // X.laytpldata("#projectadmin-info-box-dot",data.dom_role[index]['authority'],".projectadmin-info-box");
            });

            function updataapiinfo(id){
                role_id = id;
                X.api("authz/get_permissions_for_user_in_domain","post",{role_id:id,game:X['DATA']['game']},function(d){
                    X.laytpldata("#projectadmin-info-box-dot",d,".projectadmin-info-box");
                });
            };

            $(document).off('click','.projectadmin-del').on('click','.projectadmin-del',function(){
                var index = $(this).attr('data-index');
                var id = $(this).attr('data-id');
                layer.confirm('确认删除当前api吗？', {
                    btn: ['取消','删除 '] //按钮
                  }, function(){
                    //取消
                    layer.close(layer.index);
                  }, function(){
                    //删除
                    X.api("authz/del_policy",'post',{role_id:role_id,game:X['DATA']['game'],path:id,act:"*"},function(d){
                        layer.msg('删除成功');
                        layer.close(layer.index);
                        updataapiinfo(role_id);
                    })
                });
            });

            $(document).off('click','.projectadmin-edit').on('click','.projectadmin-edit',function(params){
                var id = $(this).attr('data-id');
                var arr = [];
                arr.push(id);
                X.api("authz/add_policy",'post',{role_id:role_id,game:X['DATA']['game'],path_list:arr,act:"*"},function(d){
                    layer.close(layer.index);
                    layer.msg('添加成功');
                    updataapiinfo(role_id);
                })
            });

            $(document).off('click','.projectadmin-gongduo').on('click','.projectadmin-gongduo',function(e){
                e.stopPropagation();
                var obj = $(this);
                var offset = obj.offset();
                var id = obj.attr('data-id');
                var name = obj.attr("data-name");
                var arr = [
                    {
                        title:'重命名',
                        id:'edit'
                    },
                    {
                        title:'删除',
                        id:'del'
                    }
                ]
                X.querycriteriapop(arr,'dropdownlist',offset.left,offset.top+obj.height(),function(val){
                    if(val.id == 'del'){
                        layer.confirm('确认删除当前角色吗？', {
                            btn: ['取消','删除 '] //按钮
                          }, function(){
                            //取消
                            layer.close(layer.index);
                          }, function(){
                            //删除
                            X.api("authz/del_role_user_domain",'post',{role_id:id,game:X['DATA']['game'],username:name},function(d){
                                layer.close(layer.index);
                                layer.msg('删除成功');
                                updataroles();
                            })
                        });
                    }else {
                        var arr = {
                            id:id,
                            title:$(".projectadmin-list-name").html()
                        }
                        X.parametersopen(arr,"roleeditname",'auto',function(d){
                            updataroles();
                        })
                    }
                })
            });



           
        }
    };
})();