mui.plusReady(function() {
	var token = localStorage.getItem('token');
	var isBang = 0;
	var isOver = 1;
	var wait = 120;
	var wait1 = 120;

	function time() {

		if (wait == 0) {
			$('#mobileCode').removeAttr("disabled");
			$('#mobileCode').val("重新发送");
			wait = 120;
		} else {
			$('#mobileCode').attr("disabled", true);
			$('#mobileCode').val("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
					time();
				},
				1000)
		}

	}

	function time1() {

		if (wait1 == 0) {
			$('#mobileCode1').removeAttr("disabled");
			$('#mobileCode1').val("重新发送");
			wait1 = 120;
		} else {
			$('#mobileCode1').attr("disabled", true);
			$('#mobileCode1').val("重新发送(" + wait1 + ")");
			wait1--;
			setTimeout(function() {
					time1();
				},
				1000)
		}

	}
	mui.ajax(qlgUrl('app/users/editPhone'), {
		headers: {
			"HYH-Token": token
		},
		data: {},
		dataType: 'json', //服务器返回json格式数据  
		type: 'post', //HTTP请求类型  
		timeout: 10000, //超时时间设置为10秒;  
		success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  
			var data = toJson(data);
			if (data.status == 1) {
				data = data.data;

				if (data.userPhone) {
					isBang = 1;
					$('#userPhone').val(data.userPhone);
					$('#userPhone').attr('disabled', 'disabled');
					$('#next_btn').css('display', 'block');
				} else {
					$('#true').css('display', 'block');
				}
			} else {
				mui.alert(data.msg);
			}

		},
		error: function(xhr, type, errorThrown) { //异常处理;  
			//					mui.alert(type);      
		}
	});
	mui('.row').on('tap', '.codeBtn', function() {
		var that = $(this)
		var userPhone = $('#userPhone').val();
		var url = $(this).attr('id') == 'mobileCode' ? 'sendCodeEdit' : 'sendCodeTie'
		if (isBang == 0 || isOver == 0) {
			if (userPhone == '') {
				mui.alert('手机号不能为空!');
				return;

			}
			if (!(
					/^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/
					.test(userPhone))) {
				mui.alert("手机号码有误,请重填!");
				return;
			}
		} else {
			userPhone = '';
		}

		$(this).attr("disabled", true);
		mui.ajax(qlgUrl('app/users/' + url), {

			data: {
				userPhone: userPhone
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒
			success: function(data) {
				var data = toJson(data);
				//服务器返回响应,根据响应结果,分析是否登录成功;
				if (data.status == 1) {

					if (that.attr('id') == 'mobileCode') {
						time();
					} else if (that.attr('id') == 'mobileCode1') {
						time1();
					}

				} else {
					mui.alert(data.msg)
				}
				that.removeAttr('disabled');
			},

			error: function(xhr, type, errorThrown) {
				//异常处理;
				//							mui.alert(type)
				//						mui.alert(type);      
			}
		});
	})
	//未绑定手机
	mui('.down').on('tap', '#true', function() {
		var phoneCode = $('#phoneCode').val();
		var that = $(this);
		if (phoneCode == '') {
			mui.alert('验证码不能为空!');
			return;
		} else if (phoneCode.length < 4) {
			mui.alert('验证码格式不正确!');
			return;
		}

		that.attr('disabled', 'disabled');
		mui.ajax(qlgUrl('app/users/phoneEdit'), {
			headers: {
				"HYH-Token": token
			},
			data: {
				phoneCode: phoneCode
			},
			dataType: 'json', //服务器返回json格式数据  
			type: 'post', //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒;  
			success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  
				//					//console.log(data.data.goodsFavoritesNum)
				//					//console.log(data.data.Rows)
				var data = toJson(data);
				if (data.status == 1) {
					mui.alert(data.msg);
					mui.back();

				} else {
					mui.alert(data.msg);
				}
				that.removeAttr('disabled')

			},
			error: function(xhr, type, errorThrown) { //异常处理;  
				//					mui.alert(type);      
			}
		});

	})
	//已绑定手机 1
	mui('.down').on('tap', '#next_btn', function() {
		var phoneCode = $('#phoneCode').val();
		var that = $(this);
		if (phoneCode == '') {
			mui.alert('验证码不能为空!');
			return;
		} else if (phoneCode.length < 4) {
			mui.alert('验证码格式不正确!');
			return;
		}

		that.attr('disabled', 'disabled');
		mui.ajax(qlgUrl('app/users/phoneEdito'), {
			headers: {
				"HYH-Token": token
			},
			data: {
				phoneCode: phoneCode
			},
			dataType: 'json', //服务器返回json格式数据  
			type: 'post', //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒;  
			success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  
				//					//console.log(data.data.goodsFavoritesNum)
				//					//console.log(data.data.Rows)
				var data = toJson(data);
				if (data.status == 1) {
					$('#next_btn').css('display', 'none');
					$('#next_true').css('display', 'block');
					$('#userPhone').removeAttr('disabled');
					$('#userPhone').val('');
					$('#phoneCode').val('');
					$('#mobileCode').css('display', 'none');
					$('#mobileCode1').css('display', 'block');
					isOver = 0;
				} else {
					mui.alert(data.msg);
				}
				that.removeAttr('disabled');
			},
			error: function(xhr, type, errorThrown) { //异常处理;  
				//					mui.alert(type);      
			}
		});

	})
	//已绑定手机 2
	mui('.down').on('tap', '#next_true', function() {
		var phoneCode = $('#phoneCode').val();
		var that = $(this);
		if (phoneCode == '') {
			mui.alert('验证码不能为空!');
			return;
		} else if (phoneCode.length < 4) {
			mui.alert('验证码格式不正确!');
			return;
		}

		that.attr('disabled', 'disabled');
		mui.ajax(qlgUrl('app/users/phoneEdit'), {
			headers: {
				"HYH-Token": token
			},
			data: {
				phoneCode: phoneCode
			},
			dataType: 'json', //服务器返回json格式数据  
			type: 'post', //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒;  
			success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  
				//					//console.log(data.data.goodsFavoritesNum)
				//					//console.log(data.data.Rows)
				var data = toJson(data);
				if (data.status == 1) {
					mui.alert(data.msg);
					mui.back();
				} else {
					mui.alert(data.msg);
				}
				that.removeAttr('disabled');
			},
			error: function(xhr, type, errorThrown) { //异常处理;  
				//					mui.alert(type);      
			}
		});

	})

})