(function(){
    X.pageLogic['adduser'] = {
        init : function(){

            $(".adduser-top-box img").click(function(){
                layer.closeAll();
            })
            
            var projectrolearr,projectrolearrlist=[];
            X.api("authz/roles","get",{},function(d){
                projectrolearr = d;
                for(let i in projectrolearr){
                    var arr={
                        id:projectrolearr[i]['_id'],
                        title:projectrolearr[i]['name'],
                        game:projectrolearr[i]['game']
                    }
                    projectrolearrlist.push(arr);
                }
            })
            var datarightsarr;
            X.api("data_auth/list","get",{},function(d){
                datarightsarr = d;
            })

            var adduserData =[]; //以创建账号
            $(".adduser-subject-add-box div").click(function(){
                var txt = $(".adduser-subject-add-box input").val();
                var titarr = txt.split(",");
                for(let i in titarr){
                    var arr = {
                        "username": titarr[i],
                        "role_name": projectrolearr[0]["name"],
                        "data_authority":datarightsarr[0]["title"],
                        "data_auth_id":datarightsarr[0]["id"]
                    }
                    adduserData.push(arr);
                }
                update();
                $(".adduser-subject-add-box input").val("");
            })

            $('body').on("click",".adduser-table-title-cz img",function(){
                var index = $(this).attr("data-index");
                adduserData.splice(index,1);


                update();
            })
            
            var cprojectindex = '';//获取点击的项目角色的index
            var cprojectdiv = '';//获取点击的项目角色的div
            $("body").on("click",".adduser-role span",function(){
                cprojectindex = $(this).attr("data-index");
                cprojectdiv = $(this);
            })

            var cdatarightsindex = "";//获取点击的数据权限index
            var cdatarightsdiv = "";//获取点击的数据权限div
            $("body").on("click",".adduser-attr span",function(){
                cdatarightsindex = $(this).attr("data-index");
                cdatarightsdiv = $(this);
            })

            function update(){
                X.laytpldata("#adduser-table-con-box-dot",adduserData,".adduser-table-con-box");
                // 项目角色修改
                // console.log(projectrolearrlist);

                // X.laydropdown(".adduser-role",projectrolearrlist,function(d){
                //     cprojectdiv.html(d.title);

                //     adduserData[cprojectindex]['role_name'] = d.title;
                // })
                // 数据权限修改
                // X.laydropdown(".adduser-attr",datarightsarr,function(d){
                //     cdatarightsdiv.html(d.title);
                //     console.log(d)
                //     adduserData[cdatarightsindex]['data_authority'] = d.title;
                //     adduserData[cdatarightsindex]['data_auth_id'] = d.id;
                // })
            }

            $("body").on("click",".adduser-but-box .adduser-btn",function(){
                layer.closeAll();
            })
            

            $("body").on("click",".adduser-but-box .adduser-btn2",function(){
                console.log(adduserData);
                var namearr = [];
                for(let i in adduserData){
                    namearr.push(adduserData[i]['username']);
                }
                if(namearr.length > 0){
                    X.api("user/add_account","post",{account_list:namearr},function(d){
                        X.pageLogic.useradmin.useradmin();
                        layer.closeAll();
                        layer.msg("创建成功");
                    })
                }else {
                    layer.msg("请先添加账号");
                }
                
            })
            
        }
    };
})();
