/*  setting - certification module */
app.service('CertificationService', [function(){
	
}]);

app.controller('CertificationController', ['$scope', '$ionicModal', '$timeout', function($scope, $ionicModal, $timeout){
	
	$scope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
		if(toState.name == "app.setting.main" && fromState.name == "app.setting.certify"){
			if($scope.certifyInfoChanged()){
				$scope.showConfirm("警告","强制返回则当前修改失效,强制返回？", function(res){
					if(res){
						$scope.restoreCertifyInfo();
						$scope.$ionicGoBack();
					}
				});	
				event.preventDefault();		
			}
		}
	});

	$scope.certifyConfirm = function(){
		$scope.goState('app.home.main');
	};

	$scope.saveCertifyInfoChanged = function(){
		$scope.setUserInfo($scope.certifyInfo);
		$scope.showAlert(null, '保存修改成功');
	};

	function stringValueIsEqual(oldStr, newStr){
		if((!oldStr || oldStr.trim()=="") && (!newStr || newStr.trim()=="")){
			return true;
		}else if(oldStr && newStr){
			return oldStr.trim() == newStr.trim();
		}
		return false;	
	}

	$scope.certifyInfoChanged = function(){
		var oldInfo = $scope.userInfo;
		var newInfo = $scope.certifyInfo;
		return !(stringValueIsEqual(newInfo.company,oldInfo.company) && stringValueIsEqual(newInfo.department,oldInfo.department) && stringValueIsEqual(newInfo.job,oldInfo.job));
	};

	$scope.restoreCertifyInfo = function(){
		for (var i in $scope.userInfo) {
			$scope.certifyInfo[i] = $scope.userInfo[i];
		};
	};
	

	function loadUserInfo(){
		$scope.showLoading();		
		$timeout(function(){
			for (var i in $scope.userInfo) {
				$scope.certifyInfo[i] = $scope.userInfo[i];
			};
			$scope.hideLoading();
		}, 500);
	};

	$scope.showCompanys = function(){
		showModal($scope.modal_companies, "选择公司", "company");
	};
	$scope.showDepartments = function(){
		showModal($scope.modal_departments, "选择部门", "department");
	};
	$scope.showJobs = function(){
		showModal($scope.modal_jobs, "选择岗位", "job");
	};
	function showModal(itemsData, title, level){
		$scope.brModal.selectItems = itemsData;
		$scope.brModal.title = title;
		$scope.brModal.data.level = level;
		$scope.modal.show();
		$scope.modal_type = level;
		switch(level){
			case 'company':
				$scope.brModal.placeholder = "输入 公司名 搜索";
				$scope.queryCompaninesData();			
				break;
			case 'department':
				$scope.brModal.placeholder = "输入 部门名 进行搜索";
				$scope.queryDepartmentsData();
				break;
			case 'job':
				$scope.brModal.placeholder = "输入 岗位名 进行搜索";
				$scope.queryJobsData();
				break;
		}
	};


	/*
	 * 弹出选择modal界面相关的代码 
	 */

	$scope.certifyInfo = {};
	$scope.modal_type = '';
	$scope.modal_companies = [];
	$scope.modal_departments = [];
	$scope.modal_jobs = [];
	$scope.brModal = {
		selectItems: [],
		title: '请选择',
		data: {
			clientSide: '',
			level: ''
		},
		placeholder: '输入搜索内容'		
	};
	$scope.modalTempData = {
		company: '',
		department: '',
		job: ''
	};

	function hideModal(){
		$scope.modal_type = '';
		$scope.modal_companies = [];
		$scope.modal_departments = [];
		$scope.modal_jobs = [];
		$scope.brModal = {
			selectItems: [],
			title: '请选择',
			data: {
				clientSide: '',
				level: ''
			}
			//placeholder: '输入搜索内容'		
		};
		$scope.modalTempData = {
			company: '',
			department: '',
			job: ''
		};
		$scope.query.queryStr = "";
		$scope.modal.hide();
	};
	
	function clearItemSelected(array){
		for (var i = array.length - 1; i >= 0; i--) {
			array[i].selected = false;
		};
	}

	function hasSelectedOne(array, errorMes){		
		for (var i = array.length - 1; i >= 0; i--) {
			if (array[i].selected) return true;
		};
		$scope.showAlert('操作失误', errorMes, null);
		return false;
	}

	$scope.query = {
		queryStr : ""
	}; //搜索框的输入内容
	$scope.remoteSearch = function(){
		var query = $scope.query.queryStr;
		if(!query || query=="" || query.trim()==""){
			$scope.showAlert(null, '搜索内容不能为空');
			return;
		}
		$scope.showLoading();
		$timeout(function(){
			switch($scope.modal_type){
				case 'company':
					$scope.queryCompaninesData(query);
					break;
				case 'department':
					$scope.queryDepartmentsData(query);
					break;
				case 'job':
					$scope.queryJobsData(query);
					break;
			}
			delete $scope.queryStr;
			$timeout(function(){
				$scope.hideLoading();
			}, 2000);			
		}, 3000);	
	};

	$scope.selectedItem = function(item){		
		switch($scope.modal_type){
			case 'company':
				$scope.modalTempData.company = item.text;
				clearItemSelected($scope.modal_companies);
				break;
			case 'department':
				$scope.modalTempData.department = item.text;
				clearItemSelected($scope.modal_departments);
				break;
			case 'job':
				$scope.modalTempData.job = item.text;
				clearItemSelected($scope.modal_jobs);
				break;
		}
		item.selected = true;
	};

	$scope.selectOk =  function(){
		var hasOne = false;
		switch($scope.modal_type){
			case 'company':
				hasOne = hasSelectedOne($scope.modal_companies, "请选择一个公司");
				if(hasOne) $scope.certifyInfo.company = $scope.modalTempData.company;
				break;
			case 'department':
				hasOne = hasSelectedOne($scope.modal_departments, "请选择一个部门");
				if(hasOne) $scope.certifyInfo.department = $scope.modalTempData.department;
				break;
			case 'job':
				hasOne = hasSelectedOne($scope.modal_jobs, "请选择一个岗位");
				if(hasOne) $scope.certifyInfo.job = $scope.modalTempData.job;
				break;
		}
		if(hasOne){
			$scope.modal_type = '';
			//$scope.modal.hide();
			hideModal();
		}
	}
	$scope.cancleSelect = function(){
		switch($scope.modal_type){
			case 'company':
				clearItemSelected($scope.modal_companies);
				break;
			case 'department':
				clearItemSelected($scope.modal_departments);
				break;
			case 'job':
				clearItemSelected($scope.modal_jobs);
				break;
		}
		$scope.modal_type = '';
//		$scope.modal.hide();
		hideModal();
	}

	$scope.queryCompaninesData = function(queryStr){
		$scope.showLoading();
		$timeout(function(){
			$scope.modal_companies = [
				{ text: '公司 - 1', value: '1' },
				{ text: '公司 - 2', value: '2' },
				{ text: '公司 - 3', value: '3' },
				{ text: '公司 - 4', value: '4' },
				{ text: '公司 - 5', value: '5' },
				{ text: '公司 - 6', value: '6' },
				{ text: '公司 - 7', value: '7' },
				{ text: '公司 - 8', value: '8' },
				{ text: '公司 - 9', value: '9' },
				{ text: '公司 - 10', value: '10' },
				{ text: '公司 - 1', value: '1' },
				{ text: '公司 - 2', value: '2' },
				{ text: '公司 - 3', value: '3' },
				{ text: '公司 - 4', value: '4' },
				{ text: '公司 - 5', value: '5' },
				{ text: '公司 - 6', value: '6' },
				{ text: '公司 - 7', value: '7' },
				{ text: '公司 - 8', value: '8' },
				{ text: '公司 - 9', value: '9' },
				{ text: '公司 - 10', value: '10' }
			];
			if(queryStr && queryStr!="" && queryStr.trim()!=""){
				for (var i = $scope.modal_companies.length - 1; i >= 0; i--) {
					$scope.modal_companies[i].text = queryStr + $scope.modal_companies[i].text;
				}
			}
			$scope.brModal.selectItems = $scope.modal_companies;
			$scope.hideLoading();
		}, 2000);
	};
	$scope.queryDepartmentsData = function(queryStr){
		$scope.showLoading();
		$timeout(function(){
			$scope.modal_departments = [
				{ text: '部门 - 1', value: '1' },
				{ text: '部门 - 2', value: '2' },
				{ text: '部门 - 3', value: '3' },
				{ text: '部门 - 4', value: '4' },
				{ text: '部门 - 5', value: '5' },
				{ text: '部门 - 6', value: '6' },
				{ text: '部门 - 7', value: '7' },
				{ text: '部门 - 8', value: '8' },
				{ text: '部门 - 9', value: '9' },
				{ text: '部门 - 10', value: '10' }
			];
			if(queryStr && queryStr!="" && queryStr.trim()!=""){
				for (var i = $scope.modal_departments.length - 1; i >= 0; i--) {
					$scope.modal_departments[i].text = queryStr + $scope.modal_departments[i].text;
				}
			}
			$scope.brModal.selectItems = $scope.modal_departments;
			$scope.hideLoading();
		}, 2000);
	};
	$scope.queryJobsData = function(queryStr){
		$scope.showLoading();
		$timeout(function(){
			$scope.modal_jobs = [
				{ text: '岗位 - 1', value: '1' },
				{ text: '岗位 - 2', value: '2' },
				{ text: '岗位 - 3', value: '3' },
				{ text: '岗位 - 4', value: '4' },
				{ text: '岗位 - 5', value: '5' },
				{ text: '岗位 - 6', value: '6' },
				{ text: '岗位 - 7', value: '7' },
				{ text: '岗位 - 8', value: '8' },
				{ text: '岗位 - 9', value: '9' },
				{ text: '岗位 - 10', value: '10' }
			];
			if(queryStr && queryStr!="" && queryStr.trim()!=""){
				for (var i = $scope.modal_jobs.length - 1; i >= 0; i--) {
					$scope.modal_jobs[i].text = queryStr + $scope.modal_jobs[i].text;
				}
			}
			$scope.brModal.selectItems = $scope.modal_jobs;
			$scope.hideLoading();
		}, 2000);
	};

	// 界面初始化时要执行的操作
	$scope.init = function(){
		loadUserInfo();

		$ionicModal.fromTemplateUrl('templates/modal_selects.html', {
			scope: $scope,
			animation: 'slide-in-up'
		}).then(function(modal) {
			$scope.modal = modal;
		});			
	};
	$scope.init();

}]);




