define([ 'toaster', 'services' ,'ui.router', 'file-upload', 'file-upload-shim'], function() {
	'use strict';
	var app = angular.module('myApp', [ 'toaster', 'common.services', 'angularFileUpload', 'ui.router']);
	app.init = function() {
		angular.bootstrap(document, [ 'myApp' ]);
	};
	app.config(function($stateProvider, $urlRouterProvider){
		$urlRouterProvider.otherwise("/step1");
		$stateProvider.state('step1', {
			url: '/step1',
      		templateUrl: 'static/tpl/register/step1.html'
    	}).state('step2', {
			url: '/step2',
      		templateUrl: 'static/tpl/register/step2.html'
    	}).state('step3', {
			url: '/step3/:uu/:checkcode',
      		templateUrl: 'static/tpl/register/step3.html'
    	});
	});
	
	app.controller('Step1Ctrl', function($scope, $http, $location, $upload, toaster) {
		$scope.loadingShow = false;//加载小图标，设为true即可显示
		$scope.title = '注册表单';
		$scope.enterprise = {};//企业实体
		$scope.agree = true;//默认同意服务协议
		
		/**
		 * 处理企业信息注册提交
		 */
		$scope.register = function(){
			$scope.loadingShow = true;
			var file = $scope.myFiles[0];
			console.log($scope.enterprise);
			$upload.upload({
				url: 'signin/register.action',
				file: file,
				method: 'POST',
				data: {
					enterprise: $scope.enterprise
				}
			}).success(function(data){
				$scope.loadingShow = false;
				$location.path('step2');
			}).error(function(data){
				$scope.loadingShow = false;
				toaster.pop('error', '操作失败', data.error);
			});
		};
		
		$scope.$watch('enterprise.enBussinessCode', function(){
			if($scope.enterprise.enBussinessCode && $scope.enterprise.enBussinessCode.length==15) {
				$http.get('signin/codeEnable', {
					params: {
						code: $scope.enterprise.enBussinessCode
					}
				}).success(function(data){
					console.log(data);
					if(data == 'ENABLE') {
						$scope.myForm.enBussinessCode.$setValidity('available', true);
					} else if(data == 'USED') {
						$scope.myForm.enBussinessCode.$setValidity('available', false);
						$scope.codeErrorInfo = '执照号已注册';
					} else {
						$scope.myForm.enBussinessCode.$setValidity('available', false);
						$scope.codeErrorInfo = '执照号已申请';
					}
				}).error(function() {
					$scope.myForm.enBussinessCode.$setValidity('available', false);
					$scope.codeErrorInfo = '验证出错';
				});
			} else {
				$scope.myForm.enBussinessCode.$setValidity('available', false);
				if($scope.enterprise.enBussinessCode) $scope.codeErrorInfo = '格式错误';
			}
		});
		
		$scope.$watch('enterprise.enAdminTel', function(){
			if($scope.enterprise.enAdminTel) {
				$http.get('signin/telEnable', {
					params: {
						tel: $scope.enterprise.enAdminTel
					}
				}).success(function(data){
					console.log(data);
					if(data && data == 'true') {
						$scope.myForm.enAdminTel.$setValidity('available', true);
					} else {
						$scope.myForm.enAdminTel.$setValidity('available', false);
						$scope.telErrorInfo = '手机号已被使用';
					}
				}).error(function() {
					$scope.myForm.enAdminTel.$setValidity('available', false);
					$scope.telErrorInfo = '验证出错';
				});
			} else {
				$scope.myForm.enAdminTel.$setValidity('available', false);
			}
		});
		$scope.$watch('enterprise.enAdminEmail', function(){
			if($scope.enterprise.enAdminEmail) {
				$http.get('signin/emailEnable', {
					params: {
						email: $scope.enterprise.enAdminEmail
					}
				}).success(function(data){
					if(data && data == 'true') {
						$scope.myForm.enAdminEmail.$setValidity('available', true);
					} else {
						$scope.myForm.enAdminEmail.$setValidity('available', false);
						$scope.emailErrorInfo = '邮箱已被使用';
					}
				}).error(function(){
					$scope.myForm.enAdminEmail.$setValidity('available', false);
					$scope.emailErrorInfo = '验证出错';
				});
			} else {
				$scope.myForm.enAdminEmail.$setValidity('available', false);
			}
		});
		$scope.previewShow = false;
		$scope.pdfShow = false;
		$scope.noneFileShow = true;
		$scope.$watch('myFiles', function(){
			if($scope.myFiles) {
				$scope.noneFileShow = false;
				var file = $scope.myFiles[0];
				if(file.name.indexOf('.pdf') != -1 || file.name.indexOf('.PDF') != -1) {//pdf文件
					$scope.pdfShow = true;
					$scope.previewShow = false;
					$scope.imgPreviewName = file.name;
				} else{
					if (typeof(Worker) !== "undefined") {//支持HTML5
						$scope.imgPreview = getObjectURL(file);
						$scope.imgPreviewName = file.name;
					} else{//不支持HTML5
						$scope.imgPreview = 'static/img/all/preview-error.jpg';
						$scope.imgPreviewName = file.name;
					}
					$scope.previewShow = true;
					$scope.pdfShow = false;
				}
				console.log($scope.myFiles[0]);
				console.log(getObjectURL($scope.myFiles[0]));
			} else {
				$scope.noneFileShow = true;
				$scope.previewShow = false;
				$scope.pdfShow = false;
			}
		});
		//建立一个可存取到该file的url
		var getObjectURL = function(file) {
			var url = null ; 
			if (window.createObjectURL!=undefined) { // basic
				url = window.createObjectURL(file) ;
			} else if (window.URL!=undefined) { // mozilla(firefox)
				url = window.URL.createObjectURL(file) ;
			} else if (window.webkitURL!=undefined) { // webkit or chrome
				url = window.webkitURL.createObjectURL(file) ;
			}
			return url ;
		};
	});
	
	app.controller('Step3Ctrl', function($scope, $http, $location, $upload, toaster, $stateParams) {
		$scope.uu = $stateParams.uu;
		$scope.checkcode = $stateParams.checkcode;
		$scope.activateResult = false;
		$scope.activateSuccess = false;
		$scope.activateSendAgain = false;
		
		setTimeout(function() {//延迟5秒执行
			$http.post('signin/activate?uu=' + $scope.uu + '&checkcode=' + $scope.checkcode)
			.success(function(data){
				$scope.checkResult = '成功';
				$scope.activateResult = true;
				$scope.activateSuccess = true;
			}).error(function(data){
				$scope.checkResult = '失败，原因 - '+ data.result ;
				$scope.activateResult = true;
				if(data.result == '验证码错误，激活失败') {
					$scope.activateSendAgain = true;
				}
			});
		}, 5000);
		
		$scope.setAdminPassword = function(enuu, psd) {
			$scope.loadingShow = true;
			$http.post('signin/setAdminPassword?enuu=' + enuu + '&password=' + psd).success(function(data){
				$scope.loadingShow = false;
				$scope.setSuccess = true;
			}).error(function(data) {
				$scope.loadingShow = false;
				$scope.setFaild = true;
			});
		};
		
		$scope.sendEmailAgain = function(enuu){
			$scope.loadingShow = true;
			$http.get('signin/setEmailAgain?enuu=' + enuu).success(function(data){//发送失败
				$scope.loadingShow = false;
				$scope.emailDisabled = true;
				$scope.sendSuccess = true;
			}).error(function(data){//发送成功
				$scope.loadingShow = false;
				$scope.sendFaild = true;
			});
		};
	});
	return app;
});