/*

*/
angular.module("myApp").controller("leaveFormController", function($scope, $http, $state) {
    console.log("Leave Form controller");

    $scope.lf = {
        "id": "",
        "title": "",
        "content": "",
        "employee": "",
        "leaveType": "",
        "updatedAt": "",
        "status": ""
    };

    $scope.lfdto = {
        "id": "",
        "title": "",
        "content": "",
        "employeeId": "",
        "leaveTypeId": ""
    };

    //get all leave form
    $http({
        method : 'GET',
        url : "http://localhost:8080/employee/leaveform/show"
    }).then(function successCallback(response) {
        console.log(response);
        $scope.leaveForms=response.data;
    }, function errorCallback(response) {
        console.log(response)
    });

    //get all leave type
    $http({
        method : 'GET',
        url : "http://localhost:8080/employee/leaveType/getAll"
    }).then(function successCallback(response) {
        console.log(response);
        $scope.leaveTypeList=response.data;
    }, function errorCallback(response) {
        console.log(response)
    });
    //delete leave form
    $scope.delete = function (lf) {
        $http({
            method : 'DELETE',
            url : "http://localhost:8080/employee/leaveform/delete/" + lf.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.lfdto);
        $http({
            method : 'POST',
            url : "http://localhost:8080/timesheet/leaveform/add",
            data: $scope.lfdto
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    }

    $scope.getLf = getLf;
    function getLf(lf) {
        $scope.lfdto.id = lf.id;
        $scope.lfdto.title = lf.title;
        $scope.lfdto.content = lf.content;
        $scope.lfdto.employeeId = lf.employee.id;
        $scope.lfdto.leaveTypeId = lf.leaveType.id;
    }

    $scope.update = update;
    function update(){
        $http({
            method : 'POST',
            url : "http://localhost:8080/timesheet/leaveform/update",
            data: $scope.lfdto
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    }

    $scope.updateStatus = updateStatus;
    function updateStatus(lf){
        $http({
            method : 'POST',
            url : "http://localhost:8080/timesheet/leaveform/"+lf.id+"/status",
            // data: $scope.lfdto
        }).then(function successCallback(response) {
            console.log(response);
            $state.reload();
        }, function errorCallback(response) {
            console.log(response);
            $state.reload();
        });
    }

});
