/*jshint -W100*/
/**
 * Each section of the site has its own module. It probably also has
 * submodules, though this boilerplate is too simple to demonstrate it. Within
 * `src/app/home`, however, could exist several additional folders representing
 * additional modules that would then be listed as dependencies of this one.
 * For example, a `note` section could have the submodules `note.create`,
 * `note.delete`, `note.edit`, etc.
 *
 * Regardless, so long as dependencies are managed correctly, the build process
 * will automatically take take of the rest.
 *
 * The dependencies block here is also where component dependencies should be
 * specified, as shown below.
 */
angular.module( 'ngBoilerplate.admin_suggestions', [
  'ui.router',
  'plusOne',
  'ngBoilerplate.account',
  'ngBoilerplate.users',
  'ngBoilerplate.home',
  'ngBoilerplate.admin',
  'ngBoilerplate.admin_mail_list',
  'ngBoilerplate.admin_modify_db',
  'ngBoilerplate.admin_users',
  'ngBoilerplate.message',
  'angular-md5'
])

/**
 * Each section or module of the site can also have its own routes. AngularJS
 * will handle ensuring they are all available at run-time, but splitting it
 * this way makes each module more "self-contained".
 */
.config(function config( $stateProvider ) {
  $stateProvider.state( 'admin_suggestions', {
    url: '/admin_suggestions',
    views: {
       "header": {
        controller: 'AdminCtrl',
        templateUrl: 'header/header.tpl.html'
      },
      "admin_header": {
        controller: 'AdminCtrl',
        templateUrl: 'admin_header/admin_header.tpl.html'
      },
      "main": {
        controller: 'Admin_suggestions_Ctrl',
        templateUrl: 'admin_suggestions/admin_suggestions.tpl.html'
      },
      "footer": {
        controller: 'AdminCtrl',
        templateUrl: 'footer/footer.tpl.html'
      }
    },
    data:{ pageTitle: 'Sugerencias' },
    resolve:{
        messages: function(messageService) {
            console.log("messages");
            return messageService.getAllMessages();
        }
    }
  });
})

/**
 * And of course we define a controller for our route.
 */
.controller( 'Admin_suggestions_Ctrl', function Admin_suggestions_Controller( $scope, sessionService, $state, messages, $http, md5) {
    console.log("Admin_suggestions_Ctrl");
    $scope.messages = messages;
    $scope.isLoggedIn = sessionService.isLoggedIn;
    $scope.isAdmin = sessionService.isAdmin;
    $scope.isUser = sessionService.isUser;
    $scope.isTempUser = sessionService.isTempUser;
    $scope.logout = function() {
        sessionService.logout();
        $state.go("home");
    };
    
    $scope.deleteSuggestions = function(data) {
        console.log("deleteSuggestions");
        return $http.post("/basic-web-app/rest/message/clearTable", "name=" + localStorage.getItem("name") + "&password=" +md5.createHash($scope.deleteSuggestionsPassword || ''), {
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }
        ).then(function(data){
            alert('Borrado de sugerencias completado');
            window.location.reload();
        }, function(data){
            alert("Contraseņa incorrecta");
        });
    };
});

