(function () {
    'use strict';

    angular
        .module('app')
        .controller('RegisterController', RegisterController);

    RegisterController.$inject = ['UserService', '$location', '$rootScope', 'FlashService', '$http'];
    function RegisterController(UserService, $location, $rootScope, FlashService, $http) {
        var vm = this;

        $rootScope.data = [] 
        $http({method : 'GET',url :"http://localhost:8080/GECORegistrationSpring/communities"})
        .then(successCallback, errorCallback);
        
        function successCallback(response){
            $rootScope.data = response.data;
        }
        
        function errorCallback(error){
            alert("Error");
        }    
        
        vm.register = register;

        function register() {
            vm.dataLoading = true;
            UserService.Create(vm.user)
                .then(function (response) {
                    if (response.success) {
                        FlashService.Success('Registration successful', true);
                        $location.path('/login');
                    } else {
                        FlashService.Error(response.message + 'perche errore?');
                        vm.dataLoading = false;
                    }
                });
        }
    }



})();
