angular.module('app', []).controller('AppCtrl', function($rootScope, $scope, $http, $timeout) {
	$scope.login = {
		cert_id : '',
		exam_id : '',
		valid_code : '',
		password : ''
	};
	$scope.curStep = 'login';
	$rootScope.isMobile = function(v) {
		// var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
		// 简单点
		var regex = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
		if (!regex.test(v)) {
			return false;
		}
		return true;
	}
	$rootScope.msg = function(msg, title, callback) {
		var msgTitle = '提示消息';
		if (title !== undefined) {
			var type = typeof (title);
			if (type == 'string' && title.length > 0)
				msgTitle = title;
			else if (type == 'function') {
				callback = title;
			}
		}

		if (callback == undefined)
			callback = function() {

			}
		var d = dialog({
			title : msgTitle,
			width : '20em',
			content : msg,
			cancel : false,
			ok : callback,
			cancelDisplay : false,
			okValue : '确 定'
		});
		d.showModal();
	}
	$scope.stdLogin = function() {
		var reqUrl = "";
		if (!$scope.login.cert_id || $.trim($scope.login.cert_id).length != 18) {
			$('#cert_id').focus();
			return false;
		}
		if (IsEnableReg == "true") {
			$scope.login.exam='null';
			reqUrl = "../../enrol/std/loginByPasswd.htm";
			if (!$scope.login.password || $.trim($scope.login.password).length == 0) {
				$('#password').focus();
				return false;
			}
		} else {
			$scope.login.password='null';
			reqUrl = "../../enrol/std/loginByExamId.htm";
			if (!$scope.login.exam_id || $.trim($scope.login.exam_id).length != 14) {
				$('#exam_id').focus();
				$scope.msg("高考报名号必须是14位!");
				return false;
			}
		}
		// 判断验证码
		if (!$scope.login.valid_code || $.trim($scope.login.valid_code).length != 4) {
			$('#valid_code').focus();
			return false;
		}

		$.LoadingOverlay("show");
		$.ajax({
			url : reqUrl,
			type : "POST",
			dataType : "json",
			data : $scope.login,
			success : function(data) {
				if (data.success == false) {
					if (data.errorCode == 1100) {//高考报名号登陆时，没找到去考试院取
						if (IsEnableReg == "true") {//考生自行注册，用密码登录
							$scope.msg("账号或密码不正确!");
							return;
						}
						// 遮罩层 提示 正在从考试院获取数据
						$.LoadingOverlay("show");
						$.ajax({
							type : 'POST',
							url : '../../enrol/std/fetch.htm',
							data : $scope.login,
							dataType : 'json',
							success : function(data) {
								if (data.success == false) {
									$scope.msg(data.errorMsg);
								} else {
									$scope.stdLogin();
								}
							},
							complete : function(data) {
								$.LoadingOverlay("hide");
							}
						})
					} else if (data.errorCode == 1003) {
						$scope.msg('验证码错误，请检查！');
					} else
						$scope.msg(data.errorMsg);
					return;
				} else {
					// 检查是否confirm为空 tioaz确定信息页面
					if (!data.entity.confirm_time) {
						$timeout(function() {
							$scope.std = data.entity;
							$scope.curStep = 'confirm';
						}, 100);
						return;
					}
					// 登录成功跳转系统首页
					window.location = '../index.jsp';
				}
			},
			error : function() {
				$rootScope.msg('服务器出现内部错误，请稍候重试！', '错误提示');
				return;
			},
			complete : function(data) {
				$.LoadingOverlay("hide");
			}
		});

	}

	$scope.changeValidCode = function() {
		$('#imgValidCode').attr('src', '../../enrol/code/generate.htm?verify=' + Math.random() + '&img_width=100&font_size=30&margin_left=10&font_interval=20&img_height=36&margin_top=25');
	}

	$timeout(function() {
		$scope.changeValidCode();
	}, 100);

	$scope.goLogin = function() {
		$.ajax({
			url : '../../enrol/std/logout.htm'
		});
		$scope.std = undefined;
		$timeout($scope.changeValidCode, 100);
		$scope.curStep = 'login';
	}

	$scope.goSubmit = function() {
		if (!$rootScope.isMobile($scope.std.std_mobile1)) {
			$rootScope.msg('请输入正确的联系电话(11位手机号)！');
			return;
		}
		if (!$rootScope.isMobile($scope.std.std_mobile2)) {
			$rootScope.msg('请输入正确的备用电话(11位手机号)！');
			return;
		}
		if ($scope.std.std_mobile2 == $scope.std.std_mobile1) {
			$rootScope.msg('备用电话和联系电话必须不一样！');
			return;
		}
		if (Enrol_NeedAddress == 'true' && ($scope.std.link_addr == undefined || $.trim($scope.std.link_addr).length == 0)) {
			$rootScope.msg('为确保通知书能准确送达，请输入正确的通讯地址！');
			return;
		}

		$scope.std.confirm = true;
		$.ajax({
			url : '../../enrol/std/info/save.htm',
			type : "POST",
			dataType : "json",
			data : $scope.std,
			success : function(data) {
				if (data.success == false) {
					$scope.msg(data.errorMsg);
					return;
				} else {
					window.location = '../index.jsp';
				}
			},
			error : function() {
				$rootScope.msg('服务器出现内部错误，请稍候重试！', '错误提示');
				return;
			},
			complete : function(data) {
				$.LoadingOverlay("hide");
			}
		});

	}

    $scope.goToReg = function() {
    	//window.location = "../reg/reg.jsp";  //考生注册页面
    	window.location = "../reg/std_reg.jsp";   //考生注册信息提醒页面
    }
	
	$scope.getSchoolId = function() {
		var href = window.location.href.replace('http://', '').split('.')[0];
		// href = 7157;
		if (href == "127")
			href = 1000;
		$scope.login.school_id = href;
		$.ajax({
			url : '../../enrol/std/school.htm',
			type : "POST",
			dataType : "json",
			data : {
				school_id : href
			},
			success : function(data) {
				$timeout(function() {
					$scope.school = data.entity;
				}, 100);
			}
		});
	}
	$scope.getSchoolId();
	

})