text: ;(function()
  {
      'use strict';
  
      angular.module('app.users', ['app.service.users', 'app.service.companies', 'LocalStorageModule'])
  
          .controller('UsersController', ['$scope', '$filter', '$http', '$modal', '$interval', 'UsersService', 'CompaniesService', 'localStorageService', '$window',function($scope, $filter, $http, $modal, $timeout, UsersService, CompaniesService, localStorageService, $window)  {
  
              var user_data = localStorageService.get('user_data');
              if (user_data.type === 'user' || user_data.type === 'admin') {
                  $window.location.href = './#/404';
              }
  
              // General variables
              $scope.datas = [];
              $scope.currentPageStores = [];
              $scope.searchKeywords = '';
              $scope.filteredData = [];
              $scope.row = '';
              $scope.numPerPageOpts = [5, 10, 25, 50, 100];
              $scope.numPerPage = $scope.numPerPageOpts[1];
              $scope.currentPage = 1;
              $scope.positionModel = 'topRight';
              $scope.toasts = [];
              $scope.companies = [];
              var modal;
  
              // Function for load table
              function loadDataTable() {
                  UsersService.index().then(function(response) {
                      $scope.datas = response.data.records;
                      $scope.search();
                      $scope.select($scope.currentPage);
                  });
              }
  
              function loadCompanies() {
                  CompaniesService.index().then(function(response) {
                      $scope.companies = response.data.records;
                  });
              }
  
              // Functions of table
              $scope.select = function(page) {
                  var start = (page - 1)*$scope.numPerPage,
                      end = start + $scope.numPerPage;
  
                  $scope.currentPageStores = $scope.filteredData.slice(start, end);
              };
  
              $scope.onFilterChange = function() {
                  $scope.select(1);
                  $scope.currentPage = 1;
                  $scope.row = '';
              };
  
              $scope.onNumPerPageChange = function() {
                  $scope.select(1);
                  $scope.currentPage = 1;
              };
  
              $scope.onOrderChange = function() {
                  $scope.select(1);
                  $scope.currentPage = 1;
              };
  
              $scope.search = function() {
                  $scope.filteredData = $filter('filter')($scope.datas, $scope.searchKeywords);
                  $scope.onFilterChange();
              };
  
              $scope.order = function(rowName) {
                  if($scope.row == rowName)
                      return;
                  $scope.row = rowName;
                  $scope.filteredData = $filter('orderBy')($scope.datas, rowName);
                  $scope.onOrderChange();
              };
  
              loadDataTable();
              loadCompanies();
  
              // Function for toast
              function createToast (type, message) {
                  $scope.toasts.push({
                      anim: 'bouncyflip',
                      type: type,
                      msg: message
                  });
              }
  
              function closeAlert (index) {
                  $scope.toasts.splice(index, 1);
              }
  
              // Function for sending data
              $scope.saveData = function (user) {
                  if ($scope.action == 'new') {
                      UsersService.store(user).then(
                          function successCallback(response) {
                              if (response.data.result) {
                                  loadDataTable();
                                  modal.close();
                                  createToast('success', '<strong>Éxito: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              } else {
                                  createToast('danger', '<strong>Error: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              }
                          },
                          function errorCallback(response) {
                              createToast('danger', '<strong>Error: </strong>'+response.data.message);
                              $timeout( function(){ closeAlert(0); }, 3000);
                          }
                      );
                  }
                  else if ($scope.action == 'update') {
                      UsersService.update(user).then(
                          function successCallback(response) {
                              if (response.data.result) {
                                  loadDataTable();
                                  modal.close();
                                  createToast('success', '<strong>Éxito: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              } else {
                                  createToast('danger', '<strong>Error: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              }
                          },
                          function errorCallback(response) {
                              createToast('danger', '<strong>Error: </strong>'+response.data.message);
                              $timeout( function(){ closeAlert(0); }, 3000);
                          }
                      );
                  }
                  else if ($scope.action == 'delete') {
                      UsersService.destroy(user.id).then(
                          function successCallback(response) {
                              if (response.data.result) {
                                  loadDataTable();
                                  modal.close();
                                  createToast('success', '<strong>Éxito: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              } else {
                                  createToast('danger', '<strong>Error: </strong>'+response.data.message);
                                  $timeout( function(){ closeAlert(0); }, 3000);
                              }
                          },
                          function errorCallback(response) {
                              createToast('danger', '<strong>Error: </strong>'+response.data.message);
                              $timeout( function(){ closeAlert(0); }, 3000);
                          }
                      );
                  }
              };
  
              // Functions for modals
              $scope.modalCreateOpen = function() {
                  $scope.user = {};
                  $scope.action = 'new';
  
                  modal = $modal.open({
                      templateUrl: 'views/app/users-modal.html',
                      scope: $scope,
                      size: 'md',
                      resolve: function() {},
                      windowClass: 'default'
                  });
              };
  
              $scope.modalEditOpen = function(data) {
                  $scope.action = 'update';
                  $scope.user = data;
                  if (data.cancellation == 1) {
                      $scope.user.cancellation = true;
                  } else {
                      $scope.user.cancellation = false;
                  }
                  console.log($scope.user.cancellation);
                  modal = $modal.open({
                      templateUrl: 'views/app/users-modal.html',
                      scope: $scope,
                      size: 'md',
                      resolve: function() {},
                      windowClass: 'default'
                  });
              };
  
              $scope.modalDeleteOpen = function(data) {
                  $scope.action = 'delete';
                  $scope.user = data;
  
                  modal = $modal.open({
                      templateUrl: 'views/app/users-modal.html',
                      scope: $scope,
                      size: 'md',
                      resolve: function() {},
                      windowClass: 'default'
                  });
              };
  
              $scope.modalClose = function() {
                  modal.close();
              }
          }])
  }());