unit View.ContactUs;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls, WebLib.RegularExpressions,
  WEBLib.Forms, WEBLib.Dialogs, Vcl.StdCtrls, WEBLib.StdCtrls, Vcl.Controls, WEBLib.Menus;

type
  TFContactUs = class(TWebForm)
    btnHome: TWebButton;
    btnCompliance: TWebButton;
    btnContactUs: TWebButton;
    btnSubmit: TWebButton;
    edtName: TWebEdit;
    edtEmail: TWebEdit;
    memoMessage: TWebMemo;
    edtSubject: TWebEdit;
    procedure WebFormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSubmitClick(Sender: TObject);
  private
    { Private declarations }
    function IsInputValid: Boolean;
    function IsEmailValid(AEmail: String): Boolean;
    procedure SendEmail;
  public
    { Public declarations }
  end;

var
  FContactUs: TFContactUs;

implementation

{$R *.dfm}

uses uSmtpJsWrapper;

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;

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

function TFContactUs.IsEmailValid(AEmail: String): Boolean;
const
  CPattern = '^\w+([\.-]?w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$';
begin
  Result := TRegEx.IsMatch( AEmail, CPattern );
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;
begin
  TSmtpJsWrapper.Send(
    'evtestskiclub@gmail.com',
    edtName.Text,
    edtEmail.Text,
    edtSubject.Text,
    memoMessage.Lines.Text
  );
end;

initialization
  RegisterClass(TFContactUs);

end.
