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

            $(".adduser-top-box img").click(function(){
                layer.closeAll();
            })
            
            var projectrolearr;
            X.api("authority/all_role","get",{},function(d){
                projectrolearr = d.dom_role;
            })
            var datarightsarr;
            X.api("data_auth/list","get",{},function(d){
                datarightsarr = d;
                console.log(datarightsarr)
            })

            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]["title"],
                        "data_authority":datarightsarr[0]["title"],
                        "data_auth_id":datarightsarr[0]["id"]
                    }
                    adduserData.push(arr);
                }
                console.log(adduserData);
                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(projectrolearr);
                X.laydropdown(".adduser-role",projectrolearr,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);
                if(adduserData.length > 0){
                    X.api("authority/add_account","post",{accounts:adduserData,project_id: X.DATA.projectid},function(d){
                        layer.msg("创建成功");
                        X.pageLogic.useradmin.useradmin();
                        layer.closeAll();
                    })
                }else {
                    layer.msg("请先添加账号");
                }
                
            })
            
        }
    };
})();
