unit frmSearchCard;

{$mode Delphi}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, orca_scene2d, untData, untCard, fgl;

type

  { TFormSearchCard }

  TFormSearchCard = class(TForm)
    btnSearchCard: TD2CornerButton;
    btnCancel: TD2CornerButton;
    btnOK: TD2CornerButton;
    D2Resources1: TD2Resources;
    Label1: TD2Label;
    Layout1: TD2Layout;
    Layout2: TD2Layout;
    lstCards: TD2ListBox;
    Root: TD2Background;
    Scene: TD2Scene;
    edtCardName: TD2TextBoxClearBtn;
    procedure btnOKClick(Sender: TObject);
    procedure btnSearchCardClick(Sender: TObject);
    procedure lstCardsClick(Sender: TObject);
  private
    FCardId: Int64;

  public

  published
    property CardId: Int64 read FCardId;
  end;

var
  FormSearchCard: TFormSearchCard;

implementation

uses
  untListItem;

{$R *.lfm}

{ TFormSearchCard }

procedure TFormSearchCard.btnSearchCardClick(Sender: TObject);
var
  keyword: string;
  list: TFPGList<TCard>;
  i: Integer;
  item: TCardListItem;
begin
  keyword:= edtCardName.Text;
  if (keyword = '') then Exit;
  list := Cards.SearchCards(keyword);
  lstCards.Clear;
  lstCards.BeginUpdate;
  for i := 0 to list.Count - 1 do begin
    item := TCardListItem.Create(lstCards);
    item.CardName:= list[i].CardName;
    item.Id:= list[i].CardId;
  end;
  lstCards.EndUpdate;
  FreeCards(list);
  btnOK.Enabled:= False;
  // deselect all
  lstCards.ItemIndex:= -1;
end;

procedure TFormSearchCard.lstCardsClick(Sender: TObject);
begin
  btnOK.Enabled:= lstCards.ItemIndex > -1;
end;

procedure TFormSearchCard.btnOKClick(Sender: TObject);
begin
  FCardId := TCardListItem(lstCards.Items[lstCards.ItemIndex]).Id;
  self.ModalResult:= mrOK;
end;

end.

