unit UCompraRequisicaoD;

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, Data.Bind.Controls,
  UCompraRequisicaoM;

type
  TFCompraRequisicaoD = class(TFrame)
    VertScrollBox1: TVertScrollBox;
    MainLayout1: TLayout;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    NameList: TListBox;
    ListBoxItem2: TListBoxItem;
    Edit2: TEdit;
    TabItem5: TTabItem;
    ToolBar1: TToolBar;
    Button2: TButton;
    Label2: TLabel;
    Button1: TButton;
    GestureManager1: TGestureManager;
    ActionList1: TActionList;
    PreviousTabAction: TPreviousTabAction;
    NextTabAction: TNextTabAction;
    TitleAction: TControlAction;
    TabItem3: TTabItem;
    SterlingStyleBook: TStyleBook;
    ListView1: TListView;
    StyleBook1: TStyleBook;
    Button5: TButton;
    Panel1: TPanel;
    Button3: TButton;
    Button4: TButton;
    RESTDWClientSQL1: TRESTDWClientSQL;
    Button6: TButton;
    Panel2: TPanel;
    Button7: TButton;
    ListBoxItem1: TListBoxItem;
    Layout2: TLayout;
    Edit1: TEdit;
    Button9: TButton;
    BindSourceDB1: TBindSourceDB;
    LinkControlToFieldQUANTIDADE: TLinkControlToField;
    BindingsList1: TBindingsList;
    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);
    procedure Button9Click(Sender: TObject);
  private
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses UDataModule, UPegaSubGrupo, UPegaUnidade, UPegaProduto, MainFrmCompras;

procedure TFCompraRequisicaoD.Button3Click(Sender: TObject);
var
 vError : String;
begin
  RESTDWClientSQL1.FieldByName('ID_COMPRA_REQUISICAO').AsString := (MainFrmCompras.FrameAtual as TFCompraRequisicaoM).IDSelecionado;
  RESTDWClientSQL1.FieldByName('ID_PRODUTO').AsString := Copy(Edit1.Text, 1, pos('|', Edit1.Text)-1);

  RESTDWClientSQL1.Post;

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

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

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

procedure TFCompraRequisicaoD.Button6Click(Sender: TObject);
begin
  RESTDWClientSQL1.Active := False;
  RESTDWClientSQL1.SQL.Clear;
  RESTDWClientSQL1.SQL.Add('select * from compra_requisicao_detalhe where ID_COMPRA_REQUISICAO = ' + (MainFrmCompras.FrameAtual as TFCompraRequisicaoM).IDSelecionado);
  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 TFCompraRequisicaoD.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 TFCompraRequisicaoD.Button9Click(Sender: TObject);
begin
  Application.CreateForm(TFPegaProduto, FPegaProduto);
  FPegaProduto.ShowModal;
  Edit1.Text := FPegaProduto.Selecionado;
end;

procedure TFCompraRequisicaoD.ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
begin
  //pega dados produto
  FPegaProduto.RESTDWClientSQL1.Active := False;
  FPegaProduto.RESTDWClientSQL1.SQL.Clear;
  FPegaProduto.RESTDWClientSQL1.SQL.Add('select * from produto where id=' + RESTDWClientSQL1.FieldByName('ID_PRODUTO').AsString);
  try
    FPegaProduto.RESTDWClientSQL1.Active := True;
  except
    on E: Exception do
    begin
      raise Exception.Create('Erro ao executar a consulta: ' + sLineBreak + E.Message);
    end;
  end;
  Edit1.Text := FPegaProduto.RESTDWClientSQL1.FieldByName('ID').AsString + '|' + FPegaProduto.RESTDWClientSQL1.FieldByName('NOME').AsString;

  // Necessário porque o campo de lookup não tem binding e se o usuário alterar apenas ele ocorrerá um erro de edit not in edit or insert mode
  RESTDWClientSQL1.Edit;

  NextTabAction.Execute;
end;

procedure TFCompraRequisicaoD.ActionList1Update(Action: TBasicAction; var Handled: Boolean);
var
  LTab: TTabItem;
  LList: TListBox;
begin
  LTab := TabControl1.ActiveTab;

  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.
