unit uViewDatosYSeleccionCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, StdCtrls, cxControls, cxContainer, cxEdit, cxLabel,
  cxDBLabel, ExtCtrls, DB, uDADataTable, uBizContactos,
  ComCtrls, ToolWin, ActnList, ImgList, PngImageList, cxTextEdit, cxDBEdit,
  pngimage, JvExControls, JvComponent, JvButton, JvTransparentButton, Mask,
  DBCtrls, uClientesController, dxLayoutControl, Buttons, uDAInterfaces;

type
  IViewDatosYSeleccionCliente = interface(IViewBase)
    ['{BCC2B36B-4A19-4981-B69A-56E258A898F0}']
    function GetController: IClientesController;
    procedure SetController(Value: IClientesController);
    property Controller: IClientesController read GetController write SetController;

    function GetCliente: IBizCliente;
    procedure SetCliente(Value: IBizCliente);
    property Cliente: IBizCliente read GetCliente write SetCliente;

    function GetID_Cliente: integer;
    procedure SetID_Cliente(const Value: integer);
    property ID_Cliente: integer read GetID_Cliente write SetID_Cliente;

    function GetOnClienteChanged : TNotifyEvent;
    procedure SetOnClienteChanged (const Value : TNotifyEvent);
    property OnClienteChanged : TNotifyEvent read GetOnClienteChanged
      write SetOnClienteChanged;

    function GetMsgElegirCliente: String;
    procedure SetMsgElegirCliente(const Value: String);
    property MsgElegirCliente : String read GetMsgElegirCliente
      write SetMsgElegirCliente;
  end;

  TfrViewDatosYSeleccionCliente = class(TfrViewBase, IViewDatosYSeleccionCliente)
    DADataSource: TDADataSource;
    ActionList1: TActionList;
    actElegirContacto: TAction;
    actAnadirContacto: TAction;
    actVerContacto: TAction;
    PngImageList: TPngImageList;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Item1: TdxLayoutItem;
    edtlNombre: TcxDBTextEdit;
    dxLayoutControl1Item2: TdxLayoutItem;
    edtNIFCIF: TcxDBTextEdit;
    dxLayoutControl1Item3: TdxLayoutItem;
    edtCalle: TcxDBTextEdit;
    dxLayoutControl1Item4: TdxLayoutItem;
    edtPoblacion: TcxDBTextEdit;
    dxLayoutControl1Item5: TdxLayoutItem;
    edtProvincia: TcxDBTextEdit;
    dxLayoutControl1Item6: TdxLayoutItem;
    edtCodigoPostal: TcxDBTextEdit;
    Button1: TBitBtn;
    dxLayoutControl1Item7: TdxLayoutItem;
    Button2: TBitBtn;
    dxLayoutControl1Item8: TdxLayoutItem;
    Button3: TBitBtn;
    dxLayoutControl1Item9: TdxLayoutItem;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Group2: TdxLayoutGroup;
    procedure actElegirContactoExecute(Sender: TObject);
    procedure actAnadirContactoExecute(Sender: TObject);
    procedure actVerContactoExecute(Sender: TObject);
    procedure actVerContactoUpdate(Sender: TObject);

  private
    FOnClienteChanged : TNotifyEvent;
    FMsgElegirCliente: String;
    function GetMsgElegirCliente: String;
    procedure SetMsgElegirCliente(const Value: String);

  protected
    FController : IClientesController;
    FCliente : IBizCliente;
    function GetController: IClientesController;
    procedure SetController(Value: IClientesController);
    function GetCliente: IBizCliente; virtual;
    procedure SetCliente(Value: IBizCliente); virtual;
    function GetOnClienteChanged : TNotifyEvent;
    procedure SetOnClienteChanged (const Value : TNotifyEvent);
    procedure SetReadOnly(Value: Boolean); override;
    function GetID_Cliente: integer; virtual;
    procedure SetID_Cliente(const Value: integer); virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Controller: IClientesController read GetController write SetController;
    property Cliente: IBizCliente read GetCliente write SetCliente;
    property OnClienteChanged : TNotifyEvent read GetOnClienteChanged write SetOnClienteChanged;
    property MsgElegirCliente : String read GetMsgElegirCliente write SetMsgElegirCliente;
    property ID_Cliente: integer read GetID_Cliente write SetID_Cliente;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleClientes, Math, uCustomView;

