/*2017-01-16 add by yb
 * describe:微信前端，我的
 */
$(function(){
	var contextPath=$("#contextPath").val();
	var ERROR_OK = 200;
	var mine = {
		init: function(){
			this.bindEvent();
			this.render();
		},
		render: function(){
			var openId = this.GetQueryString("openId");
			var organizationId = this.GetQueryString("organizationId");
			var developId = this.GetQueryString("developId");
			var userId = this.GetQueryString("userId");
			this.ajaxInfo(openId,organizationId,developId,userId)
		},
		bindEvent: function(){
			var _this = this;
			
			var developId = this.GetQueryString("developId");
			var userId = this.GetQueryString("userId");
			var openId = this.GetQueryString("openId");
			var organizationId = this.GetQueryString("organizationId");
			var compName = this.GetQueryString("compName");
			var accountId = this.GetQueryString("accountId");
			var virAccount = this.GetQueryString("virAccount");
			
			
		},
		/**
		 * 获取url的参数
		 * @param name 参数名
		 */
		GetQueryString: function (name) {
		    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		    var r = window.location.search.substr(1).match(reg);
		    if (r != null) return unescape(r[2]); return null;
		},
		/**
		 * 获取用户信息接口
		 * @param openid
		 * @param organizationId 楼栋id
		 * @param callback
		 */
		ajaxInfo: function(openId,organizationId,developId,userId){
            var userStatus = this.GetQueryString("userStatus");
            var register = this.GetQueryString("register");
			var _this = this;
			$.ajax({
		  		url :contextPath+"/wechat/appUser/getUserInfo",
		  		type : "POST",
		  		dataType:"json",
		  		data: {
		  			openId : openId ,
		  			organizationId: organizationId
		  		},
		  		success : function(data) {
		  			if(data.code==ERROR_OK){
		  				var result = data.data;
		  				$(".account").html(result.account);
		  				$(".name").html(result.compName);
		  			   //退出登录
		  				$(".quit-btn").on("click",function(){
		  					_this.ajaxQuit(openId,result.accountId,function(){
		  						window.location.href=$("#contextPath").val()+"/wechat/appUser/newmine?openId="+openId+"&developId="+developId+"&userStatus="+userStatus+"&userId="+userId+"&register="+register+"&organizationId="+organizationId;
		  					});
		  				})
		  			}else{
		  				alert(data.msg)
		  			}
		  		},
		  		error : function(data) {
		  			// alert("系统繁忙,菜单加载失败..");
		  		}
		  	});
		},
		/**
		 * 退出登录
		 * @param openid
		 * @param accountId 企业id
		 *  @param callback
		 */
		ajaxQuit: function(openId,accountId,callback){
			var _this = this;
			$.ajax({
		  		url :contextPath+"/wechat/appUser/exitLogin",
		  		type : "POST",
		  		dataType:"json",
		  		data: {
		  			openId : openId ,
		  			accountId: accountId,
		  			orgId: _this.GetQueryString("organizationId")
		  		},
		  		success : function(data) {
		  			if(data.code==ERROR_OK){
		  				callback();
		  			}else{
		  				layer.open({
						    content: data.msg
						    ,skin: 'msg'
						    ,time: 2 //2秒后自动关闭
						  });
//		  				alert(data.msg)
		  			}
		  		},
		  		error : function(data) {
		  			// alert("系统繁忙,菜单加载失败..");
		  		}
		  	});
		}
	};
	mine.init();
})

//$(function(){
//	var wait=60;
//	var mine = {
//		init: function(){
//			this.render();
//			this.bindEvent();
//		},
//		/**
//		 * 初始化获取用户信息
//		 * 
//		 */
//		render: function(){
//			var openId = this.GetQueryString("openId");
//			var contextPath=$("#contextPath").val();
//			$.ajax({
//    	    	url: contextPath+"/wechat/appUser/getWechatUserInfo",
//    	    	data: {openId: openId},
//    	        type: "POST",
//    	        dataType: "json",
//    	        success: function(data){
//    	        	if(data.code == 200){
//    	        		$(".header-img").attr("src",data.data.headImgUrl);
//    	        		$(".user-name").html(data.data.nickName);
//    	        		$(".phone-num").html(data.data.userMobile)
//    	        	}
//    	        },
//    	        error: function(){
//    	        	
//    	        }
//    	    });	
//		},
//		bindEvent: function(){
//			var _this = this;
//			var contextPath=$("#contextPath").val();
//			var developId = this.GetQueryString("developId");
//			var userId = this.GetQueryString("userId");
//			var openId = this.GetQueryString("openId");
//			var organizationId = this.GetQueryString("organizationId");
//			var mobile = $(".phone-num").html();
//			//更换号码
//			$(".change-mobile-btn").on("click",function(){
//				mobile = $(".phone-num").html();
//				window.location.href=contextPath+'/wechat/release/changePhone?mobile='+mobile+'&openId='+openId+'&developId='+developId+'&userId='+userId+'&organizationId='+organizationId;
//			})
//		},
//		/**
//		 * 获取url的参数
//		 * @param name 参数名
//		 */
//		GetQueryString: function (name) {
//		    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//		    var r = window.location.search.substr(1).match(reg);
//		    if (r != null) return unescape(r[2]); return null;
//		}
//	};
//	mine.init();
//})
