unit fLog;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  System.Rtti, FMX.Grid.Style, System.Generics.Collections, FMX.Grid,
  FMX.ScrollBox,
  FMX.Controls.Presentation, uRecords;

type
  TfrLog = class(TFrame)
    Panel1: TPanel;
    btnClear: TButton;
    chkWARNING: TCheckBox;
    chkERROR: TCheckBox;
    chkDEBUG: TCheckBox;
    chkINFO: TCheckBox;
    sgLog: TStringGrid;
    StringColumn5: TStringColumn;
    StringColumn1: TStringColumn;
    StringColumn2: TStringColumn;
    StringColumn3: TStringColumn;
    StringColumn4: TStringColumn;
    procedure btnClearClick(Sender: TObject);
    procedure chkWARNINGChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FLogList: TList<TRLog>;
    destructor Destroy; override;
    procedure UpdateGridFilters;
  end;

implementation

{$R *.fmx}
{ TfrLog }

procedure TfrLog.chkWARNINGChange(Sender: TObject);
begin
  UpdateGridFilters;
end;

destructor TfrLog.Destroy;
begin
  FLogList.Free;
  inherited;
end;

procedure TfrLog.btnClearClick(Sender: TObject);
begin
  FLogList.Clear;
  UpdateGridFilters;
end;

procedure TfrLog.UpdateGridFilters;
var
  ml: TRLog;
  NewRow: integer;
begin
  sgLog.BeginUpdate;
  try
    sgLog.RowCount := 0;
    // Проверка наличия всех колонок
    if sgLog.ColumnCount < 5 then
      raise Exception.Create('Грид должен содержать 5 колонок.');
    for ml in FLogList do
    begin
      // Проверка фильтра
      if ((ml.rType = 'WARNING') and chkWARNING.IsChecked) or
        ((ml.rType = 'ERROR') and chkERROR.IsChecked) or
        ((ml.rType = 'DEBUG') and chkDEBUG.IsChecked) or
        ((ml.rType = 'INFO') and chkINFO.IsChecked) then
      begin
        sgLog.RowCount := sgLog.RowCount + 1;
        NewRow := sgLog.RowCount - 1;
        // Заполнение данных с проверкой колонок
        sgLog.Cells[0, NewRow] := TimeToStr(ml.rTime); // Колонка 0
        sgLog.Cells[1, NewRow] := ml.rType; // Колонка 1
        sgLog.Cells[2, NewRow] := ml.rModule; // Колонка 2
        sgLog.Cells[3, NewRow] := ml.rMethod; // Колонка 3
        sgLog.Cells[4, NewRow] := ml.rMessage; // Колонка 4
      end;
    end;
  finally
    sgLog.EndUpdate;
  end;
end;

end.
