//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "login_dlg.h"
#include "clientes.h"
#include "tsplash.h"
#include "ZDataset.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tlogin *login;
//---------------------------------------------------------------------------
__fastcall Tlogin::Tlogin(TComponent* Owner)
        : TForm(Owner)
{
        auth = false;
}
//---------------------------------------------------------------------------
void __fastcall Tlogin::BitBtn1Click(TObject *Sender)
{
        Close();
}
//---------------------------------------------------------------------------


void __fastcall Tlogin::FormShow(TObject *Sender)
{
  if(Splash){
        delete Splash;
        Splash = NULL;
  }        
}
//---------------------------------------------------------------------------
bool __fastcall Tlogin::validateUserPass(AnsiString user, AnsiString pass) {
  bool auth = false;

  if ( !gestionClientes->ZConnection1->Connected ) return false;

  TZQuery *zq;
  zq = new TZQuery(this);
  zq->Connection = gestionClientes->ZConnection1;


  zq->SQL->Clear();
  zq->SQL->Add("SELECT id FROM usuarios WHERE usuario='"+user+"' AND password='"+pass+"'");
  zq->Active = true;
  if ( zq->RecordCount>0 ) {
      auth = true;
  }

  if ( auth ) {
    // Registrar acceso OK
  } else {
    // Registrar acceso !OK
  }

  return auth;
}
//---------------------------------------------------------------------------

void __fastcall Tlogin::BitBtn2Click(TObject *Sender)
{
  TCursor Save_Cursor = Screen->Cursor;
  Screen->Cursor = crHourGlass;    // Show hourglass cursor
  try {
    auth = validateUserPass( user->Text, pass->Text );
  } __finally {
    Screen->Cursor = Save_Cursor; // always restore the cursor
  }
}
//---------------------------------------------------------------------------

