/*

*/
angular.module("myApp").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: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();
        });
    }
});
