unit uEditorInformes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBBase, Menus, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TB2ExtItems, TBXExtItems,
  TBX, TB2Item, TB2Dock, TB2Toolbar, ExtCtrls, JvExControls, JvComponent,
  JvNavigationPane, pngimage, uBizInformes, uEditorBase,
  uViewInformes, JvExComCtrls, JvStatusBar, uIEditorInformes, uInformesController,
  uCustomView, uViewBase, JSDialog;

type
  TfEditorInformes = class(TfEditorDBBase, IEditorInformes)
    ViewPopupMenu: TPopupMenu;
    Modificar1: TMenuItem;
    Eliminar1: TMenuItem;
    N1: TMenuItem;
    Nuevo1: TMenuItem;
    pngImageNuevo: TPngImageList;
    actPropiedades: TAction;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem7: TTBXItem;
    N2: TMenuItem;
    Propiedades1: TMenuItem;
    frViewInformes1: TfrViewInformes;
    procedure actModificarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure actPropiedadesExecute(Sender: TObject);
    procedure actPropiedadesUpdate(Sender: TObject);

  private
    procedure ComprobarPrivilegios;

  protected
    FInformes : IBizInforme;
    FController : IInformesController;
    FViewInformes : IViewInformes;
    function GetInformes: IBizInforme;
    procedure SetInformes(const Value: IBizInforme);
    function GetController: IInformesController;
    procedure SetController(const Value: IInformesController);

    procedure NuevoInterno; override;
    procedure EliminarInterno; override;
    procedure RefrescarInterno; override;

  public
    property Informes: IBizInforme read GetInformes write SetInformes;
    property Controller: IInformesController read GetController write SetController;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

{ TfEditorInformes }

uses uDataModuleUsuarios;

function TfEditorInformes.GetController: IInformesController;
begin
  Result := FController;
end;

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

procedure TfEditorInformes.NuevoInterno;
var
  Respuesta: integer;
  Opcion: integer;
  i: integer;
begin
  try
    Respuesta := frViewInformes1.JsNuevoDialog.Execute;
    if Respuesta <> IDCANCEL then
    begin
      frViewInformes1.JsNuevoDialog.CustomButtonResult;
      Opcion := frViewInformes1.JsNuevoDialog.CustomButtonResult;
      for i := 0 to frViewInformes1.JsNuevoDialog.CustomButtons.Count - 1 do
          if (frViewInformes1.JsNuevoDialog.CustomButtons.Items[i].Value = Opcion) then
             break;

      FController.Nuevo(frViewInformes1.JsNuevoDialog.CustomButtons.Items[i].Info.Strings[0], FInformes);
//      showmessage('Opcion seleccionada: ' + frViewInformes1.JsNuevoDialog.CustomButtons.Items[i].Info.Strings[0]);
    end;
  finally
    FViewInformes.Refresh;
  end;
end;

procedure TfEditorInformes.RefrescarInterno;
begin
  inherited;
  if Assigned(FViewInformes) then
     FViewInformes.Refresh;
end;

procedure TfEditorInformes.SetController(const Value: IInformesController);
begin
  FController := Value;
end;

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

  ComprobarPrivilegios;
  
  if Assigned(FViewInformes) then
  begin
     FViewInformes.Informes := FInformes;
  end;
end;

procedure TfEditorInformes.actModificarExecute(Sender: TObject);
begin
  inherited;
  try
    FController.Ver(FInformes);
  finally
    FViewInformes.Refresh;
  end;
end;

procedure TfEditorInformes.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(FInformes) then
    raise Exception.Create('No hay ninguna informe asociado');

  if not Assigned(FViewInformes) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(FController) then
    raise Exception.Create('No hay ningún Controller asignado');

  FViewInformes.OnDblClick := actModificar.OnExecute;
  FViewInformes.PopupMenu := ViewPopupMenu;

  Informes.DataTable.Active := True;
  FViewInformes.Refresh;
end;

destructor TfEditorInformes.Destroy;
begin
  FViewInformes := NIL;
  FController := NIL;
  FInformes := NIL;  
  inherited;
end;

procedure TfEditorInformes.EliminarInterno;
begin
//    if not FController.EsModificable then
//      ShowMessage('Este Controller no se puede borrar')

  if (Application.MessageBox('¿Desea borrar este informe?', 'Atención', MB_YESNO) = IDYES) then
  begin
    FController.Eliminar(FInformes);
    FViewInformes.Refresh;
  end;
end;

procedure TfEditorInformes.actPropiedadesExecute(Sender: TObject);
begin
  inherited;
{
  if Assigned(FController) then
    try
      FController.VerPropiedades;
      actGuardar.Execute;
    finally
      FViewController.Refresh;
    end;
}    
end;

procedure TfEditorInformes.actPropiedadesUpdate(Sender: TObject);
begin
  inherited;
{
  if Assigned(dsDataTable.DataTable) then
    (Sender as TAction).Enabled := (not dsDataTable.DataTable.IsEmpty)
      and not (dsDataTable.DataTable.State = dsInsert)
  else
    (Sender as TAction).Enabled := False;
}    
end;

procedure TfEditorInformes.ComprobarPrivilegios;
begin
  if not dmUsuarios.EsAdministrador then
  begin
     actNuevo.Enabled := False;
     actNuevo.Visible := False;
     actEliminar.Enabled := False;
     actEliminar.Visible := False;
     actPropiedades.Enabled := False;
     actPropiedades.Visible := False;
  end;
end;

constructor TfEditorInformes.Create(AOwner: TComponent);
begin
  inherited;
  FViewInformes := frViewInformes1;
  actModificar.ShortCut := ShortCut(VK_RETURN, []);
end;

end.