procedure TfrViewDatosYSeleccionCliente.actElegirContactoExecute(Sender: TObject);
var
  ACliente : IBizCliente;
begin
  inherited;
  ACliente := (Controller.ElegirContacto(Controller.BuscarTodos, FMsgElegirCliente, False) as IBizCliente);
  if Assigned(ACliente) then
  begin
    Self.Update;  
    ShowHourglassCursor;
    try
      Cliente := ACliente;
    finally
      HideHourglassCursor;
    end;
  end;
  ACliente := Nil;
end;

procedure TfrViewDatosYSeleccionCliente.actAnadirContactoExecute(
  Sender: TObject);
var
  ACliente : IBizCliente;
begin
  inherited;
  ACliente := (Controller.Nuevo as IBizCliente);
  Controller.Ver(ACliente);
  Cliente := ACliente;
end;

procedure TfrViewDatosYSeleccionCliente.actVerContactoExecute(Sender: TObject);
begin
  inherited;
  Controller.Ver(Cliente);
end;

procedure TfrViewDatosYSeleccionCliente.actVerContactoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(edtlNombre.Text) > 0);
end;

constructor TfrViewDatosYSeleccionCliente.Create(AOwner: TComponent);
begin
  inherited;
  FMsgElegirCliente := '';
  FController := NIL;
end;

destructor TfrViewDatosYSeleccionCliente.Destroy;
begin
  FController := NIL;
  inherited;
end;

function TfrViewDatosYSeleccionCliente.GetCliente: IBizCliente;
begin
  Result := FCliente;
end;

function TfrViewDatosYSeleccionCliente.GetController: IClientesController;
begin
  if not Assigned(FController) then
    FController := TClientesController.Create;
  Result := FController;
end;

function TfrViewDatosYSeleccionCliente.GetID_Cliente: integer;
begin
  Result := -1;
  if Assigned(FCliente) then
    Result := FCliente.ID;
end;

function TfrViewDatosYSeleccionCliente.GetMsgElegirCliente: String;
begin
  Result := FMsgElegirCliente;
end;

procedure TfrViewDatosYSeleccionCliente.SetCliente(Value: IBizCliente);
begin
  FCliente := Value;
  if Assigned(FCliente) then
  begin
    DADataSource.DataTable := FCliente.DataTable;
    if not FCliente.DataTable.Active then
      FCliente.DataTable.Active := True;
  end
  else
    DADataSource.DataTable := NIL;

  if Assigned(FOnClienteChanged) then
    FOnClienteChanged(Self);
end;

procedure TfrViewDatosYSeleccionCliente.SetController(Value: IClientesController);
begin
  if Assigned(FController) then
    FController := NIL;

  FController := Value;
end;

procedure TfrViewDatosYSeleccionCliente.SetID_Cliente(const Value: integer);
begin
  Self.Cliente := IBizCliente(Controller.Buscar(Value));
end;

procedure TfrViewDatosYSeleccionCliente.SetMsgElegirCliente(
  const Value: String);
begin
  FMsgElegirCliente := Value;
end;

function TfrViewDatosYSeleccionCliente.GetOnClienteChanged: TNotifyEvent;
begin
  Result := FOnClienteChanged;
end;

procedure TfrViewDatosYSeleccionCliente.SetOnClienteChanged(
  const Value: TNotifyEvent);
begin
  FOnClienteChanged := Value;
end;

procedure TfrViewDatosYSeleccionCliente.SetReadOnly(Value: Boolean);
begin
  inherited;

  if (csLoading in Self.ComponentState) then
    Exit;
  
  actAnadirContacto.Enabled := not ReadOnly;
  actElegirContacto.Enabled := not ReadOnly;
end;

end.
