app.controller('AppCtrl', function($rootScope, $scope, $http, $state, $timeout) {
	// 请求初始化信息，这里用同步模式
	$.LoadingOverlay("show");
	$.ajax({
		url : '../enrol/std/info/init.htm',
		async : false,
		type : "POST",
		dataType : "json",
		success : function(data) {
			$.LoadingOverlay("hide");
			if (data.success == false) {
				var msg = '鉴权失败，请先登录！';
				if (data.login) {
					msg = data.errorMsg;
				}
				$rootScope.msg(msg, '错误提示', function() {
					window.location = './login/login.jsp';
				});
				return;
			} else {
				$rootScope.GobalStdReg = data.map.StdReg;
				$rootScope.GobalSchoolReg = data.map.SchoolReg;
				$rootScope.GobalInitMap = data.map;
			}
		},
		error : function() {
			$.LoadingOverlay("hide");
			$rootScope.msg('服务器出现内部错误，请稍候重试！', '错误提示', function() {
				window.location = './login/login.jsp';
			});
			return;
		}
	});

	// 通用ajax请求
	$rootScope.ajaxRequest = function(url, param, callback, type) {
		if (param == undefined)
			param = {};
		var obj;
		if ($rootScope.GobalStdReg)
			param.std_id = $rootScope.GobalStdReg.std_id;
		if ($rootScope.GobalSchoolReg)
			param.school_id = $rootScope.GobalSchoolReg.school_id;

		$http({
			url : url,
			method : 'post',
			data : param
		}).then(function success(response) {
			var data = response.data;
			if (type == 'entity')
				obj = data.entity;
			else if (type == 'array')
				obj = data.array;
			else if (type == 'map')
				obj = data.map;
			else if (type == 'page')
				obj = data.page;
			else
				obj = data;
			if (callback != undefined) {
				callback.call(undefined, obj);
			}
		});
	}
	// $.LoadingOverlay("show");
	$rootScope.goUrl = function(url, param) {
		// 确保能刷新子路由
		// $state.reload();
		$timeout(function() {
			$state.go('app.' + url, param);
		}, 100);
	}

	$rootScope.logout = function() {
		$rootScope.ajaxRequest('../enrol/std/logout.htm', {}, function() {
			window.location = './login/login.jsp';
		});
	}

	$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|7|8]\d{9}$)|(^09\d{8}$)/;
		if (!regex.test(v)) {
			return false;
		}
		return true;
	}
}).filter('CfSubjectType', function() {
	return function(data) {
		var result = '';
		switch (data) {
		case 'Face':
			result = '面试';
			break;
		case 'Write':
			result = '笔试';
			break;
		case 'Pc':
			result = '机考';
			break;
		case 'Hand':
			result = '实操';
			break;
		}

		return result;
	}
});