(function(){
  angular.module('presseApp',['apiUserAuthModule','heuer.analyticsTracking'])
  .factory('PresseService',PresseService)
  .controller('PresseController',PresseController);
  
PresseService.$inject = ["$http","$log","$q"];
function PresseService($http,$log,$q){
	return {
	  sendPresseVerteilerMail : sendPresseVerteilerMail
	};
  
	function sendPresseVerteilerMail(PresseVerteilerDo){
	  var defer = $q.defer();
	  var req = {
		method: 'POST',
		url: IZConstants.baseUrl + IZConstants.apiUrl +'/presse/presseverteiler',
		data:PresseVerteilerDo
	  }
	  $http(req)
		.then(function(res){
		defer.resolve(res);
	  },function(err){
		defer.reject(err);
	  });
	  return defer.promise;
	}
  
  }
  
 PresseController.$inject=["PresseService","$window"];
  
 function PresseController(PresseService,$window){
   
  var vm = this;
  vm.showSuccessMsg=false;
  vm.showErrorMsg=false;
  vm.loading=false;
  vm.PresseVerteilerDo={};
  vm.sendPresseVerteilerMail=sendPresseVerteilerMail;
   vm.fireGoogleAnalyticsEvent=fireGoogleAnalyticsEvent;
   
  vm.init = init;
  //Konstruktor
  init();

  function init(){
	vm.fireGoogleAnalyticsEvent('Besuch');
	vm.PresseVerteilerDo={
	  'salutation':(IZConstants.accountData)?IZConstants.accountData.salutation:'Herr',
	  'title':'',
	  'firstName':(IZConstants.accountData)?IZConstants.accountData.firstName:'',
	  'lastName':(IZConstants.accountData)?IZConstants.accountData.lastName:'',
	  'purpose':'new',
	  'press':'',
	  'redaction':'',
	  'magazine':'',
	  'city':'',
	  'street':'',
	  'country':'Deutschland',
	  'telephone':'',
	  'email':(IZConstants.accountData)?IZConstants.accountData.email:'',
	  'remark':''
	}
  };
   	
	function fireGoogleAnalyticsEvent(cause){
	if ($window.ga){
	  console.log('GA PresseVerteiler Event');
	  $window.ga('send','event','Presse','PresseVerteiler',cause);
	}
  }
   
  function sendPresseVerteilerMail(frm){
	
	console.info('=========sendPresseVerteilerMail========');
	vm.PresseVerteilerDo;
	vm.loading=true;
	if(!frm.$valid){
	  console.log(frm.$error);
	  console.info('=========invalid========');
	  vm.loading=false;
	  return;
	}
	
	vm.fireGoogleAnalyticsEvent('Bestellt');
	console.info('=========valid========');
	PresseService.sendPresseVerteilerMail(vm.PresseVerteilerDo).then(function (result) {
	  vm.loading=false;
	  vm.showSuccessMsg = true;
	},function(err){
	  vm.loading=false;
	  vm.showErrorMsg = true;
	  vm.errorMsg = err;
	})
  };
}
}());

