"use strict";
appCore.controller("MyCertificateReportHeroViewCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "My Certificate Report";
    $scope.params = $state.params;
    $scope.employee = [];
    $scope.revenueCenterList = [];
    $scope.dTable = null;
    $scope.mycertificate = [];
    $scope.selectRevenue = '0';
    $scope.filter = {
        fromDate: moment().subtract(2, "days").format("DD-MM-YYYY"),
        toDate: moment().format("DD-MM-YYYY"),
        employee: $scope.params.hero_id != 0 ? $scope.params.hero_id : 0,
        pageName: "My Certificate Report",
        selType: "Default",
        type: [{val: "View Type"}, {val: "Default"}, {val: "Project",}]
    };

    $scope.$watch("filter.selType", (newValue, oldValue) => {
        $scope.filter.pageName = "My Certificate Report ";
        // $scope.$root.page.title = "My Certificate Report of " + newValue + " View " + $scope.filter.fromDate + " - " + $scope.filter.toDate;
    });
    let numOr0 = n => isNaN(n) ? 0 : n;

    //$scope.totalHoursNew = (a, k, d) => d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b)));
    $scope.totalHoursNew = (a, k, d) => d && d.length > 0 ? d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b))) : null;
    $scope.totalHours = (a, k, d) => d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b)));
    $scope.condition = (i, d, data, c) => i === 0 ? true : (i > 0 && parseInt(d[c]) !== parseInt($scope.mycertificate[data][i - 1][c]));

    const convertDataForProject = res => {
        if (res) {
            // console.log(res);
            $scope.employee = [];
            $scope.employee.push({id: 0, name: " Select Employee", emp_status: 'active'});
            res = res.filter(emp => emp.emp_status === 'active');
            res.map((o) => $scope.employee.push({id: o.employeeid, name: o.emp_first_name + (o.emp_middle_name ? ' ' + o.emp_middle_name + ' ' : ' ') + o.emp_last_name + ' (' + o.emp_code + ')', emp_status: o.emp_status}));
            $scope.employee = $scope.$root.sort($scope.employee, 'name');
            $scope.$root.callDataTable(".mycertificateDataTableView", 1000)
        }
    };

    const convertDataForMycertificate = res => {

        // console.log(res);
        try {
            $scope.myCertificate = [];
            if ($scope.dTable) {
                $scope.dTable.destroy();
                $scope.dTable = null;
            }
            if (res && res.length > 0) {
                $scope.myCertificate = res;
                $scope.$root.callDataTable(".mycertificateDataTableView", 1000, (t) => {
                    $scope.dTable = t;
                }, 
                {orientation: "landscape"});
            }
        } catch (e) {
            console.error(e);
        }
    };
    
    
    /*On CLick Get all Certified Employee MOdified by suresh yadav------------------------------*/
     $scope.dateChanged = (p) => {
            vServices.callApi("employee/allMyCertificateReport?employee=" + p, "GET", {}).then(res => convertDataForMycertificate(res));
     }
    
    const init = () => {
        $scope.dateChanged($scope.filter.employee);
        vServices.callApi("employee/limited", "GET", {}).then(res => convertDataForProject(res));
        
        /*Get all Revenue center onload page by suresh yadav------------------------------*/
        vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                $scope.revenueCenterList = res;
            }
        }); 
        /*Get all Revenue center onload page by suresh yadav------------------------------*/
    };
    init();

     /*Get all Certified Employee In Drapdown & List Created By Suresh Yadav----------------------------------*/
     $scope.selRevenue = function(val){        
        vServices.callApi("employee/allEmployeeByRcId?rc_id="+ val , "GET", {}).then((res) => {
            $scope.employee = [];
            res.map((o) => $scope.employee.push({id: o.id, name: o.emp_first_name + (o.emp_middle_name ? ' ' + o.emp_middle_name + ' ' : ' ') + o.emp_last_name + ' (' + o.emp_code + ')'}));
            $scope.employee = $scope.$root.sort($scope.employee, 'name');
        });

        vServices.callApi("employee/allMyCertificateReport?rc_id="+val, "GET", {}).then((res) => {
            convertDataForMycertificate(res);
        });
    } 
    /*----------------------------------*/ 

}]);
