"use strict";
appCore.controller("AnniversaryReport", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "Heroes Birthday Report";
    $scope.params = $state.params;    
    $scope.reportList = [];
    $scope.month = '0';
    $scope.dTable = null;
    $scope.projectsDatatable = [];

    const init = () => {
        if($scope.dTable)$scope.dTable.destroy();
        vServices.callApi("commonfunc/workAnniversaryReports?month="+ $scope.month, "GET", {}).then((res) => {
            if(res && res.length > 0){
                res.forEach(i=>{
                    i.years = $scope.calculateYearsBetween(i.emp_doj);
                });
                $scope.reportList = res;
            }
            $scope.$root.callDataTable(".dataList123", 500, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        });
    };
    init();
    
    /*Get all Certified Employee In Drapdown & List Created By Suresh Yadav----------------------------------*/
     $scope.selectAnniversary = function(val){
        if($scope.dTable)$scope.dTable.destroy();
        $scope.reportList = [];
        vServices.callApi("commonfunc/workAnniversaryReports?month="+ val , "GET", {}).then((res) => {
            if(res && res.length > 0){
                res.forEach(i=>{
                    i.years = $scope.calculateYearsBetween(i.emp_doj);
                });
                $scope.reportList = res;
            }
            $scope.$root.callDataTable(".dataList123", 500, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});

        });
    }     

    $scope.calculateYearsBetween = function(doj) {
        const startDate = new Date(doj);
        const currentDate = new Date();
        // Calculate the difference in months
        const months = (currentDate.getFullYear() - startDate.getFullYear()) * 12  + (currentDate.getMonth() - startDate.getMonth());
        // Calculate the number of years and months
        const years = Math.floor(months / 12);
        const remainingMonths = months % 12;
        const yearsDiff = years + '.' + remainingMonths;
        // console.log(`Total experience: ${years} years and ${remainingMonths} months`);
       return yearsDiff; 
            }
}]);
