unit unitRoom;

interface

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

type
  TfrmRoom = class(TForm)
    Label1: TLabel;
    lvRoom: TListView;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    txtSearch: TEdit;
    btnSearch: TBitBtn;
    btnAdd: TBitBtn;
    cboBuil: TComboBox;
    PopRef: TPopupMenu;
    ViewOccupants1: TMenuItem;
    EditRoom1: TMenuItem;
    ChangeStatus1: TMenuItem;
    Airconditioned1: TMenuItem;
    WifiConnected1: TMenuItem;
    SingleBed1: TMenuItem;
    DoubleDeck1: TMenuItem;
    procedure btnSearchClick(Sender: TObject);
    procedure ViewOccupants1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
  public
      rn : Integer;
      roomNo : String;
      procedure loadRoom;
      procedure searchRoom;
     // procedure doubleBed;
      procedure loadBuilding;
      function roomStatus(status : Integer) : String;
      
  end;

var
  frmRoom: TfrmRoom;

implementation

uses DM, unitAddRoom, unitRMOcc;

{$R *.dfm}

procedure TfrmRoom.searchRoom;
var
  room : TListItem;
  status : String;
  rmType : String;
  internet : String;
  RMStatus : Integer;
  InternetCon : Integer;
  aircon : Integer;
begin
           if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT b.buil_id,a.ACRONYM,b.RM_NO,b.ISUSABLE,b.MAXNOOCC,b.CURRENTNO,b.RM_LOCATION, b.addedby, b.dateadd FROM BUILDING a, ROOM b WHERE b.BUIL_ID = a.BUIL_ID and b.RM_NO like :a');
          DataModule1.query.Params.AsString[0] := txtSearch.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvRoom.Items.BeginUpdate;
          lvRoom.Items.Clear;

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

                RMStatus:=StrToInt(DataModule1.query.Fields.AsString[3]);
                status:= roomStatus(RMStatus);

                room := lvRoom.Items.Add;
                room.Caption := DataModule1.query.Fields.AsString[0];
                room.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                 room.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                room.SubItems.Add(status);
                room.SubItems.Add(DataModule1.query.Fields.AsString[4]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[5]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[7]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[8]);
                DataModule1.query.Next;
                end;

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


procedure TfrmRoom.loadBuilding;
var
  room : TListItem;
  status : String;
  rmType : String;
  internet : String;
  RMStatus : Integer;
  InternetCon : Integer;
  aircon : Integer;
begin
          if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT b.buil_id,a.ACRONYM,b.RM_NO,b.ISUSABLE,b.MAXNOOCC,b.CURRENTNO,b.RM_LOCATION, b.addedby, b.dateadd FROM BUILDING a, ROOM b WHERE b.BUIL_ID = a.BUIL_ID and a.ACRONYM like :a');
          DataModule1.query.Params.AsString[0] := txtSearch.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvRoom.Items.BeginUpdate;
          lvRoom.Items.Clear;

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

                RMStatus:=StrToInt(DataModule1.query.Fields.AsString[3]);
                status:= roomStatus(RMStatus);

                room := lvRoom.Items.Add;
                room.Caption := DataModule1.query.Fields.AsString[0];
                room.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                 room.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                room.SubItems.Add(status);
                room.SubItems.Add(DataModule1.query.Fields.AsString[4]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[5]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[7]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[8]);
                DataModule1.query.Next;
                end;

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

procedure TfrmRoom.loadRoom;
var
  room : TListItem;
  status : String;
  rmType : String;
  internet : String;
  RMStatus : Integer;
  InternetCon : Integer;
  aircon : Integer;
begin
    if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT b.buil_id,a.ACRONYM,b.RM_NO,b.ISUSABLE,b.MAXNOOCC,b.CURRENTNO,b.RM_LOCATION, b.addedby, b.dateadd FROM BUILDING a, ROOM b WHERE b.BUIL_ID = a.BUIL_ID');
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvRoom.Items.BeginUpdate;
          lvRoom.Items.Clear;

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

                RMStatus:=StrToInt(DataModule1.query.Fields.AsString[3]);
                status:= roomStatus(RMStatus);

                room := lvRoom.Items.Add;
                room.Caption := DataModule1.query.Fields.AsString[0];
                room.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                 room.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                room.SubItems.Add(status);
                room.SubItems.Add(DataModule1.query.Fields.AsString[4]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[5]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[7]);
                room.SubItems.Add(DataModule1.query.Fields.AsString[8]);
                DataModule1.query.Next;
                end;

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

function TfrmRoom.roomStatus(status : Integer) : String;
begin
     case status of
          1 : roomStatus := 'Usable';
          0 : roomStatus := 'Not Usable';
     end;
end;


procedure TfrmRoom.btnSearchClick(Sender: TObject);
begin
       if (cboBuil.Text = 'Building') then
    begin
      loadBuilding;
    end
    else if Length(txtSearch.Text) > 0 then
      begin
          searchRoom;
      end
        else begin
             MessageDlg('Please input first the textbox when searching.',mtInformation,[mbOK],0);
            txtSearch.SetFocus;
      end;
end;

procedure TfrmRoom.ViewOccupants1Click(Sender: TObject);
begin
      frmRMOcc.loadOccupants(StrToInt(lvRoom.ItemFocused.SubItems[1]));
      frmRMOcc.ShowModal;
end;

procedure TfrmRoom.FormShow(Sender: TObject);
begin
     loadRoom;
end;



procedure TfrmRoom.btnAddClick(Sender: TObject);
begin
      frmAddRm.ShowModal;
end;



end.
