unit View.{ENTITY}.Edit;

interface

uses
  System.SysUtils, System.Classes, WEBLib.Graphics, WEBLib.Forms, WEBLib.Dialogs,
  Vcl.Controls, Vcl.StdCtrls, WEBLib.StdCtrls,

  Bcl.Rtti.Common, XData.Web.Client, Data.DB, WEBLib.CDS,
  WEBLib.DBCtrls, XData.Web.JsonDataset, XData.Web.Dataset, JS,

  App.Types, Crud.Utils, WEBLib.DB, WEBLib.Controls, WEBLib.ExtCtrls;

type
  TFView{ENTITY}Edit = class(TWebForm)
    pnlMessage: TWebPanel;
    lbMessage: TWebLabel;
    btCloseMessage: TWebButton;
    DataSource: TWebDatasource;
    btCancel: TWebButton;
{BUTTONSFIELDS}{INPUTCONTROLS}    XDataClient{ENTITY}Get: TXDataWebClient;
    XDataClient{ENTITY}Delete: TXDataWebClient;
{DATASETFIELDS}    procedure btCloseMessageClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
{BUTTONSCLICKDECLARATION}    procedure XDataClient{ENTITY}GetLoad(Response: TXDataClientResponse);
    procedure XDataClient{ENTITY}DeleteLoad(Response: TXDataClientResponse);
    procedure XDataClient{ENTITY}DeleteError(Error: TXDataClientError);
    procedure {ENTITY}DatasetAfterApplyUpdates(Sender: TDataSet;
      Info: TResolveResults);
    procedure WebFormCreate(Sender: TObject);
{ASSOCIATIONSDATASETSEVENTSDECLARATION}  private
    FInserting: Boolean;
    FId: JSValue;
    FShowListProc: TListProc;
    procedure InsertEntity;
    procedure LoadEntity;
    procedure ShowNotification(Notification: string);
    procedure HiddenNotification;
    class function InternalCreateForm(AElementID: string;
      AShowListProc: TListProc; AInserting: Boolean; AId: JSValue): TWebForm; overload;
  public
    class function CreateForm(AElementID: string;
      AShowListProc: TListProc; AId: JSValue): TWebForm; overload;
  end;

var
  FView{ENTITY}Edit: TFView{ENTITY}Edit;

implementation

uses
  ConnectionModule;

{$R *.dfm}

{ TFView{ENTITY}Edit }

{BUTTONSCLICKIMPLEMENTATION}
class function TFView{ENTITY}Edit.CreateForm(AElementID: string;
  AShowListProc: TListProc; AId: JSValue): TWebForm;
var
  Inserting: Boolean;
begin
  Inserting := isNull(AId);
  Result := InternalCreateForm(AElementID, AShowListProc, Inserting, AId);
end;

procedure TFView{ENTITY}Edit.HiddenNotification;
begin
  pnlMessage.ElementHandle.hidden := True;
end;

procedure TFView{ENTITY}Edit.InsertEntity;
begin
  {ENTITY}Dataset.Close;
  {ENTITY}Dataset.SetJsonData('{}');
  {ENTITY}Dataset.Open;
  {ENTITY}Dataset.Insert;
{LOADASSOCIATIONSDATASETS}end;

class function TFView{ENTITY}Edit.InternalCreateForm(AElementID: string;
  AShowListProc: TListProc; AInserting: Boolean; AId: JSValue): TWebForm;

{AFTERCREATEFORMCALLBACK}
begin
  Application.CreateForm(TFView{ENTITY}Edit, AElementID, Result, @AfterCreate);
end;

procedure TFView{ENTITY}Edit.LoadEntity;
var
  QueryString: string;
begin
  QueryString := TCrudUtils.GetEntityGetQueryString(
    {ENTITY}Dataset.Connection.Model.DefaultSchema.FindEntityType(
      {ENTITY}Dataset.EntitySetName
    )
  );
  XDataClient{ENTITY}Get.Get({ENTITY}Dataset.EntitySetName, QueryString, FId);
end;

procedure TFView{ENTITY}Edit.ShowNotification(Notification: string);
begin
  lbMessage.Caption := Notification;
  pnlMessage.ElementHandle.hidden := False;
end;

procedure TFView{ENTITY}Edit.btCloseMessageClick(Sender: TObject);
begin
  HiddenNotification;
end;

procedure TFView{ENTITY}Edit.WebFormCreate(Sender: TObject);
begin
  HiddenNotification;
end;

procedure TFView{ENTITY}Edit.XDataClient{ENTITY}GetLoad(Response: TXDataClientResponse);
begin
  {ENTITY}Dataset.Close;
  {ENTITY}Dataset.SetJsonData(Response.Result);
  {ENTITY}Dataset.Open;
  
  {ENTITY}Dataset.Edit;  
{LOADASSOCIATIONSDATASETS}end;

procedure TFView{ENTITY}Edit.XDataClient{ENTITY}DeleteError(
  Error: TXDataClientError);
begin
  ShowNotification(Error.ErrorMessage);
end;

procedure TFView{ENTITY}Edit.XDataClient{ENTITY}DeleteLoad(
  Response: TXDataClientResponse);
begin
  FShowListProc;
end;

procedure TFView{ENTITY}Edit.{ENTITY}DatasetAfterApplyUpdates(Sender: TDataSet;
  Info: TResolveResults);
var
  I: integer;
  ResolveInfo: TResolveInfo;
begin
  for I := 0 to Length(Info.Records) - 1 do
  begin
    ResolveInfo := Info.Records[I];
    if ResolveInfo.Status = TUpdateStatus.usResolveFailed then
    begin
      ShowNotification(ResolveInfo.Error);
      Exit;
    end;
  end;
  FShowListProc;
end;

procedure TFView{ENTITY}Edit.btCancelClick(Sender: TObject);
begin
  FShowListProc;
end;

{ASSOCIATIONSDATASETSEVENTSIMPLEMENTATION}end.
