 unit View.ContactUs;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, XData.Web.Client, XData.Web.Connection, Data.DB,
  XData.Web.JsonDataset, XData.Web.Dataset, Vcl.Controls, Vcl.Grids,
  WEBLib.DBCtrls, WEBLib.DB, WEBLib.Grids, Vcl.StdCtrls, WEBLib.StdCtrls,
  WEBLib.ExtCtrls, WEBLib.CDS, WEBLib.REST;

type
  TFContactUs = class(TWebForm)
    btnHome: TWebButton;
    btnCompliance: TWebButton;
    btnContactUs: TWebButton;
    btnSubmit: TWebButton;
    edtName: TWebEdit;
    edtEmail: TWebEdit;
    memoMessage: TWebMemo;
    edtSubject: TWebEdit;
    XDataWebConnection1: TXDataWebConnection;
    XDataWebClient1: TXDataWebClient;
    procedure WebFormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSubmitClick(Sender: TObject);
    procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
    procedure FContactUsCreate(Sender: TObject);
    procedure XDataWebConnection1Connect(Sender: TObject);
    procedure XDataWebClient1Error(error: TXDataClientError);
    procedure XDataWebConnection1Error(error: TXDataWebConnectionError);
  private
    { Private declarations }
    function IsInputValid: Boolean;
    function IsEmailValid(AEmail: String): Boolean;
    [async] procedure SendEmail;
  public
    { Public declarations }
  end;

var
  FContactUs: TFContactUs;

implementation

{$R *.dfm}

procedure TFContactUs.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
begin
// var
//   basicAuthStr: string;
// begin
//   asm
//     var auth = 'admin:password';
//     basicAuthStr = 'Basic ' + window.btoa(auth);
//   end;

//   Args.Request.Headers.SetValue('Authorization', basicAuthStr);
end;


procedure TFContactUs.XDataWebConnection1Error(error: TXDataWebConnectionError);
begin
  ShowMessage('Unauthorized');
end;


procedure TFContactUs.XDataWebConnection1Connect(Sender: TObject);
begin
  console.log('Connected to XData server');
end;


procedure TFContactUs.XDataWebClient1Error(Error: TXDataClientError);
begin
  console.log('Error: ', Error.ErrorMessage, ' RequestId: ', Error.RequestId, ' Code: ', Error.ErrorCode, ' Request Url: ', Error.RequestUrl);
end;


procedure TFContactUs.FContactUsCreate(Sender: TObject);
begin
  console.log('ContactUs form created');
  XDataWebConnection1.Connected := True;
end;

procedure TFContactUs.btnSubmitClick(Sender: TObject);
begin
  if IsInputValid then
  begin
    SendEmail;
  end
  else
  begin
    ShowMessage('Please complete all fields and ensure the email address is valid.');
  end;
end;

function TFContactUs.IsEmailValid(AEmail: String): Boolean;
begin
  asm
    var regex = new RegExp('^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$');
    @Result = regex.test(AEmail);
  end;
end;

function TFContactUs.IsInputValid: Boolean;
var
  LEmailValid: Boolean;
  LComplete: Boolean;

begin
  LEmailValid := IsEmailValid( edtEmail.Text );

  LComplete := (edtName.Text <> '') and
  (edtSubject.Text <> '') and
  (edtEmail.Text <> '');

  Result := LEmailValid and LComplete;

end;

procedure TFContactUs.SendEmail;
var
  xdcResponse: TXDataClientResponse;
begin
  try
    xdcResponse := await(XDataWebClient1.RawInvokeAsync('IEmailService.SendEmail',
      [edtName.Text, edtEmail.Text, edtSubject.Text, memoMessage.Text]));
    console.log('Email sent successfully');
  except
    on E: EXDataClientRequestException do
      console.log('Error when attempting to send: ' + E.Message);
  end;
end;

procedure TFContactUs.WebFormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

initialization
  RegisterClass(TFContactUs);

end.
