var app = {
	loading : function(is, position) {
		$('#loading').css('display', is ? 'block' : 'none');
		is && position && ($('#loading').addClass(position));
	},
	info : function(message, title, position) {
		toastr.info(message, title, {
			positionClass : 'toast-' + (position || 'top-center'),
			timeOut : 1000
		});
	},
	error : function(message, title, position) {
		toastr.error(message, title, {
			positionClass : 'toast-' + (position || 'top-center')
		});
	},
	getUrlParam: function(param) {   
	    var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");   
	    var r = window.location.search.substr(1).match(reg);
	    return r ? decodeURI(r[2]) : null;
	},
	showDelayTime: function(el, step) {
		el.attr('disabled', 'disabled');
		setTimeout(function(){
			el.text(step-- + ' S');
			if(step > 0) {
				app.showDelayTime(el, step);
			} else {
				el.attr('disabled', null);
				el.text('获取验证码');
			}
		}, 1000);
	},
	sendValidCode: function() {
		var btn = $(this), f_username = $('input[name=username]'), username = f_username.val(),
			now = new Date().getTime(), delay = 60;
		if (!username) {
			app.error('请填写手机号或邮箱地址');
			f_username.focus();
			return;
		}
		if (app.lastGetCodeTime && now - app.lastGetCodeTime < delay * 1000) {
			app.error('请稍后再获取');
			return;
		}
		app.lastGetCodeTime = now;
		app.loading(true);
		$.get('api/user/vcode', {
			source: 'resetPwd',
			username: username
		}, function(data){
			app.loading(false);
			// 这里无论是否error都应设置success
			app.validCodeSendSuccess = true;
			if (data.errMsg) {
				if('403' != data.errCode) {
					app.lastGetCodeTime = null;
				}
				app.error(data.errMsg);
			} else {
				app.showDelayTime(btn, delay);
			}
		});
	},
	checkValidCode: function(form, data) {
		app.loading(true);
		$.post('api/user/vcode', {
			source: 'resetPwd',
			username: data.username,
			validCode: data.validCode
		}, function(data){
			app.loading(false);
			if (data.errMsg) {
				app.error(data.errMsg);
			} else {
				// 验证成功后token
				app.pageToken = data.content;
				$('form[name=checkForm]').hide();
				$('form[name=resetForm]').show();
			}
		});
	},
	resetPwd: function(form, data) {
		app.loading(true);
		$.post('api/user/resetPwd', {
			pageToken: app.pageToken,
			password: data.password
		}, function(data){
			app.loading(false);
			if (data.errMsg) {
				app.error(data.errMsg);
			} else {
				// 弹窗通知已经修改成功
				$('#successDialog').show().find('.weui_btn_dialog').click(function () {
					// 重置成功，跳转
					var redirectUrl = 'sso/login?appId=' + (app.getUrlParam('appId') || 'sso'), retUrl = app.getUrlParam('returnURL');
					retUrl && (redirectUrl += '&returnURL=' + retUrl);
					window.location.href = redirectUrl;
				});
			}
		});
	},
	init : function() {
		app.loading(false);
		// 获取验证码
		$('.x-btn-code').click(app.sendValidCode);
		// 校验验证码
		$('form[name=checkForm]').validation({
			disable: false,
			success: app.checkValidCode,
			error: function(form, errEl, errMsg) {
				app.error(errMsg);
			},
			listeners: {
				'validCode': {
					'propertychange': function(el, val) {
						if(val && val.length > 3 && app.validCodeSendSuccess) {
							$('form[name=checkForm] .x-btn-submit').attr('disabled', null);
						} else {
							$('form[name=checkForm] .x-btn-submit').attr('disabled', 'disabled');
						}
					}
				}
			}
		});
		// 重置密码
		$('form[name=resetForm]').validation({
			disable: false,
			success: app.resetPwd,
			error: function(form, errEl, errMsg) {
				app.error(errMsg);
			},
			listeners: {
				'password': function(el, val){
					var oth = $('input[name=passwordConfirm]');
					if (oth.val() && val != oth.val()) {
						app.error('密码不一致');
					}
				},
				'passwordConfirm': {
					'change': function(el, val){
						var oth = $('input[name=password]');
						if (val != oth.val()) {
							el.addClass('x-error');
							app.error('密码不一致');
						}
					},
					'propertychange': function(el, val){
						var oth = $('input[name=password]');
						if (val && val == oth.val()) {
							$('form[name=resetForm] .x-btn-submit').attr('disabled', null);
						} else {
							$('form[name=resetForm] .x-btn-submit').attr('disabled', 'disabled');
						}
					}
				}
			}
		});
	}
};
$(document).ready(function() {
	app.init();
});