angular.module('app', []).controller('AppCtrl', function($rootScope, $scope, $http, $timeout) {
	$scope.login = {
		school_name : '',
		school_pwd : '',
		valid_code : ''
	};
	
	$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.login = function() {
		if (!$scope.login.school_name || $.trim($scope.login.school_name).length < 4) {
			$('#school_name').focus();
			return false;
		}
		if (!$scope.login.school_pwd || $.trim($scope.login.school_pwd).length < 6) {
			$('#school_pwd').focus();
			return false;
		}
		if (!$scope.login.valid_code || $.trim($scope.login.valid_code).length != 4) {
			$('#valid_code').focus();
			return false;
		}

		$.LoadingOverlay("show");
		$.ajax({
			url : '../../enrol/school/login.htm',
			type : "POST",
			dataType : "json",
			data : $scope.login,
			success : function(data) {
				if (data.success == false) {
					if (data.errorCode == 1003) {
						$scope.msg('验证码错误，请检查！');
					} else
						$scope.msg(data.errorMsg);
				}else{
					$scope.std = data.entity;
					// 登录成功跳转系统首页
					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.getSchoolId = function() {
		var href = window.location.href.replace('http://', '').split('.')[0];
//		href = 12977;
		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();

})