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

type
  TFViewTrackEdit = class(TWebForm)
    btCancel: TWebButton;
    btSave: TWebButton;
    WebLabel1: TWebLabel;
    WebLabel3: TWebLabel;
    dsTrack: TWebDatasource;
    btDelete: TWebButton;
    TrackDataset: TXDataWebDataSet;
    XDataClientTrackDelete: TXDataWebClient;
    edName: TWebDBEdit;
    edId: TWebDBEdit;
    WebLabel2: TWebLabel;
    cbGenre: TWebComboBox;
    GenreDataset: TXDataWebDataSet;
    TrackDatasetId: TIntegerField;
    TrackDatasetName: TStringField;
    TrackDatasetComposer: TStringField;
    TrackDatasetMilliseconds: TIntegerField;
    TrackDatasetGenre: TVariantField;
    WebLabel4: TWebLabel;
    edComposer: TWebDBEdit;
    WebLabel5: TWebLabel;
    edMilliseconds: TWebDBEdit;
    GenreDatasetId: TIntegerField;
    GenreDatasetName: TStringField;
    XDataClientTrackGet: TXDataWebClient;
    pnlMessage: TWebPanel;
    lbMessage: TWebLabel;
    WebButton1: TWebButton;
    procedure btCancelClick(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure XDataClientTrackDeleteLoad(Response: TXDataClientResponse);
    procedure GenreDatasetAfterOpen(DataSet: TDataSet);
    procedure XDataClientTrackGetLoad(Response: TXDataClientResponse);
    procedure XDataClientTrackDeleteError(Error: TXDataClientError);
    procedure TrackDatasetAfterApplyUpdates(Sender: TDataSet;
      Info: TResolveResults);
    procedure WebFormCreate(Sender: TObject);
    procedure WebButton1Click(Sender: TObject);
  private
    FId: JSValue;
    FShowListProc: TListProc;
    procedure InsertEntity;
    procedure LoadEntity;
    procedure LoadGenreCombobox;
    procedure PopulateGenreCombobox(Dataset: TDataset);
    procedure SelectGenre;
    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;

  TArtist = class
  private
    FId: integer;
  public
    constructor Create(AId: integer); reintroduce;
    property Id: integer read FId;
  end;

var
  FViewTrackEdit: TFViewTrackEdit;

implementation

uses
  ConnectionModule;

{$R *.dfm}

{ TFViewTrackEdit }

procedure TFViewTrackEdit.btDeleteClick(Sender: TObject);
begin
  if TrackDataset.State in dsEditModes then
    TrackDataset.Post;
  XDataClientTrackDelete.Delete(TrackDataset.EntitySetName,
    TJSObject(TrackDataset.CurrentData));
end;

procedure TFViewTrackEdit.btSaveClick(Sender: TObject);
begin
  if TrackDataset.State in dsEditModes then
  begin
    TrackDataset.FieldByName('Genre').Value := cbGenre.Items.Objects[cbGenre.ItemIndex];
    TrackDataset.Post;
    TrackDataset.ApplyUpdates;
  end
  else
    FShowListProc;
end;

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

procedure TFViewTrackEdit.InsertEntity;
begin
  TrackDataset.Close;
  TrackDataset.SetJsonData('{ }');
  TrackDataset.Open;
  TrackDataset.Insert;
  LoadGenreCombobox;
end;

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

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

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

procedure TFViewTrackEdit.LoadGenreCombobox;
begin
  GenreDataset.QueryString := '$orderby=Name';
  GenreDataset.Load;
end;

procedure TFViewTrackEdit.LoadEntity;
begin
  XDataClientTrackGet.Get(TrackDataset.EntitySetName, '$expand=Genre', FId);
end;

procedure TFViewTrackEdit.PopulateGenreCombobox(Dataset: TDataset);
begin
  cbGenre.Items.Clear;
  Dataset.First;
  while not Dataset.Eof do
  begin
    cbGenre.Items.AddObject(Dataset.FieldByName('Name').AsString, TObject(GenreDataset.ActiveBuffer.Data));
    Dataset.Next;
  end;
end;

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

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

procedure TFViewTrackEdit.TrackDatasetAfterApplyUpdates(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 TFViewTrackEdit.WebButton1Click(Sender: TObject);
begin
  HiddenNotification;
end;

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

procedure TFViewTrackEdit.XDataClientTrackGetLoad(
  Response: TXDataClientResponse);
begin
  TrackDataset.Close;
  TrackDataset.SetJsonData(Response.Result);
  TrackDataset.Open;
  TrackDataset.Edit;
  LoadGenreCombobox;
end;

procedure TFViewTrackEdit.XDataClientTrackDeleteError(Error: TXDataClientError);
begin
  ShowNotification(Error.ErrorMessage);
end;

procedure TFViewTrackEdit.XDataClientTrackDeleteLoad(Response: TXDataClientResponse);
begin
  FShowListProc;
end;

procedure TFViewTrackEdit.GenreDatasetAfterOpen(DataSet: TDataSet);
begin
  PopulateGenreCombobox(Dataset);
  if FInserting then
    cbGenre.ItemIndex := -1
  else
    SelectGenre;
end;

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

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

{ TArtist }

constructor TArtist.Create(AId: integer);
begin
  FId := AId;
end;

end.
