"use strict";

appCore.controller("GoalDashboardCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.params = $state.params;
    $scope.$root.page.title = "My Heroes";
    $scope.btn = {text: "My Goals"};
    $scope.myHeroes = [];
    $scope.dTable = null;
    $scope.allGoalTemplates = [];
    $scope.selectTemplate = $scope.params.template_id;
    $scope.selectBillingType = 0;
    $scope.consolidatedReport = [];

    const commonCallingTemplateHeroData = (tempId) =>{
        $scope.myHeroes = [];
        if($scope.dTable)$scope.dTable.destroy();
        
        if(tempId){
            vServices.callApi("goal/getMyHeroesList?tempType=" + tempId, "GET", {}).then(function (res) { 
                $scope.myHeroes = JSON.parse(JSON.stringify(res.hero));
                $scope.myHeroes.forEach(item => {
                    let goal = JSON.parse(JSON.stringify(res.goal));
                    goal = goal.filter(i=> i.hero_id==item.eid);
                    //calculate stage counter
                    let draftCounter=0;
                    let progressCounter=0;
                    let doneCounter=0;
                    let goalCompleted=0;
                    let reqHours=0;
                    let completedHours=0;
                    let totalAssignedGoals=0;
                    let expiredGoals=0;
                    goal.forEach(g=>{
                        totalAssignedGoals+=g.stage!='on-hold'?1:0;
                        draftCounter += g.stage=='not-started'?1:0;
                        progressCounter += g.stage=='in-progress'?1:0;
                        doneCounter += g.is_approved?1:0;
                        goalCompleted += g.stage=='completed'?1:0;
                        reqHours += g.required_minutes?g.required_minutes:0;
                        completedHours += (g.required_minutes && g.stage=='completed')?g.required_minutes:0;
                        // doneCounter += g.stage=='completed' && g.is_approved?1:0;
                        let currentDate = moment().format('YYYY-MM-DD');
                        let expireEnd = moment(g.target_date).format('YYYY-MM-DD');
                        let dateOne = new Date(currentDate);
                        let dateTwo = new Date(expireEnd);
                        if((dateTwo < dateOne) && !g.is_completed && (g.stage != 'on-hold') ){
                            expiredGoals+= 1;
                        }else{
                            expiredGoals = 0;
                        }
                    });
                    item.totalAssignedGoals = totalAssignedGoals;
                    item.draftCounter = draftCounter;
                    item.progressCounter = progressCounter;
                    item.doneCounter = doneCounter;
                    item.goalCompleted = goalCompleted;
                    item.reqHours = Math.floor(reqHours / 60)  + '.' + Math.floor(reqHours % 60); 
                    item.completedHours = Math.floor(completedHours / 60)  + '.' + Math.floor(completedHours % 60); 
                    item.expiredGoals = expiredGoals;
    
                });
                $scope.myHeroes.forEach(o=>{
                    o.comment_count = res.comments.filter(k => k.hero_id == o.eid);
                });
                
                $scope.$root.callDataTable(".heroList", 500, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
                
            });
          
        }
      
    };


    const commonCallingTemplateHeroDataForBillingTypePool = (tempId) =>{
        $scope.myHeroes = [];
        if($scope.dTable)$scope.dTable.destroy();
        if(tempId){
            vServices.callApi("goal/getMyHeroesList?tempType=" + tempId, "GET", {}).then(function (res) { 
                $scope.myHeroes = JSON.parse(JSON.stringify(res.hero));
                $scope.myHeroes.forEach(item => {
                    let goal = JSON.parse(JSON.stringify(res.goal));
                    goal = goal.filter(i=> i.hero_id==item.eid);
                    //calculate stage counter
                    let draftCounter=0;
                    let progressCounter=0;
                    let doneCounter=0;
                    let goalCompleted=0;
                    let reqHours=0;
                    let completedHours=0;
                    let totalAssignedGoals=0;
                    let expiredGoals=0;
                    goal.forEach(g=>{
                        totalAssignedGoals+=g.stage!='on-hold'?1:0;
                        draftCounter += g.stage=='not-started'?1:0;
                        progressCounter += g.stage=='in-progress'?1:0;
                        doneCounter += g.is_approved?1:0;
                        goalCompleted += g.stage=='completed'?1:0;
                        reqHours += g.required_minutes?g.required_minutes:0;
                        completedHours += (g.required_minutes && g.stage=='completed')?g.required_minutes:0;
                        // doneCounter += g.stage=='completed' && g.is_approved?1:0;
                        let currentDate = moment().format('YYYY-MM-DD');
                        let expireEnd = moment(g.target_date).format('YYYY-MM-DD');
                        let dateOne = new Date(currentDate);
                        let dateTwo = new Date(expireEnd);
                        if((dateTwo < dateOne) && !g.is_completed && (g.stage != 'on-hold') ){
                            expiredGoals+= 1;
                        }else{
                            expiredGoals = 0;
                        }
                    });
                    item.totalAssignedGoals = totalAssignedGoals;
                    item.draftCounter = draftCounter;
                    item.progressCounter = progressCounter;
                    item.doneCounter = doneCounter;
                    item.goalCompleted = goalCompleted;
                    item.reqHours = Math.floor(reqHours / 60)  + '.' + Math.floor(reqHours % 60); 
                    item.completedHours = Math.floor(completedHours / 60)  + '.' + Math.floor(completedHours % 60); 
                    item.expiredGoals = expiredGoals;
    
                });
                $scope.myHeroes.forEach(o=>{
                    o.comment_count = res.comments.filter(k => k.hero_id == o.eid);
                });
                var newArr=[];
                $scope.myHeroes.forEach(j=>{
                    if(j.emp_revenue_center == '17'){
                        newArr.push(j);
                    }
                });

                $scope.myHeroes = newArr;


                $scope.$root.callDataTable(".heroList", 500, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
                
                
            });
          
        }
      
    };

    const commonCallingTemplateHeroDataForBillingTypePartial = (tempId) =>{
        $scope.myHeroes = [];
        if($scope.dTable)$scope.dTable.destroy();
        if(tempId){
            vServices.callApi("goal/getMyHeroesList?tempType=" + tempId, "GET", {}).then(function (res) { 
                $scope.myHeroes = JSON.parse(JSON.stringify(res.hero));
                $scope.myHeroes.forEach(item => {
                    let goal = JSON.parse(JSON.stringify(res.goal));
                    goal = goal.filter(i=> i.hero_id==item.eid);
                    //calculate stage counter
                    let draftCounter=0;
                    let progressCounter=0;
                    let doneCounter=0;
                    let goalCompleted=0;
                    let reqHours=0;
                    let completedHours=0;
                    let totalAssignedGoals=0;
                    let expiredGoals=0;
                    goal.forEach(g=>{
                        totalAssignedGoals+=g.stage!='on-hold'?1:0;
                        draftCounter += g.stage=='not-started'?1:0;
                        progressCounter += g.stage=='in-progress'?1:0;
                        doneCounter += g.is_approved?1:0;
                        goalCompleted += g.stage=='completed'?1:0;
                        reqHours += g.required_minutes?g.required_minutes:0;
                        completedHours += (g.required_minutes && g.stage=='completed')?g.required_minutes:0;
                        // doneCounter += g.stage=='completed' && g.is_approved?1:0;
                        let currentDate = moment().format('YYYY-MM-DD');
                        let expireEnd = moment(g.target_date).format('YYYY-MM-DD');
                        let dateOne = new Date(currentDate);
                        let dateTwo = new Date(expireEnd);
                        if((dateTwo < dateOne) && !g.is_completed && (g.stage != 'on-hold') ){
                            expiredGoals+= 1;
                        }else{
                            expiredGoals = 0;
                        }
                    });
                    item.totalAssignedGoals = totalAssignedGoals;
                    item.draftCounter = draftCounter;
                    item.progressCounter = progressCounter;
                    item.doneCounter = doneCounter;
                    item.goalCompleted = goalCompleted;
                    item.reqHours = Math.floor(reqHours / 60)  + '.' + Math.floor(reqHours % 60); 
                    item.completedHours = Math.floor(completedHours / 60)  + '.' + Math.floor(completedHours % 60); 
                    item.expiredGoals = expiredGoals;
    
                });
                $scope.myHeroes.forEach(o=>{
                    o.comment_count = res.comments.filter(k => k.hero_id == o.eid);
                });
                vServices.callApi("goal/partialBillingHeroes", "GET", {}).then(function (res) { 
                    var newArr = [];
                    res.forEach(i=>{
                        $scope.myHeroes.forEach(j=>{
                            if(i.id == j.eid){
                                newArr.push(j);
                            }
                        });
                    });
                    $scope.myHeroes = newArr;
                });


                $scope.$root.callDataTable(".heroList", 500, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
                
                
            });
          
        }
      
    };

    const commonCallingTemplateHeroDataForBillingTypeNOBilling = (tempId) =>{
        $scope.myHeroes = [];
        if($scope.dTable)$scope.dTable.destroy();
        if(tempId){
            vServices.callApi("goal/getMyHeroesList?tempType=" + tempId, "GET", {}).then(function (res) { 
                $scope.myHeroes = JSON.parse(JSON.stringify(res.hero));
                $scope.myHeroes.forEach(item => {
                    let goal = JSON.parse(JSON.stringify(res.goal));
                    goal = goal.filter(i=> i.hero_id==item.eid);
                    //calculate stage counter
                    let draftCounter=0;
                    let progressCounter=0;
                    let doneCounter=0;
                    let goalCompleted=0;
                    let reqHours=0;
                    let completedHours=0;
                    let totalAssignedGoals=0;
                    let expiredGoals=0;
                    goal.forEach(g=>{
                        totalAssignedGoals+=g.stage!='on-hold'?1:0;
                        draftCounter += g.stage=='not-started'?1:0;
                        progressCounter += g.stage=='in-progress'?1:0;
                        doneCounter += g.is_approved?1:0;
                        goalCompleted += g.stage=='completed'?1:0;
                        reqHours += g.required_minutes?g.required_minutes:0;
                        completedHours += (g.required_minutes && g.stage=='completed')?g.required_minutes:0;
                        // doneCounter += g.stage=='completed' && g.is_approved?1:0;
                        let currentDate = moment().format('YYYY-MM-DD');
                        let expireEnd = moment(g.target_date).format('YYYY-MM-DD');
                        let dateOne = new Date(currentDate);
                        let dateTwo = new Date(expireEnd);
                        if((dateTwo < dateOne) && !g.is_completed && (g.stage != 'on-hold') ){
                            expiredGoals+= 1;
                        }else{
                            expiredGoals = 0;
                        }
                    });
                    item.totalAssignedGoals = totalAssignedGoals;
                    item.draftCounter = draftCounter;
                    item.progressCounter = progressCounter;
                    item.doneCounter = doneCounter;
                    item.goalCompleted = goalCompleted;
                    item.reqHours = Math.floor(reqHours / 60)  + '.' + Math.floor(reqHours % 60); 
                    item.completedHours = Math.floor(completedHours / 60)  + '.' + Math.floor(completedHours % 60); 
                    item.expiredGoals = expiredGoals;
    
                });
                $scope.myHeroes.forEach(o=>{
                    o.comment_count = res.comments.filter(k => k.hero_id == o.eid);
                });
                vServices.callApi("goal/noBillingHeroes", "GET", {}).then(function (res) { 
                    var newArr = [];
                    res.forEach(i=>{
                        $scope.myHeroes .forEach(j=>{
                            if(i.id == j.eid){
                                newArr.push(j);
                            }
                        });
                    });
                    $scope.myHeroes = newArr;
                });


                $scope.$root.callDataTable(".heroList", 500, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
                
                
            });
          
        }
      
    };

    const commonCallingTemplateHeroDataForBillingTypeNewJoinee = (tempId) =>{
        $scope.myHeroes = [];
        if($scope.dTable)$scope.dTable.destroy();
        if(tempId){
            vServices.callApi("goal/getMyHeroesList?tempType=" + tempId, "GET", {}).then(function (res) { 
                $scope.myHeroes = JSON.parse(JSON.stringify(res.hero));
                $scope.myHeroes.forEach(item => {
                    let goal = JSON.parse(JSON.stringify(res.goal));
                    goal = goal.filter(i=> i.hero_id==item.eid);
                    //calculate stage counter
                    let draftCounter=0;
                    let progressCounter=0;
                    let doneCounter=0;
                    let goalCompleted=0;
                    let reqHours=0;
                    let completedHours=0;
                    let totalAssignedGoals=0;
                    let expiredGoals=0;
                    goal.forEach(g=>{
                        totalAssignedGoals+=g.stage!='on-hold'?1:0;
                        draftCounter += g.stage=='not-started'?1:0;
                        progressCounter += g.stage=='in-progress'?1:0;
                        doneCounter += g.is_approved?1:0;
                        goalCompleted += g.stage=='completed'?1:0;
                        reqHours += g.required_minutes?g.required_minutes:0;
                        completedHours += (g.required_minutes && g.stage=='completed')?g.required_minutes:0;
                        // doneCounter += g.stage=='completed' && g.is_approved?1:0;
                        let currentDate = moment().format('YYYY-MM-DD');
                        let expireEnd = moment(g.target_date).format('YYYY-MM-DD');
                        let dateOne = new Date(currentDate);
                        let dateTwo = new Date(expireEnd);
                        if((dateTwo < dateOne) && !g.is_completed && (g.stage != 'on-hold') ){
                            expiredGoals+= 1;
                        }else{
                            expiredGoals = 0;
                        }
                    });
                    item.totalAssignedGoals = totalAssignedGoals;
                    item.draftCounter = draftCounter;
                    item.progressCounter = progressCounter;
                    item.doneCounter = doneCounter;
                    item.goalCompleted = goalCompleted;
                    item.reqHours = Math.floor(reqHours / 60)  + '.' + Math.floor(reqHours % 60); 
                    item.completedHours = Math.floor(completedHours / 60)  + '.' + Math.floor(completedHours % 60); 
                    item.expiredGoals = expiredGoals;
    
                });
                $scope.myHeroes.forEach(o=>{
                    o.comment_count = res.comments.filter(k => k.hero_id == o.eid);
                });
                vServices.callApi("goal/getNewJoinee", "GET", {}).then(function (res) { 
                    var newArr = [];
                    res.forEach(i=>{
                        $scope.myHeroes .forEach(j=>{
                            if(i.id == j.eid){
                                newArr.push(j);
                            }
                        });
                    });
                    $scope.myHeroes = newArr;
                });


                $scope.$root.callDataTable(".heroList", 500, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
                
                
            });
          
        }
      
    };



    $scope.changeTemplate = (selectTemplate) => {
        commonCallingTemplateHeroData(selectTemplate);
    };

    $scope.billingType = (selectBillingType) => {
        // 1 = pool, 2 = No-Billing, 3 = Partial Billing
        if($scope.selectTemplate==0){
            if(selectBillingType == 1){
                commonCallingTemplateHeroDataForBillingTypePool($scope.selectTemplate);
            }
            if(selectBillingType == 2){
                commonCallingTemplateHeroDataForBillingTypePartial($scope.selectTemplate);
            }
            if(selectBillingType == 3){
                commonCallingTemplateHeroDataForBillingTypeNOBilling($scope.selectTemplate);
            }
            if(selectBillingType == 4){
                commonCallingTemplateHeroDataForBillingTypeNewJoinee($scope.selectTemplate);
            }
        }
    };

    $scope.deleteHeroGoals = (heroId,templateId) =>{
        if (confirm("Do you really want to save and update the data !")) {
            vServices.callApi("goal/deleteHeroGoals?hero_id="+heroId + "&template_id="+templateId , "GET", {}).then(function (res) { 
                if(res){
                window.location.reload();
                }
            });
        }
    }

    const init = () => {   
        $scope.myHeroes = [];
        vServices.callApi("goal/template_master", "GET", {}).then(function (res) { 
            if(res){
                $scope.allGoalTemplates = res;
            }
        });
        commonCallingTemplateHeroData($scope.selectTemplate);

        vServices.callApi("goal/getGoalsReportConsolidated", "GET", {}).then(function (res) { 
            if(res){
                $scope.consolidatedReport = res;
                $scope.$root.callDataTable(".heroList2", 500);
            }
        });
        

     
    };

    init();





}]);
