/*

 */
angular.module("myApp").controller("timeSheetController", function($scope, $http) {
    console.log("Time Sheet controller");

    $scope.ts = {
        "id": "",
        "title": "",
        "content": "",
        "note": "",
        "eproject": "",
        "createdAt": ""
    };

    $scope.tsdto = {
        "id": "",
        "title": "",
        "content": "",
        "note": "",
        "eprojectId": "",
        "employeeId": ""
    };

    //get all time sheet
    $http({
        method : 'GET',
        url : "http://localhost:8080/eproject/timesheet/show"
    }).then(function successCallback(response) {
        console.log(response);
        $scope.myTimeSheets=response.data;
    }, function errorCallback(response) {
        console.log(response)
    });

    //delete time sheet
    $scope.delete = function (ts) {
        $http({
            method : 'DELETE',
            url : "http://localhost:8080/eproject/timesheet/delete/" + ts.id
        }).then(function successCallback(response) {
            console.log(response);
            $scope.config.timeSheets=response.data;
        }, function errorCallback(response) {
            console.log(response)
        });
    };


    $scope.save = save;
    function save(){
        console.log($scope.tsdto);
        $http({
            method : 'POST',
            url : "http://localhost:8080/eproject/timesheet/add",
            data: $scope.tsdto
        }).then(function successCallback(response) {
            console.log(response);
        }, function errorCallback(response) {
            console.log(response)
        });
    }

    $scope.getTs = getTs;
    function getTs(ts) {
        $scope.tsdto.id = ts.id;
        $scope.tsdto.title = ts.title;
        $scope.tsdto.content = ts.content;
        $scope.tsdto.note = ts.note;
        $scope.tsdto.employeeId = ts.eproject.employee.id;
    }

    $scope.update = update;
    function update(){
        $http({
            headers: "content-type: application/json",
            method : 'POST',
            url : "http://localhost:8080/eproject/timesheet/update",
            data: $scope.tsdto
        }).then(function successCallback(response) {
            console.log(response);
        }, function errorCallback(response) {
            console.log(response)
        });
    }
}).directive('pgnTable', ['$compile', function ($compile) {
    return {
        restrict: 'EA',
        templateUrl: 'pages/timesheet/timeSheet.html',
        replace: true,
        scope: {
            pages: "=pgnTable"
        },
        controller: function ($scope) {
            $scope.currentPage=1;
            $scope.numLimit=5;
            $scope.start = 0;
            $scope.$watch("pages",function(newVal){
                if(newVal){
                    $scope.pages=Math.ceil($scope.pages.length/$scope.numLimit);
                }
            });
            $scope.hideNext=function(){
                if(($scope.start + $scope.numLimit) < $scope.pages.length){
                    return false;
                }
                else
                    return true;
            };
            $scope.hidePrev=function(){
                if($scope.start===0){
                    return true;
                }
                else
                    return false;
            };
            $scope.nextPage=function(){
                console.log("next pages");
                $scope.currentPage++;
                $scope.start=$scope.start+ $scope.numLimit;
                console.log( $scope.start)
            };
            $scope.PrevPage=function(){
                if($scope.currentPage>1){
                    $scope.currentPage--;
                }
                console.log("next pages");
                $scope.start=$scope.start - $scope.numLimit;
                console.log( $scope.start)
            };
        },
        compile: function(elem) {
            return function(ielem, $scope) {
                $compile(ielem)($scope);
            };
        }
    };
}]);