#!/usr/bin/perl
use strict;
use Tk;
use Tk::HList;
require Tk::LabEntry;
require Tk::Dialog;
use DBI;
my $host = "localhost";
my $database="test";
my $user = "root";
my $pw = "28alonso28";
my $tl;
my $oConexInf="DBI:mysql:database=$database;$host:3306";
my $connect=DBI->connect($oConexInf,$user,$pw);
#declare vars "glob"
my $e;
my $e2;
#end
my $mw = MainWindow->new;
$mw->title("Admin Mysql By TKZeXe");
$mw->geometry('500x400');
$mw->resizable(0,0);
my $frame = $mw->Frame(-borderwidth => 2, -relief => 'groove');
my $frame2 = $mw->Frame(-borderwidth => 2,-relief => 'groove');
my $scroll = $frame->Scrollbar( );
my $hlist = $frame->HList(-command=>[\&oEditReg],-columns => 3,-width=>"50",-height=>"10",-background=>"white", -header => 1,
-selectbackground=>"#CCCCCC", -selectborderwidth=>0);
my $title=$frame->Label(-text => "Productos :\n");
my $title_h1=$mw->Label(-text => "Edita,Borra y revisa tus Productos.\n\n",
-foreground =>"Blue");
#header (cabeceras)
$hlist->headerCreate(0, -text => "ID");
$hlist->headerCreate(1, -text => "Precio");
$hlist->headerCreate(2, -text => "Nombre");
my $query="SELECT ID,Precio,Nombre from Productos ORDER BY ID ASC ";
my $i=0;
my $id;
my $price;
my $name;
my $sth = $connect->prepare($query);
$sth->execute();
while(my @result = $sth->fetchrow_array())
{
$hlist->add($i);
$hlist->itemCreate($i, 0, -text => $result[0]);
$hlist->itemCreate($i, 1, -text => $result[1]);
$hlist->itemCreate($i, 2, -text => $result[2]);
$i++;
}
$hlist->configure(-yscrollcommand => [\&scroll_listboxes],-height=>"10");
$scroll->configure(-command =>['yview' => $hlist]);
my $Bdelete=$frame2->Button(-text => "Delete",-width=>"10");
my $Badd=$frame2->Button(-text => "Add",-width=>"10");
my $Bedit=$frame2->Button(-text => "Edit",-width=>"10");
#packs
$title_h1->pack(-anchor => 'n');
$frame->pack(-side => "top",-anchor=>"n");
$title->pack();
$hlist->pack(-expand => 0, -fill => 'both',-side => 'left');
$scroll->pack(-side => 'left', -fill => 'y');
$frame2->pack(-side => 'bottom',-fill=>'x');
$Bdelete->pack(-side => 'left',-expand => 1,-padx=>10);
$Bedit->pack(-side => 'left',-expand => 1,-padx=>10);
$Badd->pack(-side => 'left',-expand => 1,-padx=>10);
#end
$Bdelete->bind('<Button-1>',\&oDeleteReg );
$Bedit->bind('<Button-1>',\&oEditReg);
$Badd->bind('<Button-1>',\&oAddReg );
sub oDeleteReg{
my $path=@_;
my $Item = $hlist->selectionGet();
my $data=$hlist->itemCget($Item, 0, -text);
my $answer = $mw->Dialog(-title => 'Informacion!',
-text => 'Deseas borrar definitivamente el registro seleccionado?',
-default_button => 'no', -buttons => [ 'yes', 'no'],
-bitmap => 'question' )->Show( );
if ($answer eq 'no') {
return 0;
}
#continue:
my $query="DELETE from Productos WHERE ID='$data' ";
my $sth = $connect->prepare($query);
unless($sth->execute()){
print "Error en la consulta".$sth->errstr;
}else{
$hlist->delete('entry', $Item);
};
}
sub oEditReg{
my($patt)=@_;
my $Item = $hlist->selectionGet();
my $data=$hlist->itemCget($Item, 0, -text);
my $query="SELECT ID,Precio,Nombre from Productos WHERE ID='$data' LIMIT 1";
my $sth = $connect->prepare($query);
$sth->execute();
my @result = $sth->fetchrow_array();
if (! Exists($tl)) {
$tl = $mw->Toplevel(-container => 0);
$tl->title("Toplevel");
$tl->geometry('400x300');
$tl->resizable(0,0);
$tl->Label(-text => "Edita tu Producto \n")->pack(-side=>'top');
my $f1=$tl->Frame(-borderwidth => 1, -relief => 'groove')->pack(-side => "top",-anchor=>"nw");
$f1->Label(-text => "Nombre Producto : ")->grid(-row => 0, -column => 0);
$e = $f1->Entry()->grid(-row => 0, -column => 1);
$e->insert('end',$result[1]);
$f1->Label(-text => "Precio Producto (ej: 12.88) : ")->grid(-row => 1, -column => 0);
$e2 = $f1->Entry()->grid(-row => 1, -column => 1);
$e2->insert('end',$result[2]);
my $f2=$tl->Frame(-borderwidth => 1, -relief => 'groove')->pack(-side => "bottom",-fill=>'x');
$f2->Button(-text => "Editar Registro",-command => \&UpdateReg)->pack(-side=>'left');
$f2->Button(-text => "Close",-command => sub { $tl->withdraw })->pack(-side=>'right');
} else {
$tl->deiconify( );
$tl->raise( );
}
}
sub oAddReg{
if (! Exists($tl)) {
$tl = $mw->Toplevel(-container => 0);
$tl->title("Toplevel");
$tl->geometry('400x300');
$tl->resizable(0,0);
$tl->Label(-text => "Ingresa tu Nuevo Producto \n")->pack(-side=>'top');
my $f1=$tl->Frame(-borderwidth => 1, -relief => 'groove')->pack(-side => "top",-anchor=>"nw");
$f1->Label(-text => "Nombre Producto : ")->grid(-row => 0, -column => 0);
$e = $f1->Entry()->grid(-row => 0, -column => 1);
$f1->Label(-text => "Precio Producto (ej: 12.88) : ")->grid(-row => 1, -column => 0);
$e2 = $f1->Entry()->grid(-row => 1, -column => 1);
my $f2=$tl->Frame(-borderwidth => 1, -relief => 'groove')->pack(-side => "bottom",-fill=>'x');
$f2->Button(-text => "Ingresar Registro",-command => \&InsertInto)->pack(-side=>'left');
$f2->Button(-text => "Close",-command => sub { $tl->withdraw })->pack(-side=>'right');
} else {
$tl->deiconify( );
$tl->raise( );
}
}
sub UpdateReg{
my($patt)=@_;
my $name=$e->get();
my $price=$e2->get();
my $Item = $hlist->selectionGet();
my $ID=$hlist->itemCget($Item, 0, -text);
my $query="UPDATE Productos SET Precio='$price',Nombre='$name' WHERE ID='$ID'";
my $sth = $connect->prepare($query);
unless($sth->execute()){
my $answer = $mw->Dialog(-title => 'Informacion!',
-text => "Error en la consulta".$sth->errstr,
-default_button => 'Aceptar', -buttons => [ 'Aceptar'],
-bitmap => 'question' )->Show( );
}else{
my $answer = $mw->Dialog(-title => 'Informacion!',
-text => 'Haz Actualizado los datos correctamente!',
-default_button => 'Aceptar', -buttons => [ 'Aceptar'],
-bitmap => 'question' )->Show( );
};
$tl->withdraw;
}
sub InsertInto{
my($patt)=@_;
my $name=$e->get();
my $price=$e2->get();
my $query="INSERT INTO Productos (ID,Precio,Nombre) VALUES('','$price','$name')";
my $sth = $connect->prepare($query);
unless($sth->execute()){
my $answer = $mw->Dialog(-title => 'Informacion!',
-text => "Error en la consulta".$sth->errstr,
-default_button => 'Aceptar', -buttons => [ 'Aceptar'],
-bitmap => 'question' )->Show( );
}else{
my $answer = $mw->Dialog(-title => 'Informacion!',
-text => 'Haz insertado los datos correctamente!',
-default_button => 'Aceptar', -buttons => [ 'Aceptar'],
-bitmap => 'question' )->Show( );
};
}
sub scroll_listboxes {
my (@args) = @_;
$scroll->set(@args); # Llama a la barra de desplazamiento
my ($top, $bottom) = $hlist->yview( );
}
MainLoop;