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

type
  TFViewAlbumEdit = class(TWebForm)
    btCancel: TWebButton;
    btSave: TWebButton;
    WebLabel1: TWebLabel;
    WebLabel3: TWebLabel;
    dsAlbum: TWebDatasource;
    btDelete: TWebButton;
    AlbumDataset: TXDataWebDataSet;
    edName: TWebDBEdit;
    edId: TWebDBEdit;
    WebLabel2: TWebLabel;
    cbArtist: TWebComboBox;
    ArtistDataset: TXDataWebDataSet;
    ArtistDatasetId: TIntegerField;
    ArtistDatasetName: TStringField;
    AlbumDatasetId: TIntegerField;
    AlbumDatasetName: TStringField;
    AlbumDatasetArtist: TVariantField;
    TracksDataset: TXDataWebDataSet;
    dsTracks: TWebDatasource;
    WebLabel4: TWebLabel;
    TracksDatasetId: TIntegerField;
    TracksDatasetName: TStringField;
    TracksDatasetComposer: TStringField;
    TracksDatasetMilliseconds: TIntegerField;
    TracksDatasetGenreName: TStringField;
    XDataClientAlbumGet: TXDataWebClient;
    XDataClientAlbumDelete: TXDataWebClient;
    tblTracks: TWebDBTableControl;
    AlbumDatasetTracks: TXDataWebDatasetField;
    pnlMessage: TWebPanel;
    lbMessage: TWebLabel;
    WebButton1: TWebButton;
    procedure btCancelClick(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure ArtistDatasetAfterOpen(DataSet: TDataSet);
    procedure XDataClientAlbumGetLoad(Response: TXDataClientResponse);
    procedure XDataClientAlbumDeleteLoad(Response: TXDataClientResponse);
    procedure XDataClientAlbumDeleteError(Error: TXDataClientError);
    procedure AlbumDatasetAfterApplyUpdates(Sender: TDataSet;
      Info: TResolveResults);
    procedure WebFormCreate(Sender: TObject);
    procedure WebButton1Click(Sender: TObject);
  private
    FId: JSValue;
    FShowListProc: TListProc;
    procedure InsertEntity;
    procedure LoadEntity;
    procedure LoadArtistCombobox;
    procedure PopulateArtistCombobox(Dataset: TDataset);
    procedure SelectArtist;
    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
  FViewAlbumEdit: TFViewAlbumEdit;

implementation

uses
  ConnectionModule;

{$R *.dfm}

{ TFViewAlbumEdit }

procedure TFViewAlbumEdit.btDeleteClick(Sender: TObject);
begin
  if AlbumDataset.State in dsEditModes then
    AlbumDataset.Post;
  XDataClientAlbumDelete.Delete(AlbumDataset.EntitySetName,
    TJSObject(AlbumDataset.CurrentData));
end;

procedure TFViewAlbumEdit.btSaveClick(Sender: TObject);
begin
  if AlbumDataset.State in dsEditModes then
  begin
    AlbumDataset.FieldByName('Artist').Value := cbArtist.Items.Objects[cbArtist.ItemIndex];
    AlbumDataset.Post;
    AlbumDataset.ApplyUpdates;
  end
  else
    FShowListProc;
end;

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

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

procedure TFViewAlbumEdit.InsertEntity;
begin
  AlbumDataset.Close;
  AlbumDataset.SetJsonData('{ }');
  AlbumDataset.Open;
  AlbumDataset.Insert;

  AlbumDatasetTracks.Value := TJSArray.new;

  LoadArtistCombobox;
end;

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

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

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

procedure TFViewAlbumEdit.LoadArtistCombobox;
begin
  ArtistDataset.QueryString := '$orderby=Name';
  ArtistDataset.Load;
end;

procedure TFViewAlbumEdit.LoadEntity;
begin
  XDataClientAlbumGet.Get(AlbumDataset.EntitySetName, '$expand=Artist,Tracks/Genre', FId);
end;

procedure TFViewAlbumEdit.PopulateArtistCombobox(Dataset: TDataset);
begin
  cbArtist.Items.Clear;
  Dataset.First;
  while not Dataset.Eof do
  begin
    cbArtist.Items.AddObject(Dataset.FieldByName('Name').AsString, TObject(ArtistDataset.CurrentData));
    Dataset.Next;
  end;
end;

procedure TFViewAlbumEdit.SelectArtist;
var
  I: Integer;
begin
  for I := 0 to cbArtist.Items.Count - 1 do
  begin
    if TJSObject(cbArtist.Items.Objects[I]).Properties['Id'] = TJSObject(AlbumDatasetArtist.Value).Properties['Id'] then
    begin
      cbArtist.ItemIndex := I;
      Break;
    end;
  end;
end;

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

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

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

procedure TFViewAlbumEdit.XDataClientAlbumGetLoad(
  Response: TXDataClientResponse);
begin
  AlbumDataset.Close;
  AlbumDataset.SetJsonData(Response.Result);
  AlbumDataset.Open;

  TracksDataset.Close;
  TracksDataset.SetJsonData( AlbumDatasetTracks.Value );
  TracksDataset.Open;

  AlbumDataset.Edit;
  LoadArtistCombobox;
end;

procedure TFViewAlbumEdit.XDataClientAlbumDeleteError(Error: TXDataClientError);
begin
  ShowNotification(Error.ErrorMessage);
end;

procedure TFViewAlbumEdit.XDataClientAlbumDeleteLoad(
  Response: TXDataClientResponse);
begin
  FShowListProc;
end;

procedure TFViewAlbumEdit.AlbumDatasetAfterApplyUpdates(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 TFViewAlbumEdit.ArtistDatasetAfterOpen(DataSet: TDataSet);
begin
  PopulateArtistCombobox(Dataset);
  if FInserting then
    cbArtist.ItemIndex := -1
  else
    SelectArtist{(AlbumDatasetArtist.Value)};
end;

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

end.
