unit uViewObraReserva;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, uBizPedidosProveedor, cxGraphics, dxLayoutControl, cxMemo,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, StdCtrls, cxControls, DB, uDADataTable,
  ActnList, uBizAlmacenes, uAlmacenesController, uPedidosProveedorController,
  Buttons, uDAInterfaces, uBizObras, cxButtonEdit, cxDBEdit,  uObrasController,
  ExtCtrls, ImgList, PngImageList;

type
  IViewObraReserva = interface(IViewBase)
  ['{571462BF-ECF3-47F1-A6F4-2C31175D0B33}']
  end;

  TfrViewObraReserva = class(TfrViewBase, IViewObraReserva)
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutGroup1: TdxLayoutGroup;
    Label1: TLabel;
    dxLayoutControl1Item1: TdxLayoutItem;
    dxLayoutControl1Item2: TdxLayoutItem;
    Button3: TBitBtn;
    PngImageList: TPngImageList;
    ActionList1: TActionList;
    actVerObra: TAction;
    dxLayoutControl1Item3: TdxLayoutItem;
    cbObraReserva: TcxComboBox;
    dxLayoutControl1Group1: TdxLayoutGroup;
    procedure actVerObraExecute(Sender: TObject);
    procedure actVerObraUpdate(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
  private
    FObras : TStringList;
    FObrasController: IObrasController;
    function GetObrasController: IObrasController;
    procedure SetObrasController(const Value: IObrasController);
    function GetObras: TStringList;
    procedure SetObras(const Value: TStringList);

  protected
    procedure SalvarObra;
    procedure SetReadOnly(Value: Boolean); override;

  public
    property Obras: TStringList read GetObras write SetObras;
    property ObrasController: IObrasController read GetObrasController write SetObrasController;
    destructor Destroy; override;
    procedure ActivarObra(const IDObra:Integer);
    function DarIDObraSeleccionada: String;
  end;

implementation
{$R *.dfm}

uses
  Variants, uEditorRegistryUtils, uCustomView, uEditorElegirPersonaContactoPedido,
  uStringsUtils;

procedure TfrViewObraReserva.actVerObraExecute(Sender: TObject);
var
  AObra : IBizObra;
  AID : Variant;
begin
  inherited;
  AID := DarIDObraSeleccionada;
  AObra := FObrasController.Buscar(AID);
  if Assigned(AObra) then
    try
      FObrasController.Ver(AObra);
    finally
      AObra := NIL;
    end;
end;

procedure TfrViewObraReserva.actVerObraUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (cbObraReserva.Properties.Items.Count > 0)
end;

procedure TfrViewObraReserva.ActivarObra(const IDObra: Integer);
var
  i : integer;
begin
  cbObraReserva.ItemIndex := 0;
  for i := 0 to FObras.Count - 1 do
  begin
    if IntToStr(IDObra) = FObras.Values[FObras.Names[i]] then
    begin
      cbObraReserva.ItemIndex := i;
      Exit;
    end;
  end;
end;

procedure TfrViewObraReserva.CustomViewShow(Sender: TObject);
begin
  inherited;
  cbObraReserva.Enabled := not Self.ReadOnly;
end;

function TfrViewObraReserva.DarIDObraSeleccionada: String;
begin
  if Assigned(FObras) then
     Result := FObras.Values[cbObraReserva.EditValue]
  else
     Raise Exception.Create('No se ha asignado ninguna lista de ALMACENES');
end;

destructor TfrViewObraReserva.Destroy;
begin
  FObrasController := Nil;
  FObras := Nil;
  inherited;
end;

function TfrViewObraReserva.GetObras: TStringList;
begin
  Result := FObras;
end;

function TfrViewObraReserva.GetObrasController: IObrasController;
begin
  Result := FObrasController;
end;

procedure TfrViewObraReserva.SalvarObra;
var
  AID : String;
begin
{
  // En el item 0 no hay nada
  if (cbObraReserva.ItemIndex > 0) then
  begin
    AID := FListaObras.Values[cbObraReserva.EditValue];
    FPedido.Edit;
    FPedido.ID_OBRA := StrToInt(AID);
    FPedido.NOMBRE_OBRA := cbObraReserva.EditValue;
  end
  else begin
    FPedido.Edit;
    FPedido.ClearField('ID_OBRA');
    FPedido.ClearField('NOMBRE_OBRA');
  end;
}  
end;

procedure TfrViewObraReserva.SetObras(const Value: TStringList);
var
  i: Integer;

begin
  FObras := Value;

  if Assigned(FObras) then
    with cbObraReserva.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
//        Add('');
        for i := 0 to FObras.Count - 1 do
            Add(FObras.Names[i]);
      finally
        EndUpdate;
      end;
    end;
end;

procedure TfrViewObraReserva.SetObrasController(const Value: IObrasController);
begin
  FObrasController := Value;
end;

procedure TfrViewObraReserva.SetReadOnly(Value: Boolean);
begin
  inherited;
  if (csLoading in Self.ComponentState) then
    Exit;  
  cbObraReserva.Enabled := not Self.ReadOnly;
end;

end.
