unit UProdutoGrupo;

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, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
  FireDAC.DApt.Intf, uDWAbout, uRESTDWPoolerDB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, uDWConstsData, FMX.Menus;

type
  TFProdutoGrupo = class(TFrame)
    VertScrollBox1: TVertScrollBox;
    MainLayout1: TLayout;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    NameList: TListBox;
    lbiNome: TListBoxItem;
    EditNome: TEdit;
    TabItem5: TTabItem;
    ToolBar1: TToolBar;
    Button2: TButton;
    Label2: TLabel;
    Button1: TButton;
    GestureManager1: TGestureManager;
    ActionList1: TActionList;
    PreviousTabAction: TPreviousTabAction;
    NextTabAction: TNextTabAction;
    TitleAction: TControlAction;
    lbiDescricao: TListBoxItem;
    EditDescricao: TEdit;
    TabItem3: TTabItem;
    BindingsList1: TBindingsList;
    SterlingStyleBook: TStyleBook;
    ListView1: TListView;
    StyleBook1: TStyleBook;
    Button5: TButton;
    Panel1: TPanel;
    Button3: TButton;
    Button4: TButton;
    RESTDWClientSQL1: TRESTDWClientSQL;
    BindSourceDB2: TBindSourceDB;
    LinkControlToField9: TLinkControlToField;
    Button6: TButton;
    Panel2: TPanel;
    Button7: TButton;
    LinkControlToField1: TLinkControlToField;
    LinkListControlToField1: TLinkListControlToField;
    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);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
    function FindListBox(const AObj: TFmxObject): TListBox;
    function GetIsFilled(const AList: TListBox): Boolean;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses UDataModule;

// pega a ListBox
function TFProdutoGrupo.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 TFProdutoGrupo.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 TFProdutoGrupo.Button3Click(Sender: TObject);
var
 vError : String;
begin
  RESTDWClientSQL1.Post;

  If Not RESTDWClientSQL1.ApplyUpdates(vError) Then
    MessageDlg(vError, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
  TabControl1.SetActiveTabWithTransition(TabItem3, TTabTransition.Slide);
end;

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

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

procedure TFProdutoGrupo.Button6Click(Sender: TObject);
begin
  RESTDWClientSQL1.Active := False;
  RESTDWClientSQL1.SQL.Clear;
  RESTDWClientSQL1.SQL.Add('select * from produto_grupo');
  try
    RESTDWClientSQL1.Active := True;
  except
    on E: Exception do
    begin
      raise Exception.Create('Erro ao executar a consulta: ' + sLineBreak + E.Message);
    end;
  end;
end;

procedure TFProdutoGrupo.Button7Click(Sender: TObject);
var
 vError : String;
begin
  RESTDWClientSQL1.Delete;

  If Not RESTDWClientSQL1.ApplyUpdates(vError) Then
    MessageDlg(vError, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
  TabControl1.SetActiveTabWithTransition(TabItem3, TTabTransition.Slide);
end;

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

procedure TFProdutoGrupo.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;
    Button6.Visible := True;
    Button5.Visible := True;
  end
  else
  begin
    Button1.Visible := True;
    Button6.Visible := False;
    Button5.Visible := False;
  end;
end;

end.
