unit uViewFiltroBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, cxRadioGroup, TBXDkPanels, TB2ExtItems, TBXExtItems,
  TBX, TB2Item, TB2Dock, TB2Toolbar, ActnList, ImgList, PngImageList,
  cxGraphics, dxLayoutLookAndFeels, dxLayoutControl, cxDropDownEdit, cxMaskEdit,
  cxCalendar, cxContainer, cxEdit, cxTextEdit, cxControls, uViewBase;

type
  IViewFiltroBase = interface(IViewBase)
  ['{0D0EA630-BF93-4BA1-93C2-FD5A5B0CBEED}']
    function GetFiltrosChange: TNotifyEvent;
    procedure SetFiltrosChange(const Value: TNotifyEvent);
    property OnFiltrosChange: TNotifyEvent read GetFiltrosChange write SetFiltrosChange;

    function GetVerFiltros: Boolean;
    procedure SetVerFiltros(const Value: Boolean);
    property VerFiltros: Boolean read GetVerFiltros write SetVerFiltros;

    function GetTexto: String;
    procedure SetTexto(const Value: String);
    property Texto: String read GetTexto write SetTexto;
  end;

  TfrViewFiltroBase = class(TfrViewBase, IViewFiltroBase)
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutControl1Item1: TdxLayoutItem;
    txtFiltroTodo: TcxTextEdit;
    dxLayoutControl1Item2: TdxLayoutItem;
    edtFechaIniFiltro: TcxDateEdit;
    dxLayoutControl1Item3: TdxLayoutItem;
    edtFechaFinFiltro: TcxDateEdit;
    dxLayoutControl1Group1: TdxLayoutGroup;
    TBXDockablePanel1: TTBXDockablePanel;
    ActionList1: TActionList;
    actQuitarFiltro: TAction;
    PngImageList: TPngImageList;
    tbxBotones: TTBXToolbar;
    TBXItem2: TTBXItem;
    TBXAlignmentPanel1: TTBXAlignmentPanel;
    dxLayoutControl1Item4: TdxLayoutItem;
    eLista: TcxComboBox;
    procedure OnCamposFiltroChange(Sender: TObject);
    procedure actQuitarFiltroExecute(Sender: TObject);

  private
    FOnFiltrosChange: TNotifyEvent;

    function GetFiltrosChange: TNotifyEvent;
    procedure SetFiltrosChange(const Value: TNotifyEvent);
    function GetVerFiltros: Boolean;
    procedure SetVerFiltros(const Value: Boolean);
    function GetTexto: String;
    procedure SetTexto(const Value: String);

  protected
    procedure LimpiarCampos; virtual;
    function ValidarCampos: Boolean; virtual;

  public
    function GetModified: Boolean;
    property OnFiltrosChange: TNotifyEvent read GetFiltrosChange write SetFiltrosChange;
    property VerFiltros: Boolean read GetVerFiltros write SetVerFiltros;
    property Texto: String read GetTexto write SetTexto;
  end;

implementation
{$R *.dfm}

uses uDialogUtils;

{ TfrViewFiltroBase }

function TfrViewFiltroBase.GetFiltrosChange: TNotifyEvent;
begin
  Result := FOnFiltrosChange;
end;

function TfrViewFiltroBase.GetModified: Boolean;
begin
  Result := False;
end;

procedure TfrViewFiltroBase.SetFiltrosChange(const Value: TNotifyEvent);
begin
  FOnFiltrosChange := Value;
end;

function TfrViewFiltroBase.GetVerFiltros: Boolean;
begin
  Result := Self.Visible;
end;

procedure TfrViewFiltroBase.SetVerFiltros(const Value: Boolean);
begin
  Self.Visible := Value;
  if not Self.Visible then
     actQuitarFiltro.Execute;
end;

procedure TfrViewFiltroBase.LimpiarCampos;
begin
  txtFiltroTodo.Clear;
  edtFechaIniFiltro.Clear;
  edtFechaFinFiltro.Clear;
end;

procedure TfrViewFiltroBase.OnCamposFiltroChange(Sender: TObject);
begin
  if ValidarCampos then
     if Assigned(FOnFiltrosChange) then
        FOnFiltrosChange(Sender);
end;

function TfrViewFiltroBase.ValidarCampos: Boolean;
begin
  Result := True;

  if not VarIsNull(edtFechaIniFiltro.EditValue) and not VarIsNull(edtFechaFinFiltro.EditValue) then
  begin
    if (edtFechaIniFiltro.EditValue > edtFechaFinFiltro.EditValue) then
    begin
       ShowWarningMessage('La fecha de inicio debe ser anterior a la fecha final');
       edtFechaIniFiltro.SetFocus;
       Result := False;
    end
  end;  
end;

procedure TfrViewFiltroBase.actQuitarFiltroExecute(Sender: TObject);
begin
  LimpiarCampos;
  if Assigned(FOnFiltrosChange) then
     FOnFiltrosChange(Sender);
end;

function TfrViewFiltroBase.GetTexto: String;
begin
  Result := txtFiltroTodo.Text;
end;

procedure TfrViewFiltroBase.SetTexto(const Value: String);
begin
  txtFiltroTodo.Text := Value;
end;

end.
