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

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

    };


    $scope.save = save;
    $scope.employess = [];

    $scope.currentPage = 1;
    $scope.employeePerPage = 3;
    $scope.maxSize = 5;
    this.emp = $scope.employess;
    $scope.numberOfPage = numberOfPage;
    $scope.dataHasLoaded = false;
    $scope.makeEmployee = function () {
        $scope.employess = [];
        for (i = 1; i <= 100; i++) {
            $scope.employess.push({text: 'emp ' + i, done: false});
        }
    };
    $scope.makeEmployee();
    $scope.getAllEmp = getAllEmp;
    $scope.getTotalItems = getTotalItems;
    $scope.keyword = "";

    getAllEmp();


    function getTotalItems() {
        return $filter('filter')($scope.employess, {'username': $scope.keyword}).length;
    }

    $scope.pageChangedIndex = function(){
        console.log($scope.currentPage);
    }

    function numberOfPage() {
        return Math.ceil($scope.employess.length / $scope.employeePerPage);

    };

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

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

    $scope.updateEmp = updateEmp;

    function updateEmp(emp) {
        console.log('edit');
        $state.go('createEmployee', {emp: emp});

    }


    $scope.deleteEmp = function (emp) {
        $http({
            method: 'DELETE',
            url: '/employee/' + emp.id
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
            $scope.employess = response.data;
        }, function errorCallback(response) {
            console.log(response)
        });
    }
    $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;
    };

    function getAllEmp(){
        $http({
            method: 'GET',
            url: "http://localhost:8081/list_employee/"
        }).then(function successCallback(response) {
            console.log(response);
            $scope.employess = response.data;
        }, function errorCallback(response) {
            console.log(response)
        });

    }

});





