unit View.Track.List;

interface

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

  WEBLib.StdCtrls, Web, JS, WEBLib.Lists,

  App.Types, WEBLib.Controls, WEBLib.Grids, Data.DB,
  XData.Web.Dataset, WEBLib.DBCtrls, WEBLib.DB, WEBLib.ExtCtrls,

  XData.Web.JsonDataset,
  XData.Web.Dataset,

  Grid.Plugins;

type
  TFViewTrackList = class(TWebForm)
    btNew: TWebButton;
    XDataWebDataset: TXDataWebDataSet;
    XDataWebDatasetId: TIntegerField;
    XDataWebDatasetName: TStringField;
    XDataWebDatasetGenreName: TStringField;
    XDataWebDatasetComposer: TStringField;
    XDataWebDatasetMilliseconds: TIntegerField;
    WebDBTableControl1: TWebDBTableControl;
    WebDataSource: TWebDataSource;
    cbPageSize: TWebComboBox;
    edSearch: TWebEdit;
    lbPaginationInfo: TWebLabel;
    lcPaginator: TWebListControl;
    procedure WebFormCreate(Sender: TObject);
    procedure btNewClick(Sender: TObject);
    procedure WebDBTableControl1GetCellChildren(Sender: TObject; ACol,
      ARow: Integer; AField: TField; AValue: string; AElement: TJSHTMLElementRecord);
    procedure cbPageSizeChange(Sender: TObject);
    procedure edSearchChange(Sender: TObject);
  private
    FEditProc: TEditProc;
    FGridPlugin: TGridPlugin;
  public
    class function CreateForm(AElementID: string;
      AShowEditProc: TEditProc): TWebForm;
  end;

var
  FViewTrackList: TFViewTrackList;

implementation

uses
  ConnectionModule;

{$R *.dfm}

{ TFViewTrackList }

procedure TFViewTrackList.cbPageSizeChange(Sender: TObject);
begin
  FGridPlugin.SetPageSize(StrToInt(cbPageSize.Text), True);
end;

class function TFViewTrackList.CreateForm(AElementID: string;
  AShowEditProc: TEditProc): TWebForm;

  procedure AfterCreate(AForm: TObject);
  begin
    TFViewTrackList(AForm).FEditProc := AShowEditProc;
  end;

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

procedure TFViewTrackList.edSearchChange(Sender: TObject);
begin
  FGridPlugin.SetFilterText(edSearch.Text, True);
end;

procedure TFViewTrackList.btNewClick(Sender: TObject);
begin
  FEditProc( nil );
end;

procedure TFViewTrackList.WebDBTableControl1GetCellChildren(Sender: TObject;
  ACol, ARow: Integer; AField: TField; AValue: string;
  AElement: TJSHTMLElementRecord);
var
  RecordId: JSValue;

  procedure EditButtonClick(Sender: TObject);
  begin
    FEditProc(RecordId);
  end;

var
  Button: TWebButton;
begin
  if ARow = 0 then
    Exit;

  if WebDBTableControl1.Columns[ACol].Title = 'Actions' then
  begin
    RecordId := XDataWebDatasetId.Value;
    Button := TWebButton.Create(nil);
    Button.Caption := 'Edit';
    Button.OnClick := @EditButtonClick;
    Button.ElementClassName := 'btn btn-outline btn-default btn-xs';
    Button.ParentElement := AElement.element;
    Button.WidthStyle := TSizeStyle.ssAuto;
    Button.HeightStyle := TSizeStyle.ssAuto;
  end;
end;

procedure TFViewTrackList.WebFormCreate(Sender: TObject);
begin
  XDataWebDataset.QueryString := '$expand=Genre';
  FGridPlugin := TGridPlugin.Create(
    WebDBTableControl1,
    XDataWebDataset,
    lcPaginator,
    lbPaginationInfo
  );
  FGridPlugin.SetPageSize(StrToInt(cbPageSize.Text));
  FGridPlugin.SetFilterText(edSearch.Text);
  FGridPlugin.Load;
end;

end.
