<script>
	
	MAMP.Subapp.Admin.controller('Affiliation', ['$scope','$http','$location', function($scope,$http,$location,$window) 
	{
		
		var ebook_volume_id =  @isset($volume->ebook_volume_id) {{ $volume->ebook_volume_id }} @else ''  @endisset ;
		var article_id =  @isset($article->article_id) {{ $article->article_id }} @else ''  @endisset ;
		var chapter_id =  @isset($selected_chapter->chapter_id) {{ $selected_chapter->chapter_id }} @else ''  @endisset ;
		
		
		$scope.affiliations_heading = 'Add Affiliation';
		$scope.action = 'INSERT';
		$scope.method = 'affiliation';
			
				
			if (ebook_volume_id)
			{
				post_path = '/admin/ebook_volume';
				
			}
			if (article_id)
			{				post_path = '/admin/articles';
				
			}
			if (chapter_id)
			{				post_path = '/admin/chapters';
				
			}
			
			
			
		$scope.GetAffiliation = function () 
		{
			$scope.affiliations = [];	
			
			$http({
				  method: 'POST',
				  url: post_path,
				  data: { 
					'ajax' : true,
					'action' 		: 'SELECT',
					'method' 		: 'Affiliation',
					'ebook_volume_id' : ebook_volume_id,
					'article_id' : article_id,
					'chapter_id' : chapter_id,
					'author_id' : $('#author_id').val()
				  }
				})
			.then(function success(e) {
				
				if (e.data.selected_affiliation)
				{
					
					angular.forEach(e.data.selected_affiliation, function(value, key) {
					  $scope.affiliations.push(value);	
					});
				}
				 
			});
		};
			
		$scope.AddAffiliation = function () 
		{
			$scope.insert_validate = true;
			$("#div_address").removeClass('has-error');
			if($scope.address == undefined || $scope.address == '')
			{
				$("#div_address").addClass('has-error');
				$scope.insert_validate = false;
			}
			if($scope.web_view == undefined || $scope.web_view == '')
			{
				$("#div_web_view").addClass('has-error');
				$scope.insert_validate = false;
			}
			if($scope.insert_validate == true)
			{
				$scope.data =   
				{
					'ajax' 			: true,
					'action' 		: 'INSERT',
					'method'		: 'Affiliation',
					'ebook_volume_id':ebook_volume_id,
					'article_id'	:article_id,
					'chapter_id'	:chapter_id,
					'author_id'		:	$('#author_id').val(),
					'institution' 	: $scope.institution,
					'department' 	: $scope.department,
					'address' 		: $scope.address,
					'country' 		: $scope.aff_country,
					'city' 			: $scope.city,
					'postal_code' 	: $scope.postal_code,
					'phone' 		: $scope.phone,
					'fax' 			: $scope.fax,
					'web_view' 		: $scope.web_view,
				}
			
				$http({ method: 'POST',url: post_path,data: $scope.data })
				.then(function successCallback(response) 
				{
					$scope.affiliations.push(response.data.insert);
					
					$scope.institution = '';
					$scope.department = '';
					$scope.address = '';
					$scope.aff_country = '';
					$scope.city = '';
					$scope.postal_code = '';
					$scope.phone = '';
					$scope.fax = '';
					$scope.web_view = '';
					
				});
			}
		
				
		};
		
		$scope.UpdateAffiliation = function () 
		{
			$scope.insert_validate = true;
			$("#div_address").removeClass('has-error');
			if($scope.address == undefined || $scope.address == '')
			{
				$("#div_address").addClass('has-error');
				$scope.insert_validate = false;
			}
			if($scope.web_view == undefined || $scope.web_view == '')
			{
				$("#div_web_view").addClass('has-error');
				$scope.insert_validate = false;
			}
			if($scope.insert_validate == true)
			{
				$scope.update =   
				{
					'ajax' 			: true,
					'action' 		: 'UPDATE',
					'method' 		: 'Affiliation',
					'aff_id' 		: $scope.affiliations[$scope.index].aff_id,
					'author_id'		:	$('#author_id').val(),
					'institution' 	: $scope.institution,
					'department' 	: $scope.department,
					'address' 		: $scope.address,
					'country' 		: $scope.aff_country,
					'city' 			: $scope.city,
					'postal_code' 	: $scope.postal_code,
					'phone' 		: $scope.phone,
					'fax' 			: $scope.fax,
					'web_view' 		: $scope.web_view,
				}
				
				$http({ method: 'POST',url: post_path,data: $scope.update })
				.then(function successCallback(response) 
				{
					$scope.affiliations[$scope.index].institution = $scope.institution;
					$scope.affiliations[$scope.index].department = $scope.department;
					$scope.affiliations[$scope.index].address = $scope.address;
					$scope.affiliations[$scope.index].country = $scope.aff_country;
					$scope.affiliations[$scope.index].city = $scope.city;
					$scope.affiliations[$scope.index].postal_code = $scope.postal_code;
					$scope.affiliations[$scope.index].phone = $scope.phone;
					$scope.affiliations[$scope.index].fax = $scope.fax;
					$scope.affiliations[$scope.index].web_view = $scope.web_view;
					
					$scope.affiliations_heading = 'Add Affiliation';
					$scope.action = 'INSERT';
					$scope.method = 'Affiliation';
					
					$scope.institution = '';
					$scope.department = '';
					$scope.address = '';
					$scope.aff_country = '';
					$scope.city = '';
					$scope.postal_code = '';
					$scope.phone = '';
					$scope.fax = '';
					$scope.web_view = '';
				});
			}
		};
		
		$scope.EditAffiliation = function (index) 
		{
			
			$("#div_address").removeClass('has-error');

			$scope.institution = $scope.affiliations[index].institution;
			$scope.department = $scope.affiliations[index].department;
			$scope.address = $scope.affiliations[index].address;
			$scope.aff_country = $scope.affiliations[index].country;
			$scope.postal_code = $scope.affiliations[index].postal_code;
			$scope.city = $scope.affiliations[index].city;
			$scope.phone = $scope.affiliations[index].phone;
			$scope.fax = $scope.affiliations[index].fax;
			$scope.web_view = $scope.affiliations[index].web_view;
			$scope.index = index;
			$scope.action = 'UPDATE';
			$scope.method = 'Affiliation';
			$scope.affiliations_heading = 'Update Affiliation';
			
		};
		
		$scope.DeleteAffiliation = function (index) {
 
			$http({
				  method: 'POST',
				  url: post_path,
				  data: { 
					'action' : 'DELETE',
					'method' : 'Affiliation',
					'ajax' : true,
					'aff_id' : $scope.affiliations[index].aff_id
				  }
				})
				.then(function success(e) {
				
					$scope.affiliations.splice(index, 1);
				});

			};

	}]);
	
	function get_affiliation(arg)
	{
		$('#author_id').val(arg);
		document.getElementById("get_aff").click();
	}
  
</script>
