"use strict";

appCore.controller("adminAddGoalTemplateCtrl", ["$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.goalList = [{
            title:'',
            description:'',
            isView:false
        }];
    $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.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;
        });
       
    };
    init();

    $scope.getGoalListByTempId = () => {
        $scope.goalList = [{
            title:'',
            description:'',
            time_in_minutes:0,
            isView:false
        }];
        vServices.callApi("goal/getTemplateFormData?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');
            }
        });

    }

    // add new goal row in list
    $scope.addRow = ()=>{        
        $scope.goalList.push({
            title:'',
            description:'',
            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 = ()=>{
        init();
    }

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

    $scope.saveOrUpdateGoals = ()=>{
        let count = 0;
        let isAlert = false;
        //validate form
        $scope.goalList.forEach(item => {
            if(!item.isView && item.title!=''){
                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=>{
            item.template_id = $scope.getTemp;
        });
        vServices.callApi("goal/adminAddGoalTemplate", "POST", {data:JSON.stringify($scope.goalList)}).then(function (res) {
           toastr.success("Your goal have been saved","Success");
           window.location.reload();
        });        
    }

    $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/deleteTempGoal", "POST", {id:$scope.goalToDelete.id}).then(function (res) {
            toastr.success("Goal has been deleted","Success");
            $scope.isConfirmModalOpen = false;
            init();
        });
    }
}]);
