unit ViewPixSend;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BodyPixClass, XSuperJSON, XSuperObject;

type
  TViewPixSend = class(TForm)
    GroupBox3: TGroupBox;
    btnConfirmRequest: TButton;
    btnCancelRequest: TButton;
    GroupBox1: TGroupBox;
    txtValue: TEdit;
    Label3: TLabel;
    GroupBox2: TGroupBox;
    txtKeyFav: TEdit;
    Label4: TLabel;
    GroupBox4: TGroupBox;
    Label5: TLabel;
    txtKeyPag: TEdit;
    txtPayerInfo: TEdit;
    Label6: TLabel;
    procedure btnConfirmRequestClick(Sender: TObject);
    procedure btnCancelRequestClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    function GetBody: String;
  public
    property Body:    String read GetBody;
    procedure ClearRequestfields;
  end;

implementation

{$R *.dfm}
        
procedure TViewPixSend.btnCancelRequestClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TViewPixSend.btnConfirmRequestClick(Sender: TObject);
begin

  if txtValue.Text = EmptyStr then
  begin
    MessageDlg('Necessário informar um valor para a cobrança', TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
    if txtValue.CanFocus then
      txtValue.SetFocus;
  end
  else if txtKeyPag.Text = EmptyStr then
  begin
    MessageDlg('A chave do pagador não pode ser vazia', TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
    if txtKeyPag.CanFocus then
      txtKeyPag.SetFocus;
  end
  else if txtKeyFav.Text = EmptyStr then
  begin
    MessageDlg('A chave do favorecido não pode ser vazia', TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
    if txtKeyFav.CanFocus then
      txtKeyFav.SetFocus;
  end
  else
    ModalResult := mrOk;
end;

procedure TViewPixSend.FormCreate(Sender: TObject);
begin
  Self.Position := poScreenCenter;
end;


function TViewPixSend.GetBody: String;
var
  BodyRequest : TBodyPixClass;
  FFavored    : TFavored;
  FPayer      : TPayer;
  FValue      : String;
begin
  BodyRequest := TBodyPixClass.Create;

  FValue := txtValue.Text;

  FFavored.Key := txtKeyFav.Text;
  BodyRequest.Favored := FFavored;

  FPayer.key := txtKeyPag.Text;
  FPayer.PayingInfo := txtPayerInfo.Text;
  BodyRequest.Payer := FPayer;

  BodyRequest.Value := FValue;

  BodyRequest := SO(ClassPixUpdate.AsJSON(False, False));

  if ClassPixUpdate.Calendar = EmptyStr then
    BodyRequest.Remove('calendario');

  if ClassPixUpdate.Status = EmptyStr then
    BodyRequest.Remove('status');

  if (ClassPixUpdate.Debtor.cpf = EmptyStr) or (ClassPixUpdate.Debtor.name = EmptyStr) then
    BodyRequest.Remove('devedor');

  if ClassPixUpdate.Value = EmptyStr then
    BodyRequest.Remove('valor');

  if ClassPixUpdate.PayerRequest = EmptyStr then
    BodyRequest.Remove('solicitacaoPagador');

  if ClassPixUpdate.Key = EmptyStr then
    BodyRequest.Remove('chave');

  if (ClassPixUpdate.ListAddInfo.Count = 0) then
    BodyRequest.Remove('infoAdicionais');

  Result := ClassPixUpdate.AsJSON;
end;

procedure TViewPixSend.ClearRequestfields;
begin
  txtValue.Text := '';
  txtKeyFav.Text := '';
  txtKeyPag.Text := '';
  txtPayerInfo.Text := '';
end;


end.
