unit uLoginForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, cxGraphics, cxControls,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit,
  cxImageComboBox, ImgList, PngImageList, pngimage, ToolWin, JvExControls,
  JvComponent, JvGradient, JvGIF, JvComponentBase, JvFormPlacement,
  JvAppStorage, JvAppRegistryStorage;

type
  TfLoginForm = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    edtPassword: TEdit;
    bAceptar: TButton;
    bCancelar: TButton;
    Label1: TLabel;
    edtUser: TEdit;
    JvGradient1: TJvGradient;
    Button1: TButton;
    Timer1: TTimer;
    JvAppRegistryStorage1: TJvAppRegistryStorage;
    JvFormStorage1: TJvFormStorage;
    Image1: TImage;
    procedure bAceptarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FIntentos: Integer;
  end;
  
var
  fLoginForm: TfLoginForm;

implementation

uses
  uDataModuleUsuarios, uDataModuleConexion, uDataModuleBase;

{$R *.dfm}

{
********************************* TfLoginForm **********************************
}
procedure TfLoginForm.bAceptarClick(Sender: TObject);
var
  bOk : Boolean;
begin
  ShowHourglassCursor;
  try
    bOK := dmUsuarios.Login(edtUser.Text, edtPassword.Text);
  finally
    HideHourglassCursor;
  end;
  
  if bOk then
    ModalResult := mrOK
  else begin
    Application.MessageBox('Usuario no válido. Compruebe si ha escrito correctamente'
      + #13 + #10 + 'el usuario y la contraseña.', 'Atención', MB_OK);
    Dec(FIntentos);
    if (FIntentos <= 0) then
      ModalResult := mrCancel;
  end;
end;

procedure TfLoginForm.FormCreate(Sender: TObject);
begin
  FIntentos := 3;
end;

procedure TfLoginForm.ToolButton4Click(Sender: TObject);
begin
  Timer1.Enabled := False;
  dmConexion.ConfigurarConexion;
  Timer1.Enabled := True;
end;

procedure TfLoginForm.FormShow(Sender: TObject);
begin
  Self.Caption := Self.Caption + ' - ' + dmBase.DarVersion;
  JvFormStorage1.RestoreFormPlacement;
  // Hacer login automática si hay usuario/password y no hay más de una base
  // de datos como opción para conectarse.
  if ((Length(edtUser.Text) > 0) and (Length(edtPassword.Text) > 0)) then
    Timer1.Enabled := True;
end;

procedure TfLoginForm.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  bAceptar.Click;
end;

end.
