unit View.Artist.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, WEBLib.DB, WEBLib.Controls, WEBLib.ExtCtrls;

type
  TFViewArtistEdit = class(TWebForm)
    btCancel: TWebButton;
    btSave: TWebButton;
    WebLabel1: TWebLabel;
    WebLabel3: TWebLabel;
    DataSource: TWebDatasource;
    btDelete: TWebButton;
    ArtistDataset: TXDataWebDataSet;
    XDataClientArtistGet: TXDataWebClient;
    edName: TWebDBEdit;
    edId: TWebDBEdit;
    ArtistDatasetId: TIntegerField;
    ArtistDatasetName: TStringField;
    XDataClientArtistDelete: TXDataWebClient;
    pnlMessage: TWebPanel;
    lbMessage: TWebLabel;
    WebButton1: TWebButton;
    procedure btCancelClick(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure XDataClientArtistGetLoad(Response: TXDataClientResponse);
    procedure XDataClientArtistDeleteLoad(Response: TXDataClientResponse);
    procedure XDataClientArtistDeleteError(Error: TXDataClientError);
    procedure ArtistDatasetAfterApplyUpdates(Sender: TDataSet;
      Info: TResolveResults);
    procedure WebFormCreate(Sender: TObject);
    procedure WebButton1Click(Sender: TObject);
  private
    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;
  protected
    FInserting: Boolean;
  public
    class function CreateForm(AElementID: string;
      AShowListProc: TListProc; AId: JSValue): TWebForm; overload;
  end;

var
  FViewArtistEdit: TFViewArtistEdit;

implementation

uses
  ConnectionModule;

{$R *.dfm}

{ TFViewArtistEdit }

procedure TFViewArtistEdit.btDeleteClick(Sender: TObject);
begin
  if ArtistDataset.State in dsEditModes then
    ArtistDataset.Post;
  XDataClientArtistDelete.Delete(ArtistDataset.EntitySetName,
    TJSObject(ArtistDataset.CurrentData));
end;

procedure TFViewArtistEdit.btSaveClick(Sender: TObject);
begin
  if ArtistDataset.State in dsEditModes then
  begin
    ArtistDataset.Post;
    ArtistDataset.ApplyUpdates;
  end
  else
    FShowListProc;
end;

class function TFViewArtistEdit.CreateForm(AElementID: string;
  AShowListProc: TListProc; AId: JSValue): TWebForm;
var
  Inserting: Boolean;
begin
  Inserting := isNull(AId);
  Result := InternalCreateForm(AElementID, AShowListProc, Inserting, AId);
end;

procedure TFViewArtistEdit.HiddenNotification;
begin
  pnlMessage.ElementHandle.hidden := True;
end;

procedure TFViewArtistEdit.InsertEntity;
begin
  ArtistDataset.Close;
  ArtistDataset.SetJsonData('{}');
  ArtistDataset.Open;
  ArtistDataset.Insert;
end;

class function TFViewArtistEdit.InternalCreateForm(AElementID: string;
  AShowListProc: TListProc; AInserting: Boolean; AId: JSValue): TWebForm;

  procedure AfterCreate(AForm: TObject);
  begin
    TFViewArtistEdit(AForm).FInserting := AInserting;
    TFViewArtistEdit(AForm).FId := AId;
    TFViewArtistEdit(AForm).FShowListProc := AShowListProc;
    if AInserting then
      TFViewArtistEdit(AForm).InsertEntity
    else
      TFViewArtistEdit(AForm).LoadEntity;
    TFViewArtistEdit(AForm).btDelete.Enabled := not AInserting;
  end;

begin
  Application.CreateForm(TFViewArtistEdit, AElementID, Result, @AfterCreate);
end;

procedure TFViewArtistEdit.LoadEntity;
begin
  XDataClientArtistGet.Get(ArtistDataset.EntitySetName, FId);
end;

procedure TFViewArtistEdit.ShowNotification(Notification: string);
begin
  lbMessage.Caption := Notification;
  pnlMessage.ElementHandle.hidden := False;
end;

procedure TFViewArtistEdit.WebButton1Click(Sender: TObject);
begin
  HiddenNotification;
end;

procedure TFViewArtistEdit.WebFormCreate(Sender: TObject);
begin
  HiddenNotification;
end;

procedure TFViewArtistEdit.XDataClientArtistGetLoad(Response: TXDataClientResponse);
begin
  ArtistDataset.Close;
  ArtistDataset.SetJsonData(Response.Result);
  ArtistDataset.Open;
end;

procedure TFViewArtistEdit.XDataClientArtistDeleteError(
  Error: TXDataClientError);
begin
  ShowNotification(Error.ErrorMessage);
end;

procedure TFViewArtistEdit.XDataClientArtistDeleteLoad(
  Response: TXDataClientResponse);
begin
  FShowListProc;
end;

procedure TFViewArtistEdit.ArtistDatasetAfterApplyUpdates(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.ResolveStatus = TResolveStatus.rsResolveFailed then
    if ResolveInfo.Error <> '' then
    begin
      ShowNotification(ResolveInfo.Error);
      Exit;
    end;
  end;
  FShowListProc;
end;

procedure TFViewArtistEdit.btCancelClick(Sender: TObject);
begin
  FShowListProc;
end;

end.
