var app = angular.module('myApp');
app.controller('reportController',projectList);
app.controller('timeSheetListController' , timeSheetList);
app.controller('updateTimeSheetStatusController',updateTimeSheetStatus);
app.controller('updateTimeSheetCheckedController', updateTimeSheetChecked);
app.controller('listLackOfReportController',employeeList);
app.controller('insertProjectReportController', insertProjectReport);
app.controller('allProjectReportController',allProjectReport);
app.controller('projectReportDetailController', projectReportDetail);
app.controller('deleteReportController',deleteReport);
app.controller('listEmployeeLackController',listEmployeeLack);




// Danh sách các dự án đang triển khai
function projectList($scope, $http) {

    $http({
        method: 'GET',
        url:"http://localhost:8081/admin/report/project",
        headers: {
            'Content-Type': 'application/json'
        }

    }).then(function successCallback(response) {
        console.log(response);
        $scope.projects = response.data;
    }, function errorCallback(response) {
        console.log(response)
    });

}

//======================================================================================================================
// Danh Sách Báo cáo ngày của dự án đó
function timeSheetList($scope, $http, $stateParams){
    $scope.request = {
        "currentDate": ""
    };
    var date = new Date();
    $scope.request.currentDate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
    $http({
        method: 'GET',
        url: "http://localhost:8081/admin/report/project/"+$stateParams.ID+"/timeSheet/"+$scope.request.currentDate,
        headers: {
            'Content-Type': 'application/json'
        }

    }).then(function successCallback(response) {
        console.log(response);
        $scope.timeSheets= response.data;
        console.log($scope.timeSheets);
    }, function errorCallback(error) {
        console.log(error)
    });
}

