/**
 *
 */
angular.module("myApp").controller("createEmployeeController", function ($scope, $http, $state, $stateParams) {
    console.log("Create Employee controller");
    console.log($stateParams.emp);

    if ($stateParams.emp) {
        $scope.emp = $stateParams.emp
    } else {
        $scope.emp = {
            "username": "",
            "password": "",
            "firstName": "",
            "lastName": "",
            "emailAddress": "",
            "phoneNumber": "",
            "homeTown": "",
            "education": "",
            "department": "",
            "position": ""

        };
    }


    $scope.save = save;

    function save() {

        if ($scope.emp.id) {
            $http({
                method: 'PUT',
                url: "/employee/" + $scope.emp.id,
                data: $scope.emp
            }).then(function successCallback(response) {
                console.log(response);
                $state.go('employees');
            }, function errorCallback(response) {
                console.log(response)
            });
        } else {
            $http({
                method: 'POST',
                url: "http://localhost:8081/list_employee/",
                data: $scope.emp
            }).then(function successCallback(response) {
                console.log(response);
                $state.go('employees');

            }, function errorCallback(response) {
                console.log(response)
            });
        }
    }

});

