unit uViewInformes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxGraphics, cxCustomData, cxStyles, cxTL,
  cxImageComboBox, cxMaskEdit, cxTextEdit, cxInplaceContainer, cxDBTL,
  cxControls, cxTLData, Menus, uBizInformes, DB, uDADataTable,
  ImgList, PngImageList, EasyListview, MPCommonUtilities,
  MPCommonObjects, pngimage, JSDialog; 

type
  IViewInformes = interface(IViewBase)
  ['{D991FCEC-08DB-4F77-BD57-FA04B3C88788}']
    function GetDblClick: TNotifyEvent;
    procedure SetDblClick(const Value: TNotifyEvent);
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;

    function GetPopupMenu: TPopupMenu;
    procedure SetPopupMenu(const Value: TPopupMenu); 
    property PopupMenu: TPopupMenu read GetPopupMenu write SetPopupMenu;

    function GetInformes: IBizInforme;
    procedure SetInformes(const Value: IBizInforme);
    property Informes: IBizInforme read GetInformes write SetInformes;

    procedure Refresh;
  end;

  TfrViewInformes = class(TfrViewBase, IViewInformes)
    ReportImages: TPngImageList;
    dsDataTable: TDADataSource;
    EasyListview: TEasyListview;
    JsNuevoDialog: TJSDialog;
//    procedure EasyListviewItemDblClick(Sender: TCustomEasyListview;
//      Button: TEasyMouseButton; MousePos: TPoint;
//      HitInfo: TEasyHitInfoItem);
    procedure EasyListviewItemContextMenu(Sender: TCustomEasyListview;
      HitInfo: TEasyHitInfoItem; WindowPoint: TPoint; var Menu: TPopupMenu;
      var Handled: Boolean);
    procedure EasyListviewItemSelectionChanged(Sender: TCustomEasyListview;
      Item: TEasyItem);
    procedure EasyListviewItemDblClick(Sender: TCustomEasyListview;
      Button: TCommonMouseButton; MousePos: TPoint; HitInfo: TEasyHitInfoItem);
  protected
    FInformes : IBizInforme;
    FOnDblClick: TNotifyEvent;
    FPopupMenu: TPopupMenu;
    
    function GetDblClick: TNotifyEvent; virtual;
    procedure SetDblClick(const Value: TNotifyEvent); virtual;

    function GetPopupMenu: TPopupMenu; virtual;
    procedure SetPopupMenu(const Value: TPopupMenu); virtual;

    function GetInformes: IBizInforme; virtual;
    procedure SetInformes(const Value: IBizInforme); virtual;
  public
    procedure Refresh; override;  
    property Informes: IBizInforme read GetInformes write SetInformes;
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;
    property PopupMenu: TPopupMenu read GetPopupMenu write SetPopupMenu;
  end;

implementation

{$R *.dfm}

{ TfrViewInformes }

function TfrViewInformes.GetInformes: IBizInforme;
begin
  Result := FInformes;
end;

function TfrViewInformes.GetDblClick: TNotifyEvent;
begin
  Result := FOnDblClick;
end;

function TfrViewInformes.GetPopupMenu: TPopupMenu;
begin
  Result := FPopupMenu;
end;

procedure TfrViewInformes.Refresh;
var
  Group: TEasyGroup;
  GroupModificable: TEasyGroup;
  AItem : TEasyItem;
  i: Integer;
  IDSelected : string;
  CategoriaAux : String;
  Opcion: TJsCustomButtonItem;
begin
  inherited;

  if not Assigned(FInformes) then
    Exit;

  EasyListview.BeginUpdate; //  BeginUpdate(False);
  try
    IDSelected := '';
    if EasyListview.Groups.Count > 0 then
    begin
      for i := 0 to EasyListview.Items.Count - 1 do
        if EasyListview.Items[i].Selected then
          IDSelected := EasyListview.Items[i].Captions[3];   // Columna ID
      EasyListview.Groups.Clear;
    end;

{
    Group := EasyListview.Groups.Add;
    Group.Caption := 'Controles generales';
    Group.Bold := True;

    GroupModificable := EasyListview.Groups.Add;
    GroupModificable.Caption := 'Controles personalizados';
    GroupModificable.Bold := True;
}
    with FInformes  do
    begin
      JsNuevoDialog.CustomButtons.Clear;
      FInformes.First;
      for i := 0 to (FInformes.RecordCount - 1) do
      begin
        if CategoriaAux <> FInformes.CATEGORIA then
        begin
          CategoriaAux := FInformes.CATEGORIA;

          Group := EasyListview.Groups.Add;
          Group.Caption := FInformes.CATEGORIA;
          Group.Bold := True;

          Opcion := JsNuevoDialog.CustomButtons.Add;
          Opcion.Caption := Group.Caption;
          Opcion.Info.Add(FInformes.CONTROLLER);
        end;

//        if EsModificable then
          AItem := Group.Items.Add(NIL);
//        else
//          AItem := GroupNoModificable.Items.Add(NIL);

        AItem.ImageIndex := ICONO;
        AItem.Captions[0] := NOMBRE;
        AItem.Captions[1] := DESCRIPCION;
        AItem.Captions[3] := IntToStr(ID);
        AItem.Captions[4] := MODIFICABLE;
        AItem.DetailCount := 2;
        AItem.Details[0] := 0;  // Columna NOMBRE;
        AItem.Details[1] := 1;  // Columna DESCRIPCION;

        AItem.Selected := (IDSelected = AItem.Captions[3]);
        FInformes.Next;
      end;
    end;
  finally
    EasyListview.Sort.SortAll;
    EasyListview.EndUpdate;

    if (Length(IDSelected) = 0) and (EasyListview.Items.Count > 0) then
      EasyListview.Items[0].Selected := True;

    for i := 0 to EasyListview.Items.Count - 1 do
      if EasyListview.Items[i].Selected then
      begin
        FInformes.First;
        FInformes.Locate('ID', StrToInt(EasyListview.Items[i].Captions[3]), []);
        Break;
      end;

    EasyListview.ShowGroupMargins := True;
  end
end;

procedure TfrViewInformes.SetInformes(const Value: IBizInforme);
begin
  FInformes := Value;
  dsDataTable.DataTable := Value.DataTable;
end;

procedure TfrViewInformes.SetDblClick(const Value: TNotifyEvent);
begin
  FOnDblClick := Value;
end;

procedure TfrViewInformes.SetPopupMenu(const Value: TPopupMenu);
begin
  FPopupMenu := Value;
end;

procedure TfrViewInformes.EasyListviewItemContextMenu(
  Sender: TCustomEasyListview; HitInfo: TEasyHitInfoItem;
  WindowPoint: TPoint; var Menu: TPopupMenu; var Handled: Boolean);
begin
  inherited;
  if Assigned(FPopupMenu) then
    Menu := FPopupMenu;
end;

procedure TfrViewInformes.EasyListviewItemDblClick(Sender: TCustomEasyListview;
  Button: TCommonMouseButton; MousePos: TPoint; HitInfo: TEasyHitInfoItem);
begin
  inherited;
  if Assigned(FOnDblClick) then
     FOnDblClick(Self);
end;

procedure TfrViewInformes.EasyListviewItemSelectionChanged(
  Sender: TCustomEasyListview; Item: TEasyItem);
begin
  inherited;
  if not Item.Destroying then
  begin
    FInformes.First;
    FInformes.Locate('ID', StrToInt(Item.Captions[3]), []);
  end;
end;

end.

