unit uEditorTiendaWeb;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDialogBase, ActnList, StdCtrls, ExtCtrls, cxTextEdit, cxDBEdit,
  cxControls, cxContainer, cxEdit, cxCheckBox, DBXpress, WideStrings, SqlExpr,
  uIEditorTiendaWeb, uBizTiendaWeb, uTiendaWebController, DB, uDADataTable;

type
  TfEditorTiendaWeb = class(TfDialogBase, IEditorTiendaWeb)
    cbActivo: TcxDBCheckBox;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edtBDSERVER: TcxDBTextEdit;
    edtBDUSER: TcxDBTextEdit;
    edtBDPASS: TcxDBTextEdit;
    edtBDNAME: TcxDBTextEdit;
    bComprobar: TButton;
    actComprobarAcceso: TAction;
    dsTiendaWeb: TDADataSource;
    procedure actComprobarAccesoUpdate(Sender: TObject);
    procedure actComprobarAccesoExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cbActivoPropertiesEditValueChanged(Sender: TObject);
    procedure actAceptarExecute(Sender: TObject);
    procedure actCancelarExecute(Sender: TObject);
  private
    procedure RefrescarControles;
  protected
    FTienda : IBizTiendaWeb;
    FController : ITiendaWebController;
    function GetTiendaWeb: IBizTiendaWeb;
    procedure SetTiendaWeb(const Value: IBizTiendaWeb);
  public
    property TiendaWeb: IBizTiendaWeb read GetTiendaWeb write SetTiendaWeb;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uDialogUtils;


procedure TfEditorTiendaWeb.actAceptarExecute(Sender: TObject);
begin
  inherited;
  FController.Guardar(FTienda);
  Close;
end;

procedure TfEditorTiendaWeb.actCancelarExecute(Sender: TObject);
begin
  inherited;
  FController.DescartarCambios(FTienda);
  Close;
end;

procedure TfEditorTiendaWeb.actComprobarAccesoExecute(Sender: TObject);
var
  SQLConnection1: TSQLConnection;
begin
  SQLConnection1 := TSQLConnection.Create(Self);
  try
    with SQLConnection1 do
    begin
      Name := 'SQLConnection1';
      ConnectionName := 'MySQLConnection';
      DriverName := 'MySQL';
      GetDriverFunc := 'getSQLDriverMYSQL';
      LibraryName := 'dbxmys30.dll';
      LoginPrompt := False;
      with Params do
      begin
        Clear;
        Add('BlobSize=-1');
        Add('DriverName=MySQL');
        Add('ErrorResourceFile=');
        Add('LocaleCode=0000');
        Add('Compressed=True');
        Add('Encrypted=True');
        Add('Database=' + edtBDNAME.Text);
        Add('HostName=' + edtBDSERVER.Text);
        Add('User_Name=' + edtBDUSER.Text);
        Add('Password=' + edtBDPASS.Text);
      end;
      VendorLib := 'LIBMYSQL.dll';

      try
        Connected := True;
        ShowInfoMessage('Conexión válida con la tienda');
        Connected := False;
      except
        on E : Exception do
          ShowErrorMessage('Error de conexión', 'No se ha podido establecer la conexión con la base de datos.', E);
      end;
    end;
  finally
    FreeAndNIL(SQLConnection1);
  end;
end;

procedure TfEditorTiendaWeb.actComprobarAccesoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled :=
    (Length(edtBDSERVER.Text) > 0) and
    (Length(edtBDUSER.Text) > 0) and
    (Length(edtBDNAME.Text) > 0);
end;

procedure TfEditorTiendaWeb.cbActivoPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  RefrescarControles;
end;

constructor TfEditorTiendaWeb.Create(AOwner: TComponent);
begin
  inherited;
  FController := TTiendaWebController.Create;
end;

destructor TfEditorTiendaWeb.Destroy;
begin
  FController := NIL;
  FTienda := NIL;
  inherited;
end;

procedure TfEditorTiendaWeb.FormShow(Sender: TObject);
begin
  inherited;
  if not FTienda.DataTable.Active then
     FTienda.DataTable.Active := True;

  RefrescarControles;
end;

function TfEditorTiendaWeb.GetTiendaWeb: IBizTiendaWeb;
begin
  Result := FTienda;
end;

procedure TfEditorTiendaWeb.RefrescarControles;
begin
  edtBDSERVER.Enabled := (FTienda.TIENDA_ACTIVA = 1);
  edtBDUSER.Enabled := (FTienda.TIENDA_ACTIVA = 1);
  edtBDPASS.Enabled := (FTienda.TIENDA_ACTIVA = 1);
  edtBDNAME.Enabled := (FTienda.TIENDA_ACTIVA = 1);
end;

procedure TfEditorTiendaWeb.SetTiendaWeb(const Value: IBizTiendaWeb);
begin
  FTienda := Value;
  if Assigned(FTienda) then
  begin
    dsTiendaWeb.DataTable := FTienda.DataTable;
    dsTiendaWeb.DataTable.Open;
  end
  else
    dsTiendaWeb.DataTable := NIL;
end;

end.
