unit unitAddRoom;

interface

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

type
  TfrmAddRm = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    txtRMNo: TEdit;
    txtOcc: TEdit;
    cmbBldg: TComboBox;
    txtLoc: TEdit;
    btnCancel: TBitBtn;
    btnSave: TBitBtn;
    Label7: TLabel;
    cmbType: TComboBox;
    Edit1: TEdit;
     procedure FormShow(Sender: TObject);
    procedure cmbBldgChange(Sender: TObject);
    procedure cmbBldgKeyPress(Sender: TObject; var Key: Char);
    procedure txtRMNoKeyPress(Sender: TObject; var Key: Char);
    procedure txtOccKeyPress(Sender: TObject; var Key: Char);
    procedure txtLocKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure btnSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cmbTypeChange(Sender: TObject);
   
  private
    { Private declarations }
  public
            airCon : String;
            wifi : String;
            room : String;
            bed: String;
            builID : Integer;
            typ : Integer;
            procedure save;
            procedure clear;
            procedure loadType;
            procedure loadBuilding;
    { Public declarations }
  end;

var
  frmAddRm: TfrmAddRm;

implementation

uses DM, unitLogin, unitRoom;

{$R *.dfm}

procedure TfrmAddRm.loadBuilding;
begin
if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('SELECT DESCRIPTION FROM BUILDING');
        DataModule1.query.Prepare;
        DataModule1.query.Open();
        cmbBldg.Items.BeginUpdate;
        cmbBldg.Clear;

        If DataModule1.query.Fields.IsNull[0] then Exit
          else begin
          while not DataModule1.query.Eof do begin
           cmbBldg.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
           end;
        end;

            If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cmbBldg.Items.EndUpdate;
end;

procedure TfrmAddRm.FormShow(Sender: TObject);
begin
    loadBuilding;
    loadType;
end;
procedure TfrmAddRm.loadType;
begin
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select DESC');
         DataModule1.query.SQL.Add('From RM_TYPE');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cmbType.Items.BeginUpdate;
         cmbType.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cmbType.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cmbType.Items.EndUpdate;
end;

procedure TfrmAddRm.cmbBldgChange(Sender: TObject);
begin
       if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT BUIL_ID') ;
          DataModule1.query.SQL.Add('FROM BUILDING');
          DataModule1.query.SQL.Add('WHERE DESCRIPTION = :a');
          DataModule1.query.Params.AsString[0] := cmbBldg.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          builID := DataModule1.query.Fields.AsInteger[0];

          if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
end;

procedure TfrmAddRm.save;
begin
        if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;
           DataModule1.query.SQL.Clear;
           DataModule1.query.SQL.Add('INSERT INTO ROOM (BUIL_ID,RM_NO,TYPE_ID,ISUSABLE,MAXNOOCC,RM_LOCATION,DATEADD,ADDEDBY) VALUES(:a, :b, :c, :d, :e, :f, :g,:h)');
           DataModule1.query.Params.AsInteger[0] := builID;
           DataModule1.query.Params.AsString[1] := txtRMNo.Text;
           DataModule1.query.Params.AsInteger[2] := typ;
          DataModule1.query.Params.AsString[3] := Edit1.Text;
           DataModule1.query.Params.AsString[4] := txtOcc.Text;
           DataModule1.query.Params.AsString[5] := txtLoc.Text;
           DataModule1.query.Params.AsString[6] := DateToStr(Now());
           DataModule1.query.Params.AsString[7] := frmLogin.fullname;
           DataModule1.query.Prepare;
           DataModule1.query.Open();

              if DataModule1.tr.InTransaction then
                DataModule1.tr.Commit;

                ShowMessage('Room successfully added');
                Clear();
                frmRoom.loadRoom;
                Close();
end;
procedure TfrmAddRm.cmbBldgKeyPress(Sender: TObject; var Key: Char);
begin
    if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = cmbBldg then
            key := #0;
      end;
end;

procedure TfrmAddRm.txtRMNoKeyPress(Sender: TObject; var Key: Char);
begin
        if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtRMNo then
            key := #0;
end;
end;

procedure TfrmAddRm.txtOccKeyPress(Sender: TObject; var Key: Char);
begin
        if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtRMNo then
            key := #0;
end;
end;

procedure TfrmAddRm.txtLocKeyPress(Sender: TObject; var Key: Char);
begin
        if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtRMNo then
            key := #0;
end;
end;

procedure TfrmAddRm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
        if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtRMNo then
            key := #0;
end;
end;

procedure TfrmAddRm.btnSaveClick(Sender: TObject);
begin
      save;
end;

procedure TfrmAddRm.Clear;
begin
      {cmbBldg.Text := '';
      txtRMNo.Text := '';
      txtOcc.Text := '';
      txtLoc.Text := '';
      Edit1.Text := '';
      checkAirCon.Checked := false;
      checkLAN.Checked := false;
      CheckBox3.Checked := false;
      CheckBox1.Checked := false;
      CheckBox2.Checked := false;     }

end;
procedure TfrmAddRm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     if MessageDlg('Do you want to exit', mtConfirmation,[mbYes,mbNo],0) = mrYes then
        Exit
    else
      action:=caNone;
end;


procedure TfrmAddRm.cmbTypeChange(Sender: TObject);
begin
 if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TYPE_ID') ;
          DataModule1.query.SQL.Add('FROM RM_TYPE');
          DataModule1.query.SQL.Add('WHERE DESC = :a');
          DataModule1.query.Params.AsString[0] := cmbType.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          typ := DataModule1.query.Fields.AsInteger[0];

          if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
end;

end.
