/*2017-01-16 add by yb
 * describe:微信前端，更换号码
 */
$(function(){
	var wait=60;
	var mine = {
		init: function(){
			this.render();
			this.bindEvent();
		},
		render: function(){
			var mobile = this.GetQueryString("mobile");
			$(".binded-phone").html(mobile)
		},
		bindEvent: function(){
			var _this = this;
			//手机输入框失去焦点
			$(".phone-text").on("blur",function(){
				var reg  = /^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
				var phone = $(this).val();
				if(!reg.test(phone)){
					$(this).closest(".phone-wp").addClass("error")
				}else{
					$(this).closest(".phone-wp").removeClass("error")
				}
			});
			//手机验证码输入框失去焦点
			$(".code-text").on("blur",function(){
				var code = $(this).val();
				if(!code){
					$(this).closest(".code-text-wp").addClass("error")
				}else{
					$(this).closest(".code-text-wp").removeClass("error")
				}
			});
			//获取手机验证码
		    $(".code-btn").on("click",function(event){
		    	var telVal  = $(".phone-text").val();
		    	var $this = $(this)
		    	if(!telVal) {
		    		$(".phone-text").closest(".phone-wp").addClass("error");
		    		return false;
		        }else{
		    		_this.time($this);
		    		var contextPath=$("#contextPath").val();
		    		$.ajax({
		    	    	url: contextPath+"/wechat/sms/getAuthCode",
		    	    	data: {
		    	    			userMobile:telVal,
		    	    			openId： _this.GetQueryString("openId"),
		    	    			orgId:  _this.GetQueryString("organizationId")
		    	    		},
		    	        type: "POST",
		    	        dataType: "json",
		    	        success: function(json){
		    	        	
		    	        },
		    	        error: function(){
		    	        }
		    	    });	
		    	}
		    });
		    //更换号码
		    $(".bind-phone-btn").on("click",function(){
		    	_this.ajaxChangePhone()
		    })
		},
		/**
		 * 倒计时
		 * @param $dom
		 * 
		 */
		time: function($dom){
			
			var _this =this;
			if (wait == 0) {
				$dom.attr("disabled",false);
				$dom.html("获取短信验证码")
				wait = 60;
			} else {
				$dom.attr("disabled", true);
				$dom.html("重新发送(" + wait + ")");
				wait--;
				setTimeout(function() {
					_this.time($dom)
				},1000)
			}
		},
		//更换号码
		ajaxChangePhone: function(){
			var contextPath=$("#contextPath").val();
			var reg  = /^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
			var mobile = $(".phone-text").val();
			if(!reg.test(mobile)){
				$(".phone-text").closest(".phone-wp").addClass("error");
				return false;
			}
			var authCode = $(".code-text").val();
			if(!authCode){
				$(".code-text").closest(".code-text-wp").addClass("error");
				return false
			};
			var developId = this.GetQueryString("developId");
			var userId = this.GetQueryString("userId");
			var openId = this.GetQueryString("openId");
			var organizationId = this.GetQueryString("organizationId");
			$.ajax({
    	    	url: contextPath+"/wechat/appUser/updateTelByHisTel",
    	    	data: {
    	    		openId: openId,
    	    		developId: developId,
    	    		mobile: mobile,
    	    		authCode: authCode
    	    	},
    	        type: "POST",
    	        dataType: "json",
    	        success: function(data){
    	        	if(data.code==200){
    	        		$(".binded-phone").html(mobile)
    	        		window.location.href=$("#contextPath").val()+"/wechat/appUser/newmine?openId="+openId+"&organizationId="+organizationId+"&developId="+developId+"&userId="+userId;
//    	        		window.location.href=$("#contextPath").val()+"/wechat/release/mine?openId="+openId+"&developId="+developId+"&userId="+userId+"&organizationId="+organizationId;
    	        	}else{
    	        		alert(data.msg)
    	        	}
    	        },
    	        error: function(){
    	        	alert('error')
    	        }
    	    });	
		},
		/**
		 * 获取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();
})
