(function(){
    X.pageLogic['addexistuser'] = {
        init : function(){
            
            var data = {
                "authDataPowers":[
                    
                ],
                "authRoles":[
                 
                ],
                "userlist":[
                   
                ]
            }

            X.api("user/all_account","get",{},function(d){
                data['userlist'] = d
                X.laytpldata("#addexistuser-con-user-box-dot",d,".addexistuser-con-user-box");
            });

            X.api("project/members","get",{},function(d){
                console.log(d)
                // X.laytpldata("#addexistuser-con-user-box-dot",d,".addexistuser-con-user-box");
            });

            X.api("authz/roles","get",{},function(d){
                var projectrolearrlist = [];
                for(let i in d){
                    var arr={
                        id:d[i]['_id'],
                        title:d[i]['name'],
                        game:d[i]['game']
                    }
                    projectrolearrlist.push(arr);
                };
                data['authRoles'] = projectrolearrlist;
            })

            $('.addexistuser-con-ss-box input').bind('input propertychange', function() {
                var txt = $(this).val();
                var userdata = data["userlist"];
                var arr = [];
                for(let i in userdata){
                    if(userdata[i]["nickname"].indexOf(txt) >= 0 ){
                        arr.push(userdata[i]);
                    }else {
                        if(userdata[i]["name"].indexOf(txt) >= 0 ){
                            arr.push(userdata[i]);
                        }
                    }
                }
                if(txt != ""){
                    X.laytpldata("#addexistuser-con-user-box-dot",arr,".addexistuser-con-user-box");
                    $(".addexistuser-con-ss-cha").show();
                }else {
                    X.laytpldata("#addexistuser-con-user-box-dot",data.userlist,".addexistuser-con-user-box");
                    $(".addexistuser-con-ss-cha").hide();
                }
            });

            $(".addexistuser-con-ss-cha").click(function(){
                $(".addexistuser-con-ss-box input").val("");
                $(".addexistuser-con-ss-cha").hide();
                X.laytpldata("#addexistuser-con-user-box-dot",data.userlist,".addexistuser-con-user-box");
            });

            var Addeddate = []; //以选中人员
            $("body").on("click",".addexistuser-con-user-list-box img",function(){
                var index = $(this).attr("data-index");
                var arr = data.userlist[index];//获取选中用户数据
                Addeddate.push(arr);
                var txt = $(".addexistuser-con-ss-box input").val();
                if(txt != ""){
                    var userdata = data["userlist"];
                    var arr = [];
                    for(let i in userdata){
                        if(userdata[i]["nickname"].indexOf(txt) >= 0 ){
                            arr.push(userdata[i]);
                        }else {
                            if(userdata[i]["name"].indexOf(txt) >= 0 ){
                                arr.push(userdata[i]);
                            }
                        }
                    }
                    arr.splice(index,1);
                    X.laytpldata("#addexistuser-con-user-box-dot",arr,".addexistuser-con-user-box");
                }else {
                    data.userlist.splice(index,1);
                    X.laytpldata("#addexistuser-con-user-box-dot",data.userlist,".addexistuser-con-user-box");
                }
                
                for(let i in Addeddate){
                    if(Addeddate[i] == undefined){
                        Addeddate.splice(i,1);
                    }
                }
                console.log(Addeddate)
                X.laytpldata("#addexistuser-table-con-list-dot",Addeddate,".addexistuser-table-con-list");

                addlaydropdown();

            });

            $("body").on("click",".addexistuser-table-tit-cz img",function(){
                var index = $(this).attr("data-index");
                data.userlist.push(Addeddate[index]);
                Addeddate.splice(index,1);
                var txt = $(".addexistuser-con-ss-box input").val();
                if(txt != ""){
                    usertxtdate();
                }else {
                    X.laytpldata("#addexistuser-con-user-box-dot",data.userlist,".addexistuser-con-user-box");
                }
                X.laytpldata("#addexistuser-table-con-list-dot",Addeddate,".addexistuser-table-con-list");
                addlaydropdown();
            });

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

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

            function usertxtdate(){
                var userdata = data["userlist"];
                var arr = [];
                for(let i in userdata){
                    if(userdata[i]["nickname"].indexOf(txt) >= 0 ){
                        arr.push(userdata[i]);
                    }else {
                        if(userdata[i]["name"].indexOf(txt) >= 0 ){
                            arr.push(userdata[i]);
                        }
                    }
                }
                arr.splice(index,1);
                X.laytpldata("#addexistuser-con-user-box-dot",arr,".addexistuser-con-user-box");
            };

            //给项目角色，数据权限添加下拉框
            function addlaydropdown(){
                X.laydropdown(".addexistuser-role",data["authRoles"],function(d){
                    cprojectdiv.html(d.title);
                    Addeddate[cprojectindex]['role_id'] = d.id;
                    Addeddate[cprojectindex]['role_tit'] = d.title;
                })

                X.laydropdown(".addexistuser-attr",data["authDataPowers"],function(d){
                    cdatarightsdiv.html(d.title);
                    Addeddate[cdatarightsindex]['datarightsid'] = d.id;
                    Addeddate[cdatarightsindex]['datarightstit'] = d.title;
                })
            };

            //叉图标
            $(".addexistuser-top-box img").click(function(){
                layer.closeAll();
            });
            // 取消按钮
            $(".addexistuser-btn-qx").click(function(){
                layer.closeAll();
            });
            //确定按钮
            $(".addexistuser-btn-qd").click(function(){
                console.log(Addeddate);
                var ylist = [];
                for(let i in Addeddate){
                    var arr = {
                        username:Addeddate[i]['name'],
                        role_id:Addeddate[i]['role_id'],
                        game:X.DATA.game
                    }
                    ylist.push(arr)
                }
                console.log(ylist);
                if(ylist.length == 0){
                    layer.msg('暂未选择用户');
                    return;
                }
                X.api("authz/add_role_domain","post",{data:ylist},function(d){
                    console.log(d)
                    layer.closeAll();
                });
            });

        }
    };
})();
