unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait, Data.DB,
  FireDAC.Comp.Client, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  FireDAC.DApt, Vcl.Grids, Vcl.DBGrids, FireDAC.Comp.DataSet, Vcl.StdCtrls, Vcl.ExtCtrls,

  //myUnits

  uDBPagination, Vcl.Samples.Spin

  ;

type
  TfMain = class(TForm)
    FDConnection: TFDConnection;
    pTopPanel: TPanel;
    bPrevious: TButton;
    bNext: TButton;
    qMainSelectRequest_WithoutSemiColonsOnTheEnd: TFDQuery;
    DataSource: TDataSource;
    DBGrid1: TDBGrid;
    bFirstPage: TButton;
    qCountOfRecords: TFDQuery;
    lPages: TLabel;
    SpinEdit: TSpinEdit;
    Label1: TLabel;
    procedure bNextClick(Sender: TObject);
    procedure bFirstPageClick(Sender: TObject);
    procedure bPreviousClick(Sender: TObject);
    procedure SpinEditChange(Sender: TObject);
  private
    { Private declarations }

   FDBPagination:TDBPagination;
    procedure SetupDBPaginationModule;



  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;

  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

procedure TfMain.bNextClick(Sender: TObject);
begin

FDBPagination.LoadNextPage();

lPages.Caption:= FDBPagination.CurrentPage.ToString()+' / '+
                 FDBPagination.CountOfPages.ToString;

end;

procedure TfMain.bPreviousClick(Sender: TObject);
begin

FDBPagination.LoadPreviousPage;

lPages.Caption:= FDBPagination.CurrentPage.ToString()+' / '+
                 FDBPagination.CountOfPages.ToString;

end;

procedure TfMain.bFirstPageClick(Sender: TObject);
begin

FDBPagination.LoadFirstPage;

lPages.Caption:= FDBPagination.CurrentPage.ToString()+' / '+
                 FDBPagination.CountOfPages.ToString;


end;

constructor TfMain.Create(AOwner: TComponent);
begin
  inherited;
  SetupDBPaginationModule;
  FDBPagination.LoadFirstPage;

lPages.Caption:= FDBPagination.CurrentPage.ToString()+' / '+
                 FDBPagination.CountOfPages.ToString;

end;

procedure TfMain.SetupDBPaginationModule;
begin

  if not Assigned(FDBPagination) then
  FDBPagination := TDBPagination.Create(Self);

  with qCountOfRecords do
  begin
    Open;
    FDBPagination.CountOfRecords := FieldByName('CountOfRecords').AsLargeInt;
    Close;
  end;

  FDBPagination.FDQuery := qMainSelectRequest_WithoutSemiColonsOnTheEnd;
  // Attention here ! Original statement FDQuery.SQL.Text
  // should be without semicolon
  FDBPagination.InitialSQLText :=qMainSelectRequest_WithoutSemiColonsOnTheEnd.SQL.Text;
  FDBPagination.RecordsOnPage :=SpinEdit.Value;
  FDBPagination.CountNumberOfPages;


end;

procedure TfMain.SpinEditChange(Sender: TObject);
begin

if Assigned(FDBPagination) then begin

if SpinEdit.Value=0 then begin

SpinEdit.Value:=1;
ShowMessage('Value cannot be 0. Number of Records on Page > 0');
Exit;
end;

  FDBPagination.RecordsOnPage :=SpinEdit.Value;
  FDBPagination.CountNumberOfPages;

  bFirstPageClick(Self);

end;



end;

end.
