var deleteObj;
var deleteNode;
$(function(){
	$('#deleteTips').hide();
	$('#div_add_member').hide();
	
	$('.backBtn').click(function(){
		$('#div_add_member').hide();
	});
	$('.backBtn2').click(function(){
		waiting();
		window.history.go(-1);
	});
	
	$('#addMemberBtn').click(function(){
		$('#deleteTips').hide();
		$('#div_add_member').show();
	});
	$('.cancle-btn').click(function(){
		deleteObj = null;
		deleteNode = null;
		$('#deleteTips').hide();
	});
	
	deleteF();
    memberInfo();
	allowAdd();

	//删除成员
	$('.sure-btn').click(function(){
		waiting();
		doPost(ctx+"/info/memberdel?appId="+appId,{openid:openid,userId:userId,cellSel:cellSel,memberId:deleteObj.memberId,member:deleteObj.member,type:deleteObj.type},false,function(result){
			if(typeof result == "string"){
				result = eval('('+result+')');
			}
			deleteObj = null;
			$('#deleteTips').hide();
			if(result.error == 0){//成功
				deleteNode.remove();
				$('#ok_msg').text(result.errormsg);
				showTips('success_tips',true);
				allowAdd();
			}else{//其他提示 如校验错误
				$('#tips_msg').text(result.errormsg);
				showTips('orther_tips',false);
			}
			deleteNode=null;
			waitingStop();
		});
	});
	
	//添加成员
	$('#postMemberBtn').click(function(){
		var data = getFormData('member_form');
		if(data.member==null || data.member==''){
			$('#tips_msg').text('成员手机号码不能为空');
			showTips('orther_tips',false);
			return;
		}
		var phoneRe = /(^\d{3,4}-?\d{5,8}$)|(^(\d{2})?1[3|4|5|6|7|8|9]\d{9}$)/;
		if(!phoneRe.test(data.member)){
			$('#tips_msg').text('成员手机号码不合法，请重新输入');
			showTips('orther_tips',false);
			return;
		}
		if(data.name==null || data.name==''){
			$('#tips_msg').text('成员名称不能为空');
			showTips('orther_tips',false);
			return;
		}
		var nameRe = /^([\x09\x0A\x0D\x20-\x7E]|[\u4e00-\u9fa5]|[\uff0c\u3002\u3001\uff1b\u2019\u2018\u3010\u3011\u3001\uff1a\u201c\u201d\u300a\u300b\uff1f\uff01\u00b7\uffe5\u2026\uff08\uff09\u2014])+$/;
		if(!nameRe.test(data.name)){
			$('#tips_msg').text('成员名称含有非法字符，请重新输入');
			showTips('orther_tips',false);
			return;
		}
        if(data.type==null || data.type==''){
            $('#tips_msg').text('用户类型不能为空');
            showTips('orther_tips',false);
            return;
        }
        if(data.type!=2 && data.type!=3){
            $('#tips_msg').text('用户类型不正确');
            showTips('orther_tips',false);
            return;
        }
		waiting();
		doPost(ctx+"/info/memberadd?appId="+appId,data,false,function(result){
			if(typeof result == "string"){
				result = eval('('+result+')');
			}
			$('#div_add_member').hide();
			if(result.error == 0){//成功
				var memberNode = "<div data=\"{'memberId':'"+result.member_id+"','member':'"+data.member+"','name':'"+data.name+"','type':'"+data.type+"'}\" class=\"member-list-item\">"+
					"<div>"+data.name+"</div>"+
					"<div class=\"delete-btn\"><span class=\"glyphicon glyphicon-remove-circle\"></span></div>"+
                    "<div class=\"member-info\"><span class=\"glyphicon glyphicon glyphicon-info-sign\"></span></div>"+
				"</div>";
				$('.member-list-'+data.type).append(memberNode);
				$('#member_form input.form-control.myform-control').val('');
				$('#ok_msg').text(result.errormsg+" 新用户默认登录密码和开锁密码为手机号码后6位");
				showTips('success_tips',true);
				deleteF();
                memberInfo();
				allowAdd();
			}else{//其他提示 如校验错误
				$('#tips_msg').text(result.errormsg);
				showTips('orther_tips',false);
			}
			waitingStop();
		});
	});
	
	$('#ok_tips_btn').click(function(){
		$(this).parent().hide();
		$('#div_tips').hide();
	});
	$('#orther_tips_btn').click(function(){
		$(this).parent().hide();
		$('#div_tips').hide();
	});
});

function memberInfo() {
    $('.member-info').click(function(){
        var memberObj = eval('('+$(this).parent().attr('data')+')');
        window.location.href=ctx+'/info/memberinfo?appId='+appId+'&memberId='+memberObj.memberId+'&selCell='+cellSel;
    });
}

function deleteF(){
	$('.delete-btn').click(function(){
		deleteNode = $(this).parent();
		deleteObj = eval('('+deleteNode.attr('data')+')');
		$('#deleteTips').show();
	});
}
function allowAdd(){
	var sonNode = $('.member-list').children();
	
	if(sonNode.length>=10){
		$('#addMemberBtn').hide();
	}else{
		$('#addMemberBtn').show();
	}
}
