/**
 * 
 */
angular.module("myApp").controller("employeeController", function($scope, $http,$window) {
	console.log("Employee controller");

	$scope.emp=   {
		"username": "",
		"password": "",
		"firstName": "",
		"lastName": "",
		"emailAddress": "",
		"phoneNumber": "",
		"homeTown": "",
		"education": "",
		"department": "",
		"position" : "",

	};



	$scope.save = save;

	function save(){
		console.log($scope.emp);
		$http({
			method : 'POST',
			url : "http://localhost:8080/list_employee/",
			data: $scope.emp
		}).then(function successCallback(response) {
			console.log(response);

		}, function errorCallback(response) {
			console.log(response)
		});
	}
	$scope.update = update;
	 function update(emp){
      $scope.emp = emp;
		 $http({
			 method : 'PUT',
			 url : "/employee/" +emp.id,
			 data: $scope.emp
		 }).then(function successCallback(response) {
			 console.log(response);

		 }, function errorCallback(response) {
			 console.log(response)
		 });
     }
	// HTTP DELETE- delete employee by Id
	// Call: http://localhost:8080/employee/{id}
    $scope.delete = function (emp) {
		$http({
			method : 'DELETE',
			url : '/employee/' + emp.id
		}).then(function successCallback(response) {
			console.log(response);
			$scope.employess=response.data;
		}, function errorCallback(response) {
			console.log(response)
		});
	};
	// In case of edit
	$scope.edit = function(emp){
	 	$scope.emp.username = emp.username;
	 	$scope.emp.password = emp.password;
	 	$scope.emp.firstName = emp.firstName;
	 	$scope.emp.lastName = emp.lastName;
	 	$scope.emp.emailAddress = emp.emailAddress;
	 	$scope.emp.phoneNumber = emp.phoneNumber;
	 	$scope.emp.homeTown  = emp.homeTown;
	 	$scope.emp.education = emp.education;
	 	$scope.emp.department = emp.department;
		$scope.emp.position = emp.position;
	};


	$http({
		method : 'GET',
		url : "http://localhost:8080/list_employee/"
	}).then(function successCallback(response) {
		console.log(response);

		$scope.employess=response.data;
	}, function errorCallback(response) {
		console.log(response)
	});
});