package Dojo::Model::Users;


use strict;
use warnings;
use DBI;
use Mojo::Util 'secure_compare';
use Dojo::Conf;


sub new { bless {}, shift }

  sub grulla_pass{ 
   return 
    _read( "select pass as gpass from usuario where nombre like 'grullas'")->[0]
  };
  sub ugrulla_pass{ 
    my($c,$p)=@_;
    _write("update usuario set pass= ? where nombre like 'grullas'",$p);
  }

sub check {
  my ($self, $user, $pass) = @_;
  my $q="select permiso_id as pid from usuario where nombre = ? and pass=? ";
# |  0 | bloqueado |
# |  1 | invitado  |
# |  2 | usuario   |
# |  3 | pago      |
# |  4 | master    |
  my $r= _read($q,$user,$pass)->[0];
  return $r->{pid} // 0;
}

sub _read{
  my ($q,@bind)=@_;
  my (@empty,$arr);
  my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UR,Dojo::Conf::GRULLADB_URP,{mysql_enable_utf8 => 1});
  return \@empty unless($dbh);
  #$dbh->do(qq{SET NAMES 'utf8'}); 
  my $h=$dbh->selectall_arrayref($q,{ Slice => {} },@bind)//"error";
  #((col1=>d1,col2=>d1),(col1=>d2,col2=>d2))
  $dbh->disconnect();
  return $h; 
}


sub _write{
  my ($q,@bind)=@_;
  my (@empty);
  my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UW,Dojo::Conf::GRULLADB_UWP,{mysql_enable_utf8 => 1});
  return 0 unless($dbh);
   my $h=$dbh->do($q,{ Slice => {} },@bind)//"error";
  $dbh->disconnect();
  # log("db write:". $h );
  return $h;
}


1;
