(function(){
    X.pageLogic['kongjianshezhi'] = {
        init : function(){
            var me = this;

            $(".kongjian-mc-box .kongjian-tianjia").click(function(){
                $(".kongjian-box .member-box").show();
            });

            $(".member-top-box .member-cha").click(function(){
                $(".kongjian-box .member-box").hide();
            });
            var userarr,adduadaserarr;

            var userinfo = X.DATA.userinfo;
            $(".gly-firs").html(userinfo.name.substring(0,1));
            $(".gly-name").html(userinfo.name);
            X.api('user/all_account',"get",{},function(d){
                for(let i in d){
                    d[i]["firs"] = d[i]['name'].substring(0,1);
                    d[i]["authority"] = "r";
                }
                userarr = d;
                adduadaserarr = [];
                X.DATA['adduadaserarr'] = [];
                X.DATA['userarr'] = d;
                me.updatauserarr();
            });
            
            //添加用户数据

            //筛选用户
            $(".member-top-box #membertxt").bind("input propertychange",function(){
                // console.log($(this).val());
                var txt = $(this).val();
                var arr=[];
                if(txt != ''){
                    for(let i in userarr){
                        if(userarr[i]['username'].indexOf(txt) != -1){
                            arr.push(userarr[i]);
                        }
                    }
                }else {
                    arr = X.DATA['userarr'];
                }

                X.laytpldata("#member-user-con-box-dot",arr,".member-user-con-box");

            });

            //给用户修改权限
            $(document).on("click",".kongjian-renyuan .kongjian-renyuan-jurisdiction",function(){
                var obj = $(this);
                var offset = obj.offset();
                X.querycriteriapop(obj.attr('data-type'),'jurisdiction',offset.left-160,offset.top+obj.height(),function(val){
                    console.log(val)
                    var index = obj.attr("data-index");
                    adduadaserarr[index]['authority'] = val;
                    if(val == "r"){
                        obj.html('仅查看');
                        obj.attr('data-type','r');
                    }else {
                        obj.html('可编辑');
                        obj.attr('data-type','rw');
                    }
                    X.DATA["adduadaserarr"] = adduadaserarr;
                })
            });
            //添加用户
            $(document).on("click",".member-user-con-right",function(){
                var userid = $(this).attr('data-id');
                for(let i in userarr){
                    if(userarr[i]["_id"] == userid){
                        // userarr[i]
                        adduadaserarr.push(userarr[i]);
                        userarr.splice(i,1);
                    }
                }
                X.DATA['adduadaserarr'] = adduadaserarr;
                X.DATA['userarr'] = userarr;
                me.updatauserarr();
            });

            //删除用户
            $(document).on("click",".kongjian-renyuan-right .kongjian-renyuan-del",function(){
            // $(".kongjian-renyuan-right .kongjian-renyuan-del").on('click',function(){
                var userid = $(this).attr('data-id');
                for(let i in adduadaserarr){
                    if(adduadaserarr[i]['_id'] == userid){
                        userarr.push(adduadaserarr[i]);
                        adduadaserarr.splice(i,1);
                    }
                }
                X.DATA['adduadaserarr'] = adduadaserarr;
                X.DATA['userarr'] = userarr;
                me.updatauserarr();
            });

            $(".kongjian-but .kongjian-qd").click(function(){
                var tit = $(".kongjian-mc-box .kongjian-mc-box-txt").val();
                var adduserarr = X.DATA['adduadaserarr'];
                if(tit == ''){
                    alert('请输入空间名称');
                    return;
                }
                var data = {
                    'name': tit,
                    'members': adduserarr,
                    'project_id':X.DATA.projectid
                }
                for(let i in adduserarr){
                    if(adduserarr[i]['_id'] == "0"){
                        data["is_all_member"] = true,
                        data["authority"] = adduserarr[i]['authority']
                    }else {
                        data["is_all_member"] = false,
                        data["authority"] = "r"
                    }
                }
               
                

                X.api('space/create',"post",data,function(d){
                    // if(d.code == "ok"){
                        layer.msg(d, function(){
                            //do something
                            X.pageLogic.dashboard.freshMenu(X.DATA.projectid);//刷新侧边栏
                            //关闭对应弹窗
                            layer.closeAll();
                          });
                    // }
                })

            })

            $(".kongjian-but .kongjian-qx").click(function(){
                layer.closeAll();
            })

        },
        updatauserarr : function(){
            var adduadaserarr =  X.DATA['adduadaserarr'];
            var userarr =  X.DATA['userarr'];
            console.log(adduadaserarr);
            //以添加用户
            X.laytpldata("#kongjian-renyuan-box-dot",adduadaserarr,".kongjian-renyuan-box2");
            //未添加的用户
            X.laytpldata("#member-user-con-box-dot",userarr,".member-user-con-box");
        }
    };
})();
