unit ufrm.principal;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, REST.Types, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, Vcl.Grids, Vcl.DBGrids,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, REST.Response.Adapter, REST.Client,
  Data.Bind.Components, Data.Bind.ObjectScope, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.ComCtrls, Vcl.DBCtrls, uClass.FunionalCard.Api;

type
  TfrmPrincipal = class(TForm)
    RESTRequest: TRESTRequest;
    RESTResponse: TRESTResponse;
    RESTResponseDataSetAdapter: TRESTResponseDataSetAdapter;
    FDMemTable: TFDMemTable;
    dsFDMemTable: TDataSource;
    FDMemTableITENS: TFDMemTable;
    dsFDMemTableITENS: TDataSource;
    RESTClient: TRESTClient;
    dsFDMemTableReceita: TDataSource;
    FDMemTableReceita: TFDMemTable;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    memoKey: TMemo;
    Button1: TButton;
    DBGrid2: TDBGrid;
    Label4: TLabel;
    Label3: TLabel;
    DBGrid3: TDBGrid;
    DBGrid1: TDBGrid;
    Button2: TButton;
    cmbOrigem: TComboBox;
    edtData: TDateTimePicker;
    edtAutorizacao: TLabeledEdit;
    edtCartao: TLabeledEdit;
    edtCNPJ: TLabeledEdit;
    Label2: TLabel;
    Label1: TLabel;
    edtNumSequencil: TLabeledEdit;
    edtUsuario: TLabeledEdit;
    edtPassword: TLabeledEdit;
    Button3: TButton;
    Button4: TButton;
    edtValor: TLabeledEdit;
    edtEndPoint: TLabeledEdit;
    FDMemtableCabecalhoVenda: TFDMemTable;
    dsFDMemtableCabecalhoVenda: TDataSource;
    FDMemTableItensVenda: TFDMemTable;
    dsFDMemTableItensVenda: TDataSource;
    TabSheet3: TTabSheet;
    Label7: TLabel;
    Label6: TLabel;
    DBGrid4: TDBGrid;
    Label5: TLabel;
    DBGrid5: TDBGrid;
    FDMemTableComprovante: TFDMemTable;
    dsFDMemTableComprovante: TDataSource;
    Label8: TLabel;
    DBMemo1: TDBMemo;
    DBGrid6: TDBGrid;
    Memo1: TMemo;
    RESTRequest1: TRESTRequest;
    RESTResponse1: TRESTResponse;
    RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
    FDMemTable1: TFDMemTable;
    dsFDMemTable1: TDataSource;
    DBGrid7: TDBGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dsFDMemTableITENSDataChange(Sender: TObject; Field: TField);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPrincipal: TfrmPrincipal;
  oFuncionalCard :TFuncionalCardAPI;

implementation

{$R *.dfm}

procedure TfrmPrincipal.Button1Click(Sender: TObject);
begin

   RESTClient.BaseURL := edtEndPoint.Text;

   oFuncionalCard.getLogin(FDMemTable, edtUsuario.Text, edtPassword.Text);
   memoKey.Lines.Add(oFuncionalCard.ChaveLogin);
   RESTResponseDataSetAdapter.Active := False;

end;

procedure TfrmPrincipal.Button2Click(Sender: TObject);
begin

  RESTResponseDataSetAdapter.Active := False;
  RESTResponseDataSetAdapter.Dataset := FDMemTable;

  oFuncionalCard.getResgatarPreAutorizacao(edtCNPJ.Text, edtCartao.Text, edtAutorizacao.Text, cmbOrigem.Text[1],  edtData.DateTime);

  oFuncionalCard.setRetornaItens(FDMemTableITENS);

end;

procedure TfrmPrincipal.Button3Click(Sender: TObject);
begin
  RESTResponseDataSetAdapter1.Active := False;
//  RESTResponseDataSetAdapter1.RootElement := '';
//  RESTResponseDataSetAdapter1.StringFieldSize := 1000;

  oFuncionalCard.getConfirmarPreAutorizacao(edtAutorizacao.Text, edtCNPJ.Text, edtCartao.Text,cmbOrigem.Text[1], edtData.DateTime);


  if (FDMemTable1.Active and  FDMemTable1.FieldByName('IsValid').AsBoolean)    then
  begin

    //oFuncionalCard.setRetornaNo('Transacao',FDMemtableCabecalhoVenda);
    RESTResponseDataSetAdapter1.Dataset         := FDMemtableCabecalhoVenda;
    RESTResponseDataSetAdapter1.RootElement     := 'Transacao';
    RESTResponseDataSetAdapter1.StringFieldSize := 250;


    //oFuncionalCard.setRetornaNo('Transacao.Itens',FDMemTableItensVenda,100);
    RESTResponseDataSetAdapter1.Dataset         := FDMemTableItensVenda;
    RESTResponseDataSetAdapter1.RootElement     := 'Transacao.Itens';
    RESTResponseDataSetAdapter1.StringFieldSize := 250;



    //oFuncionalCard.setRetornaNo('Comprovante',FDMemTableComprovante,1000);
    RESTResponseDataSetAdapter1.Dataset         := FDMemTableComprovante;
    RESTResponseDataSetAdapter1.RootElement     := 'Comprovante';
    RESTResponseDataSetAdapter1.StringFieldSize := 1000;

    DBMemo1.DataField := 'JSONString';

    edtValor.Text        := FDMemtableCabecalhoVenda.FieldByName('ValorTotal').AsString;
    edtAutorizacao.Text  := FDMemtableCabecalhoVenda.FieldByName('NumeroAutorizacao').AsString;
    edtNumSequencil.Text := FDMemtableCabecalhoVenda.FieldByName('NumeroSequencial').AsString;


  end;

  PageControl1.ActivePage := TabSheet3;





end;

procedure TfrmPrincipal.Button4Click(Sender: TObject);
begin
  oFuncionalCard.getCanelarAutorizacao(edtCNPJ.Text,edtAutorizacao.Text, edtNumSequencil.Text, cmbOrigem.Text[1], edtCartao.Text, edtValor.text, edtData.Datetime);

  ShowMessage( RESTResponse.Content);


end;

procedure TfrmPrincipal.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 //if not odd(FDMemTableITENS.RecNo) then
   if Pos('"Aprovado":true',FDMemTableITENS.FieldByName('status').AsString)>0 then
   begin
     DBGrid2.Canvas.Brush.Color := clMoneyGreen;
     DBGrid2.Canvas.FillRect(Rect);
     DBGrid2.DefaultDrawDataCell(rect,Column.Field,state);
   end else
   begin
     DBGrid2.Canvas.Brush.Color := clRed;
     DBGrid2.Canvas.FillRect(Rect);
     DBGrid2.DefaultDrawDataCell(rect,Column.Field,state);
   end;
end;

procedure TfrmPrincipal.dsFDMemTableITENSDataChange(Sender: TObject; Field: TField);
begin
  try
    oFuncionalCard.setRetornaReceita(FDMemTable.RecNo-1, FDMemTableReceita);
  except
    try
       oFuncionalCard.setRetornaReceita(FDMemTable.RecNo-1, FDMemTableReceita,'Transacao.');
    except

    end;
  end;
end;

procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
  oFuncionalCard := TFuncionalCardAPI.Create;

  edtData.DateTime := Now;

end;

end.
