unit unitAddBuilding;

interface

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

type
  TfrmAddBuilding = class(TForm)
    Label5: TLabel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CheckBox1: TCheckBox;
    txtBname: TEdit;
    txtAcro: TEdit;
    txtFloor: TEdit;
    StatusBar1: TStatusBar;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
      builID : Integer;
      systemDate : TDateTime;
      DateTimeformat : string;
      procedure Clear;
      procedure getPK;
      procedure save;
  end;

var
  frmAddBuilding: TfrmAddBuilding;

implementation

uses DM, unitLogin;

{$R *.dfm}

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

procedure TfrmAddBuilding.btnCancelClick(Sender: TObject);
begin
      Close();
end;

procedure TfrmAddBuilding.Clear;
begin
       txtBname.Text := '';
       txtAcro.Text := '';
       txtFloor.Text := '';
       CheckBox1.Checked := false;
end;

procedure TfrmAddBuilding.getPK;
var primary : Integer;
begin
      primary :=1;
        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.Prepare;
          DataModule1.query.Open();

              if DataModule1.query.Fields.Eof then
                  builID := primary
                    else begin
                        DataModule1.query.SQL.Clear;
                        DataModule1.query.SQL.Add('SELECT MAX(BUIL_ID) FROM BUILDING');
                        DataModule1.query.Prepare;
                        DataModule1.query.Open();
                        builID  := DataModule1.query.Fields.AsInteger[0] + 1;
                end;
                      if  DataModule1.tr.InTransaction then
                          DataModule1.tr.Commit;

end;

procedure TfrmAddBuilding.save;
var
  wifi : String;
begin
      getPK;
      if (CheckBox1.Checked = true) then
      begin
            wifi := '1';
      end
      else if (CheckBox1.Checked = false) then
      begin
            wifi := '0';
      end
      else
          if DataModule1.tr.InTransaction then
            DataModule1.tr.Commit;

            DataModule1.query.SQL.Clear;
            DataModule1.query.SQL.Add('INSERT INTO BUILDING (BUIL_ID, DESCRIPTION,ACRONYM,FLOORCOUNTS,LANREADY,ADDED_BY,DATE_ADD) VALUES(:a, :b, :c, :d, :e, :f, :g)');
            DataModule1.query.Params.AsInteger[0] := builID;
            DataModule1.query.Params.AsString[1] := txtBname.Text;
            DataModule1.query.Params.AsString[2] := txtAcro.Text;
            DataModule1.query.Params.AsString[3] := txtFloor.Text;
            DataModule1.query.Params.AsString[4] := wifi;
            DataModule1.query.Params.AsString[5] := frmLogin.fullname;
            DataModule1.query.Params.AsString[6]:= DateToStr(Now());
            DataModule1.query.Prepare;
            DataModule1.query.Open();

              if DataModule1.tr.InTransaction then
                 DataModule1.tr.Commit;
                 ShowMessage('Room successfully added');
                 //frmBldg.loadBldg;
                 Clear();
                 Close();
end;
end.
