/*

*/
angular.module("myApp")
    .filter('beginningTimeSheet', function() {
        return function(input, begin) {
            if (input) {
                begin = +begin;
                return input.slice(begin);
            }
            return [];
        }
    })
    .controller("timeSheetController", function($scope, $http, $state) {
    console.log("Time Sheet controller");

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

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

            $scope.timeSheetList = response.data;
            $scope.currentPage = 1;
            $scope.timeSheetPerPage = 3;
            $scope.filterTimeSheet = $scope.timeSheetList.length;
            $scope.entireTimeSheet = $scope.timeSheetList.length;
        }, function errorCallback(response) {
            console.log(response)
        });

    $scope.pagePosition = function(pageNumber) {
        $scope.currentPage = pageNumber;
    };

    $scope.filter = function() {
        $timeout(function() {
            $scope.filterTimeSheet = $scope.searched.length;
        }, 20);
    };
    $scope.sortWith = function(base) {
        $scope.base = base;
        $scope.reverse = !$scope.reverse;
    };

    //get all eproject
    $http({
        method: 'GET',
        url: "http://localhost:8081/eproject/getAll"
    }).then(function successCallback(response) {
        console.log(response);
        $scope.eProjectList = response.data;
    }, function errorCallback(response) {
        console.log(response)
    });
    //delete time sheet
    $scope.delete = function (ts) {
        $http({
            method: 'DELETE',
            url: "http://localhost:8081/eproject/timesheet/delete/" + ts.id
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    };


    $scope.save = save;

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

    $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:8081/eproject/timesheet/update",
            data: $scope.tsdto
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    }
});
