"use strict";

app.controller("ProjectViewCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, filterFilter, $uibModal, $document) {
        $scope.$root.page.title = "Project View";
        $scope.param = $state.params;
        $scope.minTeamEffort = [];
        $scope.allocations = [];
        $scope.project = {"id": 0, "proj_name": "", "proj_description": "", "proj_start_date": "2001-01-01 00:00:00+05:30", "proj_expected_end_date": "2001-01-01 00:00:00+05:30", "proj_status": "active", "proj_resource_requirement_perday": 0, "proj_code": ""};
        $scope.releaseFromProject = (e, c, f, d) => {
            vServices.callApi("allocation/remove/" + e, "PUT", {allocation_release_comment: c, hero_feedback: f, toDate: d}).then((message) => {
                vServices.callApi("allocation/list/" + $state.params.projectid, "GET", {}).then((res) => {
                    $scope.allocations = res ? res : [];
                    toastr.success(message, 'Message');

                });
            });
        };
        $scope.userACL = $scope.$root.gUser.emp_acls;
        $scope.thisAdmin = $scope.userACL.includes(",27,");
        $scope.isAllowToEdit = ($scope.userACL.includes(",72,") || $scope.userACL.includes(",73,") || $scope.userACL.includes(",74,") ) ?true:false;
        if (!localStorage.getItem("id_token")) {
            $state.go("login", {}, {reload: true});
        }
        const init = function () {
            // Code for hard refresh
            if( window.localStorage )
            {
                //check if reloaded once already
                if( !localStorage.getItem('firstLoad') )
                {
                    //if not reloaded once, then set firstload to true
                    localStorage['firstLoad'] = true;
                    //reload the webpage using reload() method
                    window.location.reload();
                }
                else{
                    localStorage.removeItem('firstLoad');
                }
            }
            vServices.callApi("project/" + $state.params.projectid, "GET", {}).then((res) => {
                if (res) {
                    res.proj_start_date = res.proj_start_date ? moment(res.proj_start_date) : '';
                    res.proj_expected_end_date = res.proj_expected_end_date ? moment(res.proj_expected_end_date) : '';
                    $scope.project = res;
                    $scope.$root.callDataTable("#projectView", 1000);


                    //$scope.$root.page.title = "Project View: "+$scope.project.proj_name;
                }
            });
            vServices.callApi("allocation/list/" + $state.params.projectid, "GET", {}).then((res) => {
                if (res) 
                {
                    res.forEach(i=>{
                        if($scope.thisAdmin){
                            i.isAllowToEditLocation = false
                        }
                        if(!$scope.thisAdmin){
                            var isInd = $scope.userACL.includes(",72,") && i.emp_country == 1 ? true : false;
                            var isAus = $scope.userACL.includes(",73,") && i.emp_country == 2 ? true : false;
                            var isUsa = $scope.userACL.includes(",74,") && i.emp_country == 3 ? true : false;
                            var isClearedCountry = (isInd || isAus || isUsa) ? true : false;
                            i.isAllowToEditLocation = !isClearedCountry ? true : false;
                        }
                    })
                    $scope.allocations = res;
                }
            });
            vServices.callApi("project/designation/" + $state.params.projectid, "GET", {}).then((res) => {
                if (res) $scope.minTeamEffort = res;
            });
        };
        init();

        /* Model code start*/
        $scope.setReleaseUser = (obj) => {
            let modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                templateUrl: 'modelDynamic.html',
                controller: 'ModelDynamicCtrl',
                controllerAs: '$scope',
                size: 'sm',//sm md lg
                backdrop: 'static',
                resolve: {
                    data: () => {
                        return {
                            allocation: obj,
                            project: $scope.project
                        };
                    }
                }
            });

            modalInstance.result.then((data) => {
                $scope.releaseFromProject(data.allocation.allocationid, data.allocation.change_comment, data.allocation.hero_feedback, data.allocation.toDate);
            }, () => {
                toastr.success('Action has been canceled', 'Cancel');
            });
        };
        /* Model code end*/

    }
])
    .controller('ModelDynamicCtrl', function ($uibModalInstance, data, $scope) {
        $scope.data = data;
        $scope.ok = () => {
            if ($scope.data.allocation.change_comment.length < 1) {
                toastr.error("Reason of Releasing should not be empty");
            } else if ($scope.data.allocation.hero_feedback.length < 1) {
                toastr.error("Overall Hero\'s Feedback should not be empty");
            } else {
                $uibModalInstance.close($scope.data);
            }
        };
        $scope.cancel = () => $uibModalInstance.dismiss('cancel');
    });
