unit uEditorTiendaWeb;

interface

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

type
  TfEditorTiendaWeb = class(TForm, IEditorTiendaWeb)
    Panel1: TPanel;
    Panel2: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    OKBtn: TButton;
    CancelBtn: TButton;
    ActionList1: TActionList;
    actAceptar: TAction;
    actCancelar: TAction;
    cbActivo: TcxDBCheckBox;
    dsTiendaWeb: TDADataSource;
    GroupBox1: TGroupBox;
    edtBDSERVER: TcxDBTextEdit;
    edtBDUSER: TcxDBTextEdit;
    edtBDPASS: TcxDBTextEdit;
    edtBDNAME: TcxDBTextEdit;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    bComprobar: TButton;
    actComprobar: TAction;
    procedure FormShow(Sender: TObject);
    procedure actCancelarExecute(Sender: TObject);
    procedure actAceptarExecute(Sender: TObject);
    procedure cbActivoPropertiesEditValueChanged(Sender: TObject);
    procedure actComprobarUpdate(Sender: TObject);
    procedure actComprobarExecute(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}

{ TfEditorTiendaWeb }

uses
  uDialogUtils;

procedure TfEditorTiendaWeb.actAceptarExecute(Sender: TObject);
begin
  FTienda.Edit;
  if cbActivo.Checked then
    FTienda.TIENDA_ACTIVA := 1
  else
    FTienda.TIENDA_ACTIVA := 0;
  
  FController.Guardar(FTienda);
  Close;
end;

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

procedure TfEditorTiendaWeb.actComprobarExecute(Sender: TObject);
begin
  if FController.HayConexionConTienda(edtBDSERVER.Text, edtBDNAME.Text,
    edtBDUSER.Text, edtBDPASS.Text) then
    ShowInfoMessage('Conexión válida con la tienda')
  else
    ShowErrorMessage('Error de conexión', 'No se ha podido establecer la conexión con la base de datos.');

end;

procedure TfEditorTiendaWeb.actComprobarUpdate(Sender: TObject);
begin
  (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
  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.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.FormShow(Sender: TObject);
begin
  if not FTienda.DataTable.Active then
     FTienda.DataTable.Active := True;

  RefrescarControles;
end;

function TfEditorTiendaWeb.GetTiendaWeb: IBizTiendaWeb;
begin
  Result := FTienda;
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.

