unit UCompraTipoRequisicao;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.ScrollBox, FMX.Memo, FMX.ListBox, FMX.DateTimeCtrls, FMX.Edit,
  FMX.Layouts, FMX.TabControl, FMX.Controls.Presentation, System.Actions,
  FMX.ActnList, FMX.Gestures, Data.Bind.EngExt, Fmx.Bind.DBEngExt, System.Rtti,
  System.Bindings.Outputs, Fmx.Bind.Editors, FMX.ListView.Types,
  FMX.ListView.Appearances, FMX.ListView.Adapters.Base, FMX.ListView,
  Data.Bind.Components, Data.Bind.DBScope, Data.DB, Datasnap.DBClient,
  Fmx.Bind.Navigator;

type
  TFCompraTipoRequisicao = class(TFrame)
    VertScrollBox1: TVertScrollBox;
    MainLayout1: TLayout;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    NameList: TListBox;
    firstName: TListBoxItem;
    EditCodigo: TEdit;
    LastName: TListBoxItem;
    EditNome: TEdit;
    TabItem5: TTabItem;
    ToolBar1: TToolBar;
    Button2: TButton;
    Label2: TLabel;
    Button1: TButton;
    GestureManager1: TGestureManager;
    ActionList1: TActionList;
    PreviousTabAction: TPreviousTabAction;
    NextTabAction: TNextTabAction;
    TitleAction: TControlAction;
    ListBoxItem3: TListBoxItem;
    EditDescricao: TEdit;
    TabItem3: TTabItem;
    SterlingStyleBook: TStyleBook;
    ListView1: TListView;
    StyleBook1: TStyleBook;
    Button5: TButton;
    Panel1: TPanel;
    Button3: TButton;
    Button4: TButton;
    ClientDataSet1: TClientDataSet;
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    LinkFillControlToField1: TLinkFillControlToField;
    LinkControlToField1: TLinkControlToField;
    LinkControlToField2: TLinkControlToField;
    LinkControlToField3: TLinkControlToField;
    procedure ActionList1Update(Action: TBasicAction; var Handled: Boolean);
    procedure ListView1ItemClick(const Sender: TObject;
      const AItem: TListViewItem);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
    function FindListBox(const AObj: TFmxObject): TListBox;
    function GetIsFilled(const AList: TListBox): Boolean;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

// pega a ListBox
function TFCompraTipoRequisicao.FindListBox(const AObj: TFmxObject): TListBox;
var
  Obj: TFmxObject;
begin
  Result := nil;
  if AObj.ChildrenCount > 0 then
  begin
    for Obj in AObj.Children do
    begin
      if Obj is TListBox then
        Result := TListBox(Obj)
      else
        Result := FindListBox(Obj);

      if Result <> nil then
        Exit(Result);
    end;
  end;
end;

// verifica se os itens da ListBox estão preenchidos
function TFCompraTipoRequisicao.GetIsFilled(const AList: TListBox): Boolean;
var
  I: Integer;
  Item: TListBoxItem;
  Obj: TFmxObject;
begin
  Result := True;
  for I := 0 to AList.Count - 1 do
  begin
    Item := AList.ItemByIndex(I);
    for Obj in Item.Children do
    begin
      if Obj is TEdit then
        Result := TEdit(Obj).Text <> '';
      if Obj is TDateEdit then
        Result := not TDateEdit(Obj).IsEmpty;
      if Obj is TComboBox then
        Result := TComboBox(Obj).ItemIndex >= 0;

      if not Result then
        Exit(Result);
    end;
    if not Result then
      Exit(Result);
  end;
end;

procedure TFCompraTipoRequisicao.Button3Click(Sender: TObject);
begin
  showmessage('será salvo no servidor');
end;

procedure TFCompraTipoRequisicao.Button4Click(Sender: TObject);
begin
  ClientDataSet1.Cancel;
  TabControl1.SetActiveTabWithTransition(TabItem3, TTabTransition.Slide);
end;

procedure TFCompraTipoRequisicao.Button5Click(Sender: TObject);
begin
  ClientDataSet1.Insert;
  TabControl1.SetActiveTabWithTransition(TabItem1, TTabTransition.Slide);
end;


procedure TFCompraTipoRequisicao.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
begin
  NextTabAction.Execute;
end;

// navega entre as abas e atualiza o título
procedure TFCompraTipoRequisicao.ActionList1Update(Action: TBasicAction; var Handled: Boolean);
var
  LTab: TTabItem;
  LList: TListBox;
begin
  LTab := TabControl1.ActiveTab;

  if Action = NextTabAction then
  begin
    LList := FindListBox(LTab);
    if LList <> nil then
      NextTabAction.Enabled := GetIsFilled(LList);
  end;

  if Action = TitleAction then
    TitleAction.Text := LTab.Text;

  if LTab.Name = 'TabItem3' then
  begin
    Button1.Visible := False;
    Button5.Visible := True;
  end
  else
  begin
    Button1.Visible := True;
    Button5.Visible := False;
  end;
end;

end.
