unit fSimpleGrid;

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, FMX.Edit, FMX.Grid, FMX.Controls.Presentation,
  FMX.ScrollBox, uRecords;

type
  TfrSimpleGrid = class(TFrame)
    sg: TStringGrid;
    sgR2: TStringColumn;
    sgR1: TStringColumn;
    edtName: TEdit;
    edtFileName: TEdit;
    btnOpen: TButton;
    btnAdd: TButton;
    btnDel: TButton;
    btnEdt: TButton;
    od: TOpenDialog;
    procedure btnAddClick(Sender: TObject);
    procedure btnDelClick(Sender: TObject);
    procedure sgCellClick(const Column: TColumn; const Row: Integer);
    procedure btnOpenClick(Sender: TObject);
    procedure btnEdtClick(Sender: TObject);
  private
    { Private declarations }
    FMyRecord: TArray<TListCommands>;
    FTableName: string;
    procedure SetMyArray(const Value: TArray<TListCommands>);
    procedure SetTableName(const Value: string);

  public
    { Public declarations }
     procedure UpdateGrid;
  published
    property ObjectRecord: TArray<TListCommands> read FMyRecord
      write SetMyArray;
    property TableName: string read FTableName write SetTableName;

  end;

implementation

{$R *.fmx}

uses ugeneral;

{ TfrSimpleGrid }

procedure TfrSimpleGrid.btnAddClick(Sender: TObject);
var
  NewRec: TListCommands;
begin
  if (edtName.Text = '') or (edtFileName.Text = '') then
    exit;
  NewRec.R1 := edtName.Text;
  NewRec.R2 := edtFileName.Text;
  SetLength(FMyRecord, Length(FMyRecord) + 1);
  FMyRecord[High(FMyRecord)] := NewRec;
  DB.SaveRecordArray<TListCommands>(FTableName, FMyRecord);
  edtName.Text := '';
  edtFileName.Text := '';
  UpdateGrid;
end;

procedure TfrSimpleGrid.btnDelClick(Sender: TObject);
var
  i, RowIndex: Integer;
begin
  RowIndex := sg.Row;
  if (RowIndex < 0) or (RowIndex > High(FMyRecord)) then
    exit;
  for i := RowIndex to High(FMyRecord) - 1 do
    FMyRecord[i] := FMyRecord[i + 1];
  SetLength(FMyRecord, Length(FMyRecord) - 1);
  DB.SaveRecordArray<TListCommands>(FTableName, FMyRecord);
  UpdateGrid;
end;

procedure TfrSimpleGrid.btnEdtClick(Sender: TObject);
var RowIndex:integer;
begin
  if (edtName.Text = '') or (edtFileName.Text = '') then
    exit;
  RowIndex := sg.Row;
  if (RowIndex < 0) or (RowIndex > High(FMyRecord)) then
    exit;
  FMyRecord[RowIndex].R1 := edtName.Text;
  FMyRecord[RowIndex].R2 := edtFileName.Text;
  DB.SaveRecordArray<TListCommands>(FTableName, FMyRecord);
  edtName.Text := '';
  edtFileName.Text := '';
  UpdateGrid;
end;

procedure TfrSimpleGrid.btnOpenClick(Sender: TObject);
var
  i, RowIndex: Integer;
begin
  if od.Execute then
    edtFileName.Text := od.FileName;
end;

procedure TfrSimpleGrid.SetMyArray(const Value: TArray<TListCommands>);
begin
  FMyRecord := Value;
end;

procedure TfrSimpleGrid.SetTableName(const Value: string);
begin
  FTableName := Value;
end;

procedure TfrSimpleGrid.sgCellClick(const Column: TColumn; const Row: Integer);
begin
  edtName.Text := sg.Cells[0, Row];
  edtFileName.Text := sg.Cells[1, Row];
end;

procedure TfrSimpleGrid.UpdateGrid;
var
  i: Integer;
begin
  sg.BeginUpdate;
  try
    sg.RowCount := 0; // Сбрасываем строки (оставляем только заголовки)

    for i := 0 to High(FMyRecord) do
    begin
      sg.RowCount := i + 1;
      sg.Cells[0, i] := FMyRecord[i].R1;
      sg.Cells[1, i] := FMyRecord[i].R2;
    end;
  finally
    sg.EndUpdate;
  end;
end;

end.
