unit fGroupsRequest;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Controls.Presentation, FMX.Edit, FMX.Layouts, FMX.ListBox;

type
  TfrGroupsRequest = class(TFrame)
    lbRandomGroup: TListBox;
    lbRandomRespons: TListBox;
    edtRandomGroup: TEdit;
    edtRandomRespons: TEdit;
    btnRandomAdd: TButton;
    btnRandomDel: TButton;
    btnRmGroup: TButton;
    Label4: TLabel;
    Label5: TLabel;
    procedure btnRandomAddClick(Sender: TObject);
    procedure btnRandomDelClick(Sender: TObject);
    procedure btnRmGroupClick(Sender: TObject);
    procedure lbRandomGroupChange(Sender: TObject);
    procedure lbRandomResponsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses ugeneral;

procedure TfrGroupsRequest.btnRandomAddClick(Sender: TObject);
var
  i: Integer;
begin
  i := lbRandomGroup.ItemIndex;
  DB.addGroupResponse(edtRandomGroup.text, edtRandomRespons.text);
  DB.getGroupName(lbRandomGroup.Items);
  if i <> -1 then
  begin
    DB.getGroupResponse(lbRandomGroup.Items[i], lbRandomRespons.Items);
    lbRandomGroup.ItemIndex := i;
  end;
end;

procedure TfrGroupsRequest.btnRandomDelClick(Sender: TObject);
var
  i: Integer;
begin
  i := lbRandomGroup.ItemIndex;
  if (edtRandomGroup.text = '') or (edtRandomRespons.text = '') then
    exit;
  DB.delGroupResponse(edtRandomGroup.text, edtRandomRespons.text);
  DB.getGroupResponse(lbRandomGroup.Items[i], lbRandomRespons.Items);
  lbRandomGroup.ItemIndex := i;
end;

procedure TfrGroupsRequest.btnRmGroupClick(Sender: TObject);
begin
  if lbRandomGroup.ItemIndex = -1 then
    exit;
  DB.delGroupName(lbRandomGroup.Items[lbRandomGroup.ItemIndex]);
  DB.getGroupName(lbRandomGroup.Items);
  lbRandomRespons.Items.Clear;
end;

procedure TfrGroupsRequest.lbRandomGroupChange(Sender: TObject);
begin
  DB.getGroupResponse(lbRandomGroup.Items[lbRandomGroup.ItemIndex],
    lbRandomRespons.Items);
end;

procedure TfrGroupsRequest.lbRandomResponsClick(Sender: TObject);
begin
 if (lbRandomGroup.ItemIndex = -1) or (lbRandomRespons.ItemIndex = -1) then
    exit;
  edtRandomGroup.text := lbRandomGroup.Items[lbRandomGroup.ItemIndex];
  edtRandomRespons.text := lbRandomRespons.Items[lbRandomRespons.ItemIndex];
end;

end.
