package Dojo::Controller::Users;
use Mojo::Base 'Mojolicious::Controller';
use Dojo::Support qw{ merge_hash load_module };


sub login {
  my $self = shift;
  #falta si el usuario ya está registrado y llama a login.
  if (($self->session('ureq')//0) == 4){
    $self->stash( css=>["users/login/cssaForm.css"]);
    $self->stash(template=>'users/logina') ;
    $self->stash(layout=>"default");
  }
  elsif (($self->session('ureq')//0) == 2){
    $self->stash( css=>["users/login/cssuForm.css"]);
    $self->stash(template=>'users/loginu') ;
    $self->stash(layout=>"defaultContact");
  }
  else{
    $self->stash( css=>["users/login/cssForm.css"]);
    $self->stash(layout=>"defaultContact");
  }
  my $user = $self->param('uname') // '';
  my $pass = $self->param('pass') //'';
  return $self->render unless my $pmid = $self->dbg->check($user, $pass);
  $self->session(user => $user, pmid=>$pmid, nick=> $self->param('nick'));
  $self->redirect_to($self->session('prev')) ;
}

sub is_logged {
  my $self = shift;
  return 1 if $self->session('user');
  $self->session(ureq=>1,prev=>$self->url_for);
  $self->redirect_to('login');
}
sub is_admin {
  my $self = shift;
    return 1 if ( ($self->session('pmid') //0) == 4);
  $self->session(ureq=>4,prev=>$self->url_for);
  $self->redirect_to('login');
}
sub is_grulla {
  my $self = shift;
    return 1 if (($self->session('pmid')//0)>=2);
    $self->session(ureq=>2,prev=>$self->url_for);
    $self->redirect_to('login');
}

sub logout {
  my $self = shift;
  $self->session(expires => 1);
  $self->redirect_to('home');
}

1;
