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

            $(".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;
            var adduadaserarr;

            var userinfo = X.DATA.userinfo;
            $(".gly-firs").html(userinfo.name.substring(0,1));
            $(".gly-name").html(userinfo.name);

            if(id){
                $(".editkongjian").show();
                $(".kongjian-cz").show();
                X.api('space/detail','post',{space_id:id},function(d){
                    $('.kongjian-mc-box-txt').val(d.name);
                    for(let i in d.members){
                        d.members[i]["firs"] = d.members[i]['name'].substring(0,1);
                    }
                    X.DATA['adduadaserarr'] = d.members;
                    adduadaserarr = d.members;
                    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";
                        }
                        for(let i in adduadaserarr){
                            for(let j in d){
                                if(adduadaserarr[i]['_id'] == d[j]['_id']){
                                    d.splice(j,1);
                                }
                            }
                        }
                        userarr = d;
                        X.DATA['userarr'] = d;
                        me.updatauserarr();
                    });
                })

                var listarr = [
                    {title:'移交空间',id:'transfer'},
                    {title:'删除空间',id:'del'}
                ]
                X.laydropdown("#kongjian-cz",listarr,function(d){
                    console.log(d);
                    if(d.id == 'del'){
                        layer.confirm('确认删除此空间吗？该操作不可恢复', {
                            btn: ['取消','删除 '] //按钮
                          }, function(){
                            //取消
                            layer.close(layer.index);
                          }, function(){
                            //删除
                            X.api("space/delete",'post',{_id:id},function(d){
                                layer.msg('删除成功');
                                layer.close(layer.index);
                                X.pageLogic.dashboard.freshMenu(X.DATA.projectid);//刷新侧边栏
                                me.updata(X.DATA.projectid);//刷新数据
                            })
                        });
                    }
                })
            }else {
                $(".addkongjian").show();
                $(".kongjian-cz").hide();
                adduadaserarr = [];
                X.DATA['adduadaserarr'] = [];
                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;
                    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.parametersopen(obj.attr('data-type'),'jurisdiction',[offset.top+obj.height(),offset.left-160],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);
                    }
                }
                console.log(adduadaserarr);
                X.DATA['adduadaserarr'] = adduadaserarr;
                X.DATA['userarr'] = userarr;
                me.updatauserarr();
            });

            //删除用户
            $(document).off('click','.kongjian-renyuan-right .kongjian-renyuan-del').on("click",".kongjian-renyuan-right .kongjian-renyuan-del",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);
                    }
                }
                // console.log(adduadaserarr);
                // console.log(userarr);
                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"
                    }
                }
               
                if(id){
                    console.log(adduserarr);
                    X.api('space/rename','post',{space_id:id,new_name:tit},function(d){
                        X.api('space/set_members','post',{space_id:id,members:adduserarr},function(d){
                            layer.closeAll();
                            layer.msg('修改成功');
                            X.pageLogic.dashboard.freshMenu(X.DATA.projectid);
                        })
                    })
                    
                }else{
                    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'];
            //以添加用户
            X.laytpldata("#kongjian-renyuan-box-dot",adduadaserarr,".kongjian-renyuan-box2");
            //未添加的用户
            X.laytpldata("#member-user-con-box-dot",userarr,".member-user-con-box");
        }
    };
})();
