/*

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

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

    //get all time sheet
    //function getAllTimesheet(){
        $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)
        });
    //}

    //get all eproject
    $http({
        method: 'GET',
        url: "http://localhost:8080/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:8080/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:8080/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:8080/eproject/timesheet/update",
            data: $scope.tsdto
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    }

    // $scope.myTimeSheets = [];
    // $scope.save = save;
    // $scope.currentPage = 1;
    // $scope.timesheetPerPage = 3;
    // $scope.maxSize = 5;
    // this.myTimeSheets = $scope.myTimeSheets;
    // $scope.numOfPage = numOfPage;
    // $scope.dataHasLoaded = false;
    // $scope.makeTimesheet = function () {
    //     $scope.myTimeSheets = [];
    //     for (let i = 1; i <=50 ; i++) {
    //         $scope.myTimeSheets.push( {text : 'ts'+ i , done:false});
    //     }
    //
    // };
    // $scope.makeTimesheet();
    // $scope.getAllTimesheet = getAllTimesheet;
    // //$scope.getTotalTimesheet = getTotalTimesheet;
    // getAllTimesheet();
    // $scope.pageChangedIndex = pageChangedIndex;
    // function pageChangedIndex() {
    //     console.log($scope.currentPage);
    // }
    // function numOfPage() {
    //     return Math.ceil($scope.myTimeSheets.length/ $scope.timesheetPerPage);
    //
    // }
});
// }).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);
//             };
//         }
//     };