function updateTimeSheetStatus($scope, $window, $http) {
    $scope.request = {
        "currentDate": ""
    };
    var date = new Date();
    $scope.request.currentDate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
    $scope.updateTimeSheetStatus = function (id) {
        if (confirm("Bạn có muốn thực hiện ?")) {
            $http({
                method: 'POST',
                url: "http://localhost:8081/admin/report/project/" + id + "/update/" + $scope.request.currentDate,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);

            //tạo funtion nếu thành công!
            function successCallback(response) {
                $scope.timeSheetsMessages=response.data;
                alert($scope.timeSheetsMessages.view);
            }

            //tạo funtion kiểm tra nếu thất bại
            function errorCallback(error) {
                //error code
                console.log("Update False !!!");
            }
        }
    };
};

function updateTimeSheetChecked($scope, $window, $http) {
     $scope.updateTimeSheetChecked = function (id, check) {
        if (confirm("Bạn có muốn thực hiện ?")) {
            $http({
                method: 'POST',
                url: "http://localhost:8081/admin/report/project/timeSheet/update/" + id ,
                data: check,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);
            //tạo funtion nếu thành công!
            function successCallback(response) {
                $scope.timeSheetsMessages=response.data;
                alert($scope.timeSheetsMessages.view);
            }
            //tạo funtion kiểm tra nếu thất bại
            function errorCallback(error) {
                //error code
                console.log("UPDATE FALSE !!!");
            }
        }
    };
};

// Danh Sách Các Thành viên Thiếu Báo Cáo Ngày Của Dự Án
function employeeList($scope, $http, $stateParams){
    $scope.request = {
        "currentDate": ""
    };
    var date = new Date();
    $scope.request.currentDate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
    console.log($scope.request.currentDate);
    $http({
        method: 'GET',
        url: "http://localhost:8081/admin/report/project/"+$stateParams.ID+"/employee/"+$scope.request.currentDate,
        headers: {
            'Content-Type': 'application/json'
        }

    }).then(function successCallback(response) {
        console.log(response);
        $scope.employeesList= response.data;
        console.log($scope.employeesList);
    }, function errorCallback(response) {
        console.log(response)
    });
}
//======================================================================================================================

function insertProjectReport($scope, $window, $http) {
    $scope.insertReport = function (id, firstDate, finalDate) {

        $scope.request = {
            "projectId": id,
            "firstPoint": "",
            "finalPoint": ""
        }

        $scope.request.firstPoint = firstDate.getFullYear() + '-' + ('0' + (firstDate.getMonth() + 1)).slice(-2) + '-' + ('0' + firstDate.getDate()).slice(-2);
        $scope.request.finalPoint = finalDate.getFullYear() + '-' + ('0' + (finalDate.getMonth() + 1)).slice(-2) + '-' + ('0' + finalDate.getDate()).slice(-2);

        if (confirm("Bạn có muốn thực hiện ?")) {
            $http({
                method: 'POST',
                url: "http://localhost:8081/admin/report/project/newReport",
                data: $scope.request,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);
            //tạo funtion nếu thành công!
            function successCallback(response) {
                $scope.messages=response.data;
                alert($scope.messages.view);
            }
            //tạo funtion kiểm tra nếu thất bại
            function errorCallback(error) {
                //error code
                console.log("Create FALSE !!!");
            }
        }
    };
};

function allProjectReport($scope, $http, $stateParams){
    $http({
        method: 'GET',
        url: "http://localhost:8081/admin/report/project/"+$stateParams.ID +"/allReport",
        headers: {
            'Content-Type': 'application/json'
        }

    }).then(function successCallback(response) {
        console.log(response);
        $scope.ProjectReports= response.data;
        console.log($scope.ProjectReports);
    }, function errorCallback(response) {
        console.log(response)
    });
}

function projectReportDetail($scope, $http, $stateParams){

    $http({
        method: 'GET',
        url: "http://localhost:8081/admin/report/project/report/"+$stateParams.ID,
        headers: {
            'Content-Type': 'application/json'
        }

    }).then(function successCallback(response) {
        console.log(response);
        $scope.projectReportDetails= response.data;
        console.log($scope.projectReportDetails);
    }, function errorCallback(response) {
        console.log(response)
    });
}

function deleteReport($scope, $window, $http, $state) {
    $scope.deleteReport = function (id) {
        if (confirm("Bạn có muốn xóa ?")) {
            $http({
                method: 'POST',
                url: "http://localhost:8081/admin/report/project/report/delete/" + id,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(successCallback, errorCallback);
            //tạo funtion nếu thành công!
            function successCallback(response) {
                $scope.messages=response.data;
                $state.reload();
                alert($scope.messages.view);
            }
            //tạo funtion kiểm tra nếu thất bại
            function errorCallback(error) {
                //error code
                console.log("Delete FALSE !!!");
            }
        }
    };
};

//======================================================================================================================


function listEmployeeLack($scope, $window, $http, $stateParams) {

    //button setting
    $scope.inputDate = true;
    $scope.cancel = false;
    $scope.show = true;
    $scope.detail = false;

    //Date
    $scope.request = {
        "firstPoint": "",
        "finalPoint": ""
    }


    $scope.listEmployeeLacks = function (id, firstDate, finalDate) {

        $scope.request.firstPoint = firstDate.getFullYear() + '-' + ('0' + (firstDate.getMonth() + 1)).slice(-2) + '-' + ('0' + firstDate.getDate()).slice(-2);
        $scope.request.finalPoint = finalDate.getFullYear() + '-' + ('0' + (finalDate.getMonth() + 1)).slice(-2) + '-' + ('0' + finalDate.getDate()).slice(-2);

        $http({
            method: 'GET',
            url: "http://localhost:8081/admin/report/project/" + $stateParams.ID + "/employee/" + $scope.request.firstPoint + "/" + $scope.request.finalPoint,
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(successCallback, errorCallback);

        //tạo funtion nếu thành công!
        function successCallback(response) {
            $scope.listEmployee = response.data;
            console.log($scope.listEmployee);

        }

        //tạo funtion kiểm tra nếu thất bại
        function errorCallback(error) {
            //error code
            console.log(error);
        }
    };

    $scope.listEmployeeLackDetail = function (employeeId) {

        //button setting
        $scope.inputDate = false;
        $scope.cancel = true;
        $scope.show = false;
        $scope.detail = true;

        $http({
            method: 'GET',
            url: "http://localhost:8081/admin/report/project/" + $stateParams.ID + "/employee/" + employeeId + "/" + $scope.request.firstPoint + "/" + $scope.request.finalPoint,
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(successCallback, errorCallback);

        //tạo funtion nếu thành công!
        function successCallback(response) {
            $scope.listEmployeeLackDetails = response.data;
            console.log($scope.listEmployeeLackDetails);

        }

        //tạo funtion kiểm tra nếu thất bại
        function errorCallback(error) {
            //error code
            console.log(error);
        }
    };

    $scope.cancelView = function(){
        $scope.inputDate = true;
        $scope.cancel = false;
        $scope.show = true;
        $scope.detail = false;
    };

};

