unit uNavPaneController;

interface

uses
  Forms, SysUtils, ActnList, uHostManager, Classes, JvNavigationPane, Controls,
  uPantallaPrincipal;

type
  TNavPaneController = class(TObject)
  private
    FNavigationPane: TJvNavigationPane;
    FLargeImages: TImageList;
    FSmallImages: TImageList;
    FMainForm: IMainForm;
    procedure SetNavigationPane(const Value: TJvNavigationPane);
    function CrearPagina(const APageList: TJvNavigationPane;
      ACaption: String; AAction: TAction; AImageIndex: Integer): Integer;
    function EncontrarPagina(const APageList: TJvNavigationPane;
      ACaption: String): Integer;
    function EncontrarSeccionPadre(AParent : TWinControl): TScrollBox;

    function CrearDivisor(AParent: TWinControl;
      ACaption: String) : TJvNavPanelDivider;
    function CrearSeccion(AParent : TWinControl): TScrollBox;
  public
    destructor Destroy; override;
    procedure InitNavPane;
    constructor Create;
    procedure RegisterModule(AModuleInfo : TModuleInfo);    

    property SmallImages: TImageList read FSmallImages write FSmallImages;
    property LargeImages: TImageList read FLargeImages write FLargeImages;

    property NavigationPane : TJvNavigationPane read FNavigationPane
      write SetNavigationPane;

    property MainForm : IMainForm read FMainForm write FMainForm;
  end;

var
  NavPaneController : TNavPaneController;

implementation
{$INCLUDE ..\FactuGES.inc}

uses
  Dialogs, uModuleController, uAcercaDe, uNavPaneUtils,
  uMainMenuController, uClienteUtils, Menus, uDMBase,
  uFactuGES_App, uBizEmpresas, JvPageList;

{ TNavPaneController }

function TNavPaneController.CrearDivisor(AParent: TWinControl;
  ACaption: String): TJvNavPanelDivider;
begin
  Result := TJvNavPanelDivider.Create(AParent);
  with Result do
  begin
    Parent := AParent;
    Caption := ACaption;
    Cursor := crSizeNS;
    Align := alTop;
    Enabled := false;
    Cursor := crDefault;
    StyleManager := dmBase.StyleManager;
  end;
end;

function TNavPaneController.CrearPagina(const APageList : TJvNavigationPane;
  ACaption : String; AAction : TAction; AImageIndex : Integer): Integer;
var
  APanePage: TJvNavPanelPage;
begin
  Result := 0;
  APanePage := TJvNavPanelPage.Create(Application);
  with APanePage do
  begin
    Caption := StringReplace(ACaption, '&', '', []);
    Action := AAction;
    PageList := APageList;
    Iconic := False;
    ImageIndex := AImageIndex;
    Result := PageIndex;
  end;
end;

constructor TNavPaneController.Create;
begin
  inherited;
end;

function TNavPaneController.CrearSeccion(AParent : TWinControl): TScrollBox;
begin
  Result := TScrollBox.Create(AParent);
  with Result do
  begin
    Parent := AParent;
    BorderStyle := bsNone;
  end;
end;

procedure TNavPaneController.InitNavPane;
var
  AListaSecciones : TStringList;
  AEmpresasCount: Integer;
  AIndex : Integer;
  ADivisor :TJvNavPanelDivider;
  ASeccion : TScrollBox;
  ASeccionPadre : TScrollBox;
  ASeccionCount: Integer;
  AEmpresas : IBizEmpresa;
begin
  AListaSecciones := TStringList.Create;
  AListaSecciones.Duplicates := dupIgnore;
  AEmpresasCount := 0;

  try
    // Guardar la lista de secciones (Ventas, Compras, etc...)
    for ASeccionCount := 0 to MainMenuController.MainMenu.Items.Count - 1 do
    begin
      if (not EsUnModulo(MainMenuController.MainMenu.Items[ASeccionCount]))
      or (not EsModuloVisible(MainMenuController.MainMenu.Items[ASeccionCount])) then
        Continue;
      AListaSecciones.AddObject(StringReplace(MainMenuController.MainMenu.Items[ASeccionCount].Caption, '&', '', []),
        MainMenuController.MainMenu.Items[ASeccionCount])
    end;

    // Crear páginas con empresas y rellenarlas
    AEmpresas := AppFactuGES.EmpresasController.BuscarTodos(AppFactuGES.UsuarioActivo.ID_PERFIL);;
    try
      AEmpresas.DataTable.Active := True;

      {$IFDEF MULTIEMPRESA}
      while not AEmpresas.DataTable.EOF do
      begin
      {$ENDIF}

//        ASeccionPadre := NIL;
//        AIndex := -1;

        // Buscar la página si existe
        AIndex := EncontrarPagina(FNavigationPane, AEmpresas.NOMBRE);
        if AIndex > -1 then
          ASeccionPadre := EncontrarSeccionPadre(FNavigationPane.NavPages[AIndex])
        else begin
          // Crear la página de la empresa
          AIndex := CrearPagina(FNavigationPane, AEmpresas.NOMBRE, nil, -1);

          ASeccionPadre := CrearSeccion(FNavigationPane.NavPages[AIndex]);
          ASeccionPadre.Align := alClient;
          ASeccionPadre.AutoScroll := True;
        end;
        FNavigationPane.NavPages[AIndex].Tag := AEmpresas.ID;


        // Crear secciones
        for ASeccionCount := 0 to AListaSecciones.Count - 1 do
        begin
          ASeccion := CrearSeccion(ASeccionPadre);
          with ASeccion do
          begin
            Top := 100 * AEmpresasCount;
            Align := alTop;
            AutoScroll := False;
          end;

          ADivisor := CrearDivisor(ASeccion, AListaSecciones[ASeccionCount]);

          PopulateNavPagePane(ASeccion, TMenuItem(AListaSecciones.Objects[ASeccionCount]),
            FLargeImages, dmBase.StyleManager);
          ASeccion.Height := ASeccion.Height + ADivisor.Height;
        end;

      {$IFDEF MULTIEMPRESA}
        AEmpresas.DataTable.Next;
      end;
      {$ENDIF}

      AEmpresas.DataTable.Active := False;
    finally
      AEmpresas := NIL;
    end;
  finally
    FreeAndNIL(AListaSecciones);
  end;
end;

procedure TNavPaneController.RegisterModule(AModuleInfo: TModuleInfo);
begin
  // Por si es necesario en el furuto
end;

destructor TNavPaneController.Destroy;
begin
  inherited;
end;

function TNavPaneController.EncontrarPagina(const APageList: TJvNavigationPane;
  ACaption: String): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to APageList.PageCount - 1 do
    if APageList.NavPages[i].Caption = ACaption then
    begin
      Result := i;
      Break;
    end;
end;

function TNavPaneController.EncontrarSeccionPadre(
  AParent: TWinControl): TScrollBox;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to AParent.ControlCount - 1 do
    if AParent.Controls[i] is TScrollBox then
    begin
      Result := (AParent.Controls[i] as TScrollBox);
      Break;
    end;
end;

procedure TNavPaneController.SetNavigationPane(const Value: TJvNavigationPane);
begin
  FNavigationPane := Value;
end;


end.
