unit unitBuilding;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, Buttons, ComCtrls;

type
  TfrmBuilding = class(TForm)
    Label3: TLabel;
    lvBuil: TListView;
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    txtSearch: TEdit;
    btnSearch: TBitBtn;
    btnAdd: TBitBtn;
    PopupMenu1: TPopupMenu;
    procedure btnAddClick(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
      net : Integer;
      procedure loadBldg;
      procedure searchBuilding;
      function isLANReady(internet : Integer) : String;
  end;

var
  frmBuilding: TfrmBuilding;
  systemDate : TDateTime;
  DateTimeformat : string;
implementation

uses DM, unitAddBuilding;

{$R *.dfm}

procedure TfrmBuilding.btnAddClick(Sender: TObject);
begin
       frmAddBuilding.ShowModal;
end;

procedure TfrmBuilding.btnSearchClick(Sender: TObject);
begin
      searchBuilding;
end;

procedure TfrmBuilding.loadBldg;
var
    buil : TListItem;
    internet : String;
    netCon : Integer;
begin
        if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;

           DataModule1.query.SQL.Clear;
           DataModule1.query.SQL.Add('SELECT BUIL_ID, DESCRIPTION, ACRONYM, FLOORCOUNTS,LANREADY FROM BUILDING ORDER BY BUIL_ID');
           DataModule1.query.Prepare;
           DataModule1.query.Open();
           lvBuil.Items.BeginUpdate;
           lvBuil.Items.Clear;

           if DataModule1.query.Fields.IsNull[0] then Exit;
             while not DataModule1.query.Eof do begin

             netCon :=StrToInt(DataModule1.query.Fields.AsString[4]);
             internet := isLANReady(netCon);

             buil :=  lvBuil.Items.Add;
             buil.Caption := DataModule1.query.Fields.AsString[0];
             buil.SubItems.Add(DataModule1.query.Fields.AsString[1]);
             buil.SubItems.Add(DataModule1.query.Fields.AsString[2]);
             buil.SubItems.Add(DataModule1.query.Fields.AsString[3]);
             buil.SubItems.Add(internet);
             DataModule1.query.Next;
            end;

            if DataModule1.tr.InTransaction then
               DataModule1.tr.Commit;
               lvBuil.Items.EndUpdate;
        end;

function TfrmBuilding.isLANReady(internet : Integer) : String;
begin
    case internet of
      1: isLANReady := 'Wifi Connected';
      0: isLANReady := 'Not Connected';
  end;
end;


procedure TfrmBuilding.searchBuilding;
var
    buil : TListItem;
    internet : String;
    netCon : Integer;
begin
        if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT BUIL_ID, DESCRIPTION, ACRONYM, FLOORCOUNTS,LANREADY FROM BUILDING WHERE DESCRIPTION LIKE :A OR ACRONYM LIKE :B ORDER BY BUIL_ID');
          DataModule1.query.Params.AsString[0] := txtSearch.Text;
          DataModule1.query.Params.AsString[1] := txtSearch.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvBuil.Items.BeginUpdate;
          lvBuil.Items.Clear;

          if DataModule1.query.Fields.IsNull[0] then Exit;
             while not DataModule1.query.Eof do begin

             netCon :=StrToInt(DataModule1.query.Fields.AsString[4]);
             internet := isLANReady(netCon);

             buil :=  lvBuil.Items.Add;
             buil.Caption := DataModule1.query.Fields.AsString[0];
             buil.SubItems.Add(DataModule1.query.Fields.AsString[1]);
             buil.SubItems.Add(DataModule1.query.Fields.AsString[2]);
             buil.SubItems.Add(DataModule1.query.Fields.AsString[3]);
             buil.SubItems.Add(internet);
             DataModule1.query.Next;
            end;

            if DataModule1.tr.InTransaction then
               DataModule1.tr.Commit;
               lvBuil.Items.EndUpdate;
        end;

procedure TfrmBuilding.FormShow(Sender: TObject);
begin
      loadBldg;
end;

end.
