unit View.Main;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, WEBLib.ExtCtrls, Vcl.Controls, Vcl.StdCtrls,
  WEBLib.StdCtrls, Data.DB, XData.Web.JsonDataset, XData.Web.Dataset,
  App.Types, ConnectionModule, XData.Web.Client, WEBLib.Menus, Utils, View.Home;

type
  TFViewMain = class(TWebForm)
    wllblLogout: TWebLinkLabel;
    WebPanel1: TWebPanel;
    WebMessageDlg1: TWebMessageDlg;
    WebMemo1: TWebMemo;
    XDataWebClient: TXDataWebClient;
    lblVersion: TWebLabel;
    lblAppTitle: TWebLabel;
    edtTaskIdMain: TWebEdit;
    procedure WebFormCreate(Sender: TObject);
    procedure mnuLogoutClick(Sender: TObject);
    procedure wllblLogoutClick(Sender: TObject);
 private
    { Private declarations }
    FTasksHtmlForm: TWebForm;
    FLogoutProc: TLogoutProc;
    procedure ConfirmLogout;
    procedure LoadTasksHtmlForm;
    procedure LoadHomeForm;
  public
    { Public declarations }
    FUserId: string;
    FTaskId: string;
    FCode: string;
    class procedure Display(LogoutProc: TLogoutProc; const AUserId, ATaskId, ACode: string);
  end;

var
  FViewMain: TFViewMain;

implementation

uses
  Auth.Service,
  View.TasksHTML;

{$R *.dfm}

class procedure TFViewMain.Display(LogoutProc: TLogoutProc; const AUserId, ATaskId, ACode: string);
begin
  if Assigned(FViewMain) then
    FViewMain.Free;

  FViewMain := TFViewMain.CreateNew;
  FViewMain.FLogoutProc := LogoutProc;
  FViewMain.FUserId := AUserId;
  FViewMain.FTaskId := ATaskId;
  FViewMain.FCode := ACode;

  console.log('Main form values assigned after create');
  console.log('UserId=' + FViewMain.FUserId);
  console.log('TaskId=' + FViewMain.FTaskId);
  console.log('Code=' + FViewMain.FCode);
end;

procedure TFViewMain.WebFormCreate(Sender: TObject);
begin
  console.log('TFViewMain.WebFormCreate fired');
  lblAppTitle.Caption := 'emT3web';
  lblVersion.Caption := 'v' + DMConnection.clientVersion;

  console.log('Main form values assigned in webformcreate');
  console.log('UserId=' + FViewMain.FUserId);
  console.log('TaskId=' + FViewMain.FTaskId);
  console.log('Code=' + FViewMain.FCode);

  LoadHomeForm;
//  LoadTasksHtmlForm;
end;



procedure TFViewMain.mnuLogoutClick(Sender: TObject);
begin
  ConfirmLogout;
end;


procedure TFViewMain.wllblLogoutClick(Sender: TObject);
begin
  ConfirmLogout;
end;


procedure TFViewMain.ConfirmLogout;
begin
  ShowConfirmationModal(
    'End this session?.',
    'Yes',
    'No',
    procedure(confirmed: Boolean)
    begin
      if confirmed and Assigned(FLogoutProc) then
        FLogoutProc('');
    end
  );
end;

procedure TFViewMain.LoadTasksHtmlForm;
begin
  if Assigned(FTasksHtmlForm) then
    FTasksHtmlForm.Free;

  console.log('About to create TFTasksHTML, host=' + WebPanel1.ElementID);
  console.log('Main form task id is: ' + FTaskId);

  FTasksHtmlForm := TFTasksHTML.CreateForm(WebPanel1.ElementID, FTaskId);
end;

procedure TFViewMain.LoadHomeForm;
begin
  if Assigned(FTasksHtmlForm) then
    FTasksHtmlForm.Free;

  console.log('About to create TFHome, host=' + WebPanel1.ElementID);
  console.log('Main form task id is: ' + FTaskId);
  console.log('Main form user id is: ' + FUserId);
  console.log('Main form code is: ' + FCode);

  FTasksHtmlForm := TFHome.CreateForm(WebPanel1.ElementID, FTaskId, FUserId, FCode);
end;

end.
