/*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_mail_list', [
  'ui.router',
  'plusOne',
  'ngBoilerplate.account',
  'ngBoilerplate.users',
  'ngBoilerplate.home',
  'ngBoilerplate.admin',
  'ngBoilerplate.admin_modify_db',
  'ngBoilerplate.admin_suggestions',
  'ngBoilerplate.admin_users',
  'ngBoilerplate.emailDomain',
  '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_mail_list', {
    url: '/admin_mail_list',
    views: {
       "header": {
        controller: 'Admin_mail_list_Ctrl',
        templateUrl: 'header/header.tpl.html'
      },
      "admin_header": {
        controller: 'Admin_mail_list_Ctrl',
        templateUrl: 'admin_header/admin_header.tpl.html'
      },
      "main": {
        controller: 'Admin_mail_list_Ctrl',
        templateUrl: 'admin_mail_list/admin_mail_list.tpl.html'
      },
      "footer": {
        controller: 'Admin_mail_list_Ctrl',
        templateUrl: 'footer/footer.tpl.html'
      }
    },
    data:{ pageTitle: 'Dominios de mail' },
    resolve:{
        emailDomains: function(emailDomainService) {
            console.log("emailDomains");
            return emailDomainService.getAllEmailDomains();
        }
    }
  });
})

/**
 * And of course we define a controller for our route.
 */
.controller( 'Admin_mail_list_Ctrl', function Admin_mail_list_Ctrl( $scope, sessionService, $state, emailDomains, emailDomainService, $http, md5) {
    $scope.emailDomains = emailDomains;
    $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.loadEmailExcel = function($fileContent){
        console.log("Admin_mail_list_Ctrl - uploadExcel");
        emailDomainService.uploadExcel($fileContent, function() {
                alert("Excel de dominios de mail cargado correctamente");
                window.location.reload();
        },
        function() {
            alert("Error en la carga del excel de dominios de mail");
        });
    };
    
    $scope.deleteEmailDomains = function(data) {
        console.log("deleteEmailDomains");
        return $http.post("/basic-web-app/rest/emailDomain/clearTable", "name=" + localStorage.getItem("name") + "&password=" +md5.createHash($scope.deleteEmailDomainsPassword || ''), {
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }
        ).then(function(data){
            alert('Borrado de emails completado');
            window.location.reload();
        }, function(data){
            alert("Contraseņa incorrecta");
        });
    };
})


.directive('on-read-file', function ($parse) {
    return {
        restrict: 'A',
        scope: false,
        link: function(scope, element, attrs) {
            var fn = $parse(attrs.onReadFile);
            element.on('change', function(onChangeEvent) {
                var reader = new FileReader();
                reader.onload = function(onLoadEvent) {
                    scope.$apply(function() {
                        fn(scope, {$fileContent:onLoadEvent.target.result});
                    });
                };
                reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
            });
        }
    };
})



;

