"use strict";
appCore.controller("MyDiaryLocationReportCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "My Diary Location Report";
    $scope.allCountries = [];
    $scope.filter = {
        fromDate: moment().subtract(2, "days").format("DD-MM-YYYY"),
        toDate: moment().format("DD-MM-YYYY"),
        location: '0',
        pageName: "Heroes Diary Report (Location)",
        selType: "Default",
        type: [ {val: "Default"}, {val: "Project",}, {val: "Hero",}]
    };
    $scope.empACL = $scope.$root.gUser.emp_acls;
    $scope.locationWiseDiaryReport = [];
    $scope.locationWiseDiaryReportProjectView = [];
    $scope.locationWiseDiaryReportHeroView = [];
    $scope.viewType = "Default";
    $scope.dateChanged = () => {
        var fromDate = $scope.filter.fromDate;
        var toDate = $scope.filter.toDate;
        var location = $scope.filter.location;
        if(location=='0'){
            toastr.error("Please select country.");
        }else{
            vServices.callApi("mydiary/locationWiseMyDiaryReport?toDate="+toDate+'&fromDate='+fromDate+'&location='+location, "GET", {}).then(res => 
                {
                    $scope.locationWiseDiaryReport = res;
                   
                      console.log($scope.locationWiseDiaryReportProjectView);
                    $scope.$root.callDataTable(".myDiaryDataTableView",1000);
                });
        }
    }

    const init = () => {
        vServices.callApi("commonfunc/getCountriesList", "GET", {}).then(function (res) {
            if (res && res.length > 0) 
            $scope.allCountries = res;
          });
    };

    init();

    $scope.selType = (val) =>{
        $scope.temArr = $scope.locationWiseDiaryReport;
        $scope.filter.selType=val;
        if(val=='Default'){
            $scope.locationWiseDiaryReport = $scope.temArr;
        }
        if(val=='Project'){
            $scope.locationWiseDiaryReportProjectView = $scope.temArr.reduce((group, product) => {
                const { proj_name } = product;
                group[proj_name] = group[proj_name] ?? [];
                group[proj_name].push(product);
                return group;
              }, {});
        }
        if(val=='Hero'){
            $scope.locationWiseDiaryReportHeroView = $scope.temArr.reduce((group, product) => {
                const { emp_id } = product;
                group[emp_id] = group[emp_id] ?? [];
                group[emp_id].push(product);
                return group;
              }, {});
            
        }

        $scope.$root.callDataTable(".myDiaryDataTableView",1000);
    }

}]);
