"use strict";

appCore.controller("HeroGoalAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.params = $state.params;
    $scope.$root.page.title = "My Hero Goals";
    $scope.btn = {text: "My Goals"};
    $scope.today = moment().format("DD-MM-YYYY");
    $scope.goalsTemplate = [];
    $scope.getTemp = '';
	$scope.designations = [];
	$scope.designation_name = 0;
    $scope.department_name = 0;
    $scope.goalList = [{
            hero_id:$scope.params.hero_id,
            lm_id:$scope.$root.gUser.id,
            goal_name:'',
            target_date: $scope.today,
            stage:'not-started',
            description:'',
            is_approved:false,
            emp_first_name:$scope.$root.gUser.emp_first_name,
            emp_last_name:$scope.$root.gUser.emp_last_name,
            emp_code:$scope.$root.gUser.emp_code,
            created_date:$scope.today,
            isView:false,
            required_minutes : 0
        }];
    $scope.stageOptions = [{label:'Not Started',value:'not-started'},{label:'In-Progress',value:'in-progress'}];
    $scope.statusOptions=[{label:'No',value:false},{label:'Yes',value:true}];
    $scope.isEdit = false;
    $scope.heroObj = {};
    $scope.heroCount = '';
    $scope.department = [];
    $scope.isModalOpen = false;
    $scope.goalCommentList = [];
    $scope.goalComment = {comment:'',comment_by:$scope.$root.gUser.id};
    $scope.isConfirmModalOpen = false;
    $scope.goalToDelete={};
    const init = () => {
        // Get Goals Templates
        vServices.callApi("goal/getGoalTemplates", "GET", {}).then(function (res) {
            $scope.goalsTemplate = res;
        });
		
		vServices.callApi("goal/getDesignations", "GET", $scope.designations).then(function (res) {
            if (res && res.length > 0) {
                $scope.designations = res;
            }
        });

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

    $scope.getGoalListByTempId = () => {

        vServices.callApi("goal/getTemplateFormData?template_id="+$scope.getTemp, "GET", {}).then(function (res) {
            // console.log(res);
            $scope.goalList = res;
            $scope.goalList .forEach(item=>{
               // item.hero_id=$scope.params.hero_id;
                item.lm_id=$scope.$root.gUser.id;
                item.goal_name=item.title;
                item.target_date= $scope.today;
                item.stage='not-started';
                item.description= item.description;
                item.is_approved=false;
                item.emp_first_name=$scope.$root.gUser.emp_first_name;
                item.emp_last_name=$scope.$root.gUser.emp_last_name;
                item.emp_code=$scope.$root.gUser.emp_code;
                item.created_date=$scope.today;
                item.isView=false;
                item.required_minutes = item.time_in_minutes;
                delete item.id;
                delete item.title;
                delete item.created_at;
                delete item.status;
            });
			
			console.log($scope.goalList);
            // if (res && res.length > 0) {
            //     $scope.goalList = JSON.parse(JSON.stringify(res));
            //     $scope.goalList.forEach(item=>{
            //         item.isView = true;                    
            //     })
            //     $scope.isEdit = false;
            // }
            // else{
            //     $scope.isEdit = true;
            //     // console.log('hhh');
            // }
        });


        // $scope.goalList = [{
        //     hero_id:$scope.params.hero_id,
        //     lm_id:$scope.$root.gUser.id,
        //     goal_name:'',
        //     target_date: $scope.today,
        //     stage:'draft',
        //     description:'',
        //     is_approved:false,
        //     emp_first_name:$scope.$root.gUser.emp_first_name,
        //     emp_last_name:$scope.$root.gUser.emp_last_name,
        //     emp_code:$scope.$root.gUser.emp_code,
        //     created_date:$scope.today,
        //     isView:false,
        // }];
        vServices.callApi("goal/getGoalList?template_id="+$scope.getTemp, "GET", {}).then(function (res) {
            // console.log(res);
            if (res && res.length > 0) {
                $scope.goalList = JSON.parse(JSON.stringify(res));
                $scope.goalList.forEach(item=>{
                    item.isView = true;                    
                })
                $scope.isEdit = false;
            }
            else{
                $scope.isEdit = true;
                // console.log('hhh');
            }
        });


    }

  /*   const getHero = ()=>{
        vServices.callApi("goal/getHeroById?hero_id="+$scope.params.hero_id, "GET", {}).then(function (res) {
            $scope.heroObj = JSON.parse(JSON.stringify(res));            
        });
    } */
    //getHero();

    // add new goal row in list
    $scope.addRow = ()=>{        
        $scope.goalList.push({
           // hero_id:$scope.params.hero_id,
            lm_id:$scope.$root.gUser.id,
            goal_name:'',
            target_date:$scope.today,
            stage:'not-started',
            description:'',
            is_approved:false,
          //  emp_first_name:$scope.$root.gUser.emp_first_name,
            //emp_last_name:$scope.$root.gUser.emp_last_name,
           // emp_code:$scope.$root.gUser.emp_code,
            created_date:$scope.today,
            is_completed:false,
            isView:false,
        });
        $scope.isEdit = true;
        // console.log('Add row')
    };

    //remove row
    $scope.removeRow = (index)=>{
        
        $scope.goalToDelete = $scope.goalList[index];
        if($scope.goalToDelete.id){
            $scope.isConfirmModalOpen = true;
        }
        else{
            $scope.goalList.splice(index,1);  
        }
    };
    $scope.cancel = ()=>{
        window.history.back();
    }

    $scope.editRow = (index) =>{
        $scope.goalList[index].isView = false;
        $scope.isEdit = true;
    }

    $scope.saveOrUpdateGoalsDesignation = ()=>{
        let count = 0;
        let isAlert = false;
        //validate form
		console.log($scope.goalList);
        $scope.goalList.forEach(item => {
            if(!item.isView && item.goal_name!=''){
                count = 1;
            }
        });

        //validation message
        if(count==0){
            toastr.error("No goal update, fill all required field and submit","Warning");
            return false;
        }
        else if(isAlert){
            toastr.error("Goal stage should be completed, if you want to approve goal.","Warning");
            return false;
        }

        $scope.goalList.forEach(item=>{
            if($scope.getTemp == ''){
                item.template_id = 0;
            }else{
                item.template_id = $scope.getTemp;
            }
            
        });
        if($scope.designation_name != 0){
            vServices.callApi("goal/saveGoalByDesignation", "POST", {data:JSON.stringify($scope.goalList), data2:JSON.stringify($scope.designation_name)}).then(function (res) {
                toastr.success("Your goal have been saved","Success");
               // window.history.back();
             });     
        }
        if($scope.department_name != 0){
            vServices.callApi("goal/saveGoalByDepartment", "POST", {data:JSON.stringify($scope.goalList), data2:JSON.stringify($scope.department_name)}).then(function (res) {
                toastr.success("Your goal have been saved","Success");
               // window.history.back();
             });     
        }
   
    }

    $scope.opemModal = (index)=>{
        $scope.goalComment.goal_id = $scope.goalList[index].id;        
        vServices.callApi("goal/getGoalComments?goal_id="+$scope.goalComment.goal_id, "GET", {}).then(function (res) {
            $scope.goalCommentList = JSON.parse(JSON.stringify(res));
            $scope.isModalOpen = true;
        });
    }
    $scope.closeModal = ()=>{
        $scope.isModalOpen = false;
    }
    $scope.submitComment = ()=>{
        if($scope.goalComment.comment==''){
            toastr.error("Please fill all required fields.","Error");
            return false;
        }

        vServices.callApi("goal/saveGoalComments", "POST", $scope.goalComment).then(function (res) {
            toastr.success("Your comment has been saved","Success");
            $scope.isModalOpen = false;
        });
    }

    $scope.noCloseGolaDeleteModal = ()=>{
        $scope.goalToDelete = {};
        $scope.isConfirmModalOpen = false;
    }

    $scope.yesDeleteGoal = ()=>{
        
        vServices.callApi("goal/deleteGoal", "POST", {id:$scope.goalToDelete.id}).then(function (res) {
            toastr.success("Goal has been deleted","Success");
            $scope.isConfirmModalOpen = false;
            init();
        });
    }

    $scope.getHeroCount = ()=>{
        if($scope.designation_name != 0){
            vServices.callApi("goal/getHeroesCount?des_id=" + $scope.designation_name , "GET", {}).then(function (res) {
                if(res){
                    $scope.heroCount = res + ' heroes.';
                }else{
                    $scope.heroCount = 0 + ' heroes.';
                }
                
            });
        }
        if($scope.department_name != 0){
            vServices.callApi("goal/getHeroesCountForDept?des_id=" + $scope.department_name , "GET", {}).then(function (res) {
                if(res){
                    $scope.heroCount = res + ' heroes.';
                }else{
                    $scope.heroCount = 0 + ' heroes.';
                }
                
            });
        }

    }
	
	
}]);
