unit uViewContactos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewGrid, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, uBizContactos, ActnList, Menus,
  cxGridBandedTableView, cxGridDBBandedTableView, JvComponent,
  JvFormAutoSize, PngImageList, ImgList, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns,  dxPSCore, dxPScxCommon, 
  uDataModuleClientes, cxGridCustomPopupMenu, cxGridPopupMenu, uViewFiltroBase,
  ComCtrls, TB2Item, SpTBXItem, TB2Toolbar, dxPgsDlg, dxPScxGrid6Lnk, uDAInterfaces, SpTBXDkPanels,
  TB2Dock, uCustomView, uViewBase, cxLookAndFeels, cxLookAndFeelPainters,
  dxPSPDFExportCore, dxPSPDFExport, cxDrawTextUtils, dxPSPrVwStd, dxPSPrVwAdv,
  dxPScxEditorProducers, dxPScxExtEditorProducers, dxPScxPageControlProducer;

type
  IViewContactos = interface(IViewGrid)
    ['{9E43DEE1-F72B-4C93-8592-1C7DF01E293D}']
    function GetContactos: IBizContacto;
    procedure SetContactos(const Value: IBizContacto);
    property Contactos: IBizContacto read GetContactos write SetContactos;
  end;

  TfrViewContactos = class(TfrViewGrid, IViewContactos)
    cxGridViewNIF_CIF: TcxGridDBColumn;
    cxGridViewNOMBRE: TcxGridDBColumn;
    cxGridViewCALLE: TcxGridDBColumn;
    cxGridViewPOBLACION: TcxGridDBColumn;
    cxGridViewPROVINCIA: TcxGridDBColumn;
    cxGridViewCODIGO_POSTAL: TcxGridDBColumn;
    cxGridViewTELEFONO_1: TcxGridDBColumn;
    cxGridViewTELEFONO_2: TcxGridDBColumn;
    cxGridViewMOVIL_1: TcxGridDBColumn;
    cxGridViewFAX: TcxGridDBColumn;
    cxGridViewEMAIL_1: TcxGridDBColumn;
    cxGridViewEMAIL_2: TcxGridDBColumn;
    cxGridViewPAGINA_WEB: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    procedure OnFiltroListaPropertiesChange(Sender: TObject);
    procedure OnFiltroListaPropertiesInitPopup(Sender: TObject);

    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
  private
    procedure AnadirFiltroProvincia;

  protected
    FProvincias : TStringList;
    FContactos: IBizContacto;
    function GetContactos: IBizContacto; virtual;
    procedure SetContactos(const Value: IBizContacto); virtual;
    procedure CargarProvincias;
  public
    procedure AnadirOtrosFiltros; override;
    property DataSet: IBizContacto read GetContactos write SetContactos;
  end;

implementation
{$R *.dfm}
{ TfrViewContactos }

uses uProvinciasPoblacionesController;

{
******************************* TfrViewContactos *******************************
}
procedure TfrViewContactos.AnadirFiltroProvincia;
var
  FFiltro : TcxFilterCriteriaItemList;
begin

  //Solo se aplica este filtro en el caso de tener activo el panel de detalle de filtro
  //y sobre la lista de pedidos
  if frViewFiltroBase1.Visible then
  begin
    if (VarToStr(frViewFiltroBase1.eLista.EditValue) <> 'Todos') then
    begin
      FFiltro := AddFilterGrid(fboAnd);
      FFiltro.AddItem(cxGridViewPROVINCIA, foEqual, VarToStr(frViewFiltroBase1.eLista.EditValue), VarToStr(frViewFiltroBase1.eLista.EditValue));
    end;
  end;
end;

procedure TfrViewContactos.AnadirOtrosFiltros;
begin
  inherited;
  AnadirFiltroProvincia;

  //Finalmente activamos el filtro si tenemos algo
  if cxGridView.DataController.Filter.IsEmpty
  then cxGridView.DataController.Filter.Active := False
  else cxGridView.DataController.Filter.Active := True;
  cxGrid.ActiveLevel.GridView := cxGridView;
end;

procedure TfrViewContactos.CargarProvincias;
var
  i : integer;
begin
  with TProvinciasPoblacionesController.Create do
    try
      FProvincias := DarListaProvincias;

      with frViewFiltroBase1.eLista.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          Add('Todos');  //Case 0
          for i := 0 to FProvincias.Count - 1 do
            Add(FProvincias.Names[i]);

          frViewFiltroBase1.eLista.ItemIndex := 0;
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
end;

procedure TfrViewContactos.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FProvincias := NIL;
end;

procedure TfrViewContactos.CustomViewDestroy(Sender: TObject);
begin
  frViewFiltroBase1.eLista.Properties.OnChange := Nil;
  frViewFiltroBase1.eLista.Properties.OnInitPopup := Nil;

  if Assigned(FProvincias) then
    FreeANDNIL(FProvincias);

  inherited;
end;

procedure TfrViewContactos.CustomViewShow(Sender: TObject);
begin
  inherited;
  frViewFiltroBase1.eLista.Properties.OnChange := OnFiltroListaPropertiesChange;
  frViewFiltroBase1.eLista.Properties.OnInitPopup := OnFiltroListaPropertiesInitPopup;
end;

function TfrViewContactos.GetContactos: IBizContacto;
begin
  Result := FContactos;
end;

procedure TfrViewContactos.OnFiltroListaPropertiesChange(Sender: TObject);
begin
  inherited;
  RefrescarFiltro;
end;

procedure TfrViewContactos.OnFiltroListaPropertiesInitPopup(Sender: TObject);
begin
  inherited;
  ShowHourglassCursor;
  try
    if not Assigned(FProvincias) then
      CargarProvincias;
  finally
    HideHourglassCursor;
  end;
end;

procedure TfrViewContactos.SetContactos(const Value: IBizContacto);
begin
  FContactos := Value;
  if Assigned(FContactos) then
    dsDataSource.DataTable := FContactos.DataTable;
end;


end.
