unit unitStuLog;

interface

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

type
  TfrmStudentLog = class(TForm)
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Image1: TImage;
    Label7: TLabel;
    Label10: TLabel;
    lblTime: TLabel;
    Label1: TLabel;
    Label6: TLabel;
    txtID: TEdit;
    StatusBar1: TStatusBar;
    rbOUT: TRadioButton;
    rbIN: TRadioButton;
    Panel1: TPanel;
    Image2: TImage;
    Timer1: TTimer;
    procedure txtIDKeyPress(Sender: TObject; var Key: Char);
    procedure rbINClick(Sender: TObject);
    procedure rbOUTClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
        logid : Integer;
        occid : Integer;
        occupantID : Integer;
        procedure studentInfo;
        procedure updateStat;
        procedure getprimaryKey;
        procedure clear;
        procedure loadid;
        procedure loadImage;
  end;

var
  frmStudentLog: TfrmStudentLog;
  IDNumber : String;
  systemDate : TDateTime;
  DateTimeformat : string;
  statusToken : Integer;
implementation

uses DM, JPEG, unitOccDes;

{$R *.dfm}

procedure TfrmStudentLog.clear;
begin
      txtID.Text := '';
      Image2.Picture := nil;
end;

procedure TfrmStudentLog.studentInfo;
begin
  
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          
          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT * FROM STUDENTPROF WHERE STUDENT_ID LIKE :a AND APP_STATUS = :B');
          DataModule1.query.Params.AsString[0] := Trim(txtID.Text);
          DataModule1.query.Params.AsString[1] := 'Admit';
          DataModule1.query.Prepare;
          DataModule1.query.Open();

          IDNumber := DataModule1.query.Fields.AsString[1];
          Label4.Caption := DataModule1.query.Fields.AsString[2];
          Label10.Caption := DataModule1.query.Fields.AsString[0];

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

            if (txtID.Text <> IDNumber) then
            begin
              MessageDlg('ID Number not found',mtInformation,[mbOK],0);
              txtID.Text := '';
              txtID.SetFocus;
            end

end;

procedure TfrmStudentLog.loadid;
begin
      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('SELECT OCC_ID FROM OCCUPANTS WHERE APPID = :A');
        DataModule1.query.Params.AsString[0] := Label10.Caption;
        DataModule1.query.Prepare;
        DataModule1.query.Open();
        occupantID := DataModule1.query.Fields.AsInteger[0];

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


end;

procedure TfrmStudentLog.getprimaryKey;
var maximum:integer;
begin
      maximum := 1;
      if DataModule1.tr.InTransaction then
      DataModule1.tr.Commit;
      DataModule1.query.SQL.Clear();
      DataModule1.query.SQL.Add('SELECT LOGID FROM LOGGED');
      DataModule1.query.Prepare;
      DataModule1.query.Open();

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

end;

procedure TfrmStudentLog.updateStat;
begin
    getprimaryKey;
    loadid;
    if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO LOGGED (LOGID,OCC_ID,DATELOG,TIMELOG,STATUSID) VALUES(:a, :b, :c, :d,:e)');
        DataModule1.query.params.AsInteger[0] := logid;
        DataModule1.query.Params.AsInteger[1] := occupantID;
        DataModule1.query.Params.AsString[2] := DateToStr(Now());
        DataModule1.query.Params.AsString[3] := TimeToStr(Now());
        DataModule1.query.Params.AsInteger[4] := StrToInt(Label7.Caption);
        DataModule1.query.Prepare;
        DataModule1.query.Open();

            if DataModule1.tr.InTransaction then
            DataModule1.tr.Commit;
            ShowMessage('You have successfully logged.');
            // loadImage;
            Clear();
end;

procedure TfrmStudentLog.loadImage;
var
    Stream: TMemoryStream;
    Jpg: TJpegImage;
begin
 loadid;
    Image2.Picture := nil;
    if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.tr.StartTransaction;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('SELECT IMAGE FROM OCCUPANTS WHERE OCC_ID =:a');
        DataModule1.query.Params.AsInteger[0] := occupantID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();
            if DataModule1.query.Fields.BlobData[0] <> nil then begin
                Stream := TMemoryStream.Create;
                Jpg := TJPEGImage.Create;
                try
                  DataModule1.query.ReadBlob('IMAGE',Stream);
                  Jpg.LoadFromStream(Stream);
                  Image2.Picture.Graphic := Jpg;
                    finally
                      Stream.Free;
                      Jpg.Free;
          end;
      end
          else begin
              Image2.Picture := nil;
          end;

          if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
end;
procedure TfrmStudentLog.txtIDKeyPress(Sender: TObject; var Key: Char);
begin
       if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtID then
            studentInfo;
            key := #0;
end;
end;

procedure TfrmStudentLog.rbINClick(Sender: TObject);
begin
    if (trim(txtID.Text) = '') then
      begin
          MessageDlg('Please enter your ID Number',mtInformation,[mbOK],0);
          txtID.SetFocus;
      end
        else if (rbIN.Checked = true) then
        begin
            updateStat;
        end
end;

procedure TfrmStudentLog.rbOUTClick(Sender: TObject);
begin
     if (trim(txtID.Text) = '') then
      begin
        MessageDlg('Please enter your ID Number',mtInformation,[mbOK],0);
            txtID.SetFocus;
            rbOUT.Checked := false;
      end
        else if (rbOUT.Checked = true) then
        begin
           frmOccDes.ShowModal;
        end
end;

procedure TfrmStudentLog.Timer1Timer(Sender: TObject);
var
DateTime : TDateTime;
Timestr,Timestr2 : string;
begin
    DateTime := Time; // store the current date and time
    Timestr2 := TimeToStr(DateTime); // convert the time into a string

    Timestr := formatDateTime('hh:mm:ss', StrToDateTime(Timestr2));//format the time }
    Label3.caption := DateToStr(Date);
    lblTime.caption := Caption;
    frmStudentLog.Caption := Caption; //puts time on Form Bar
    lblTime.caption := TimeToStr(Time); //alternative method
end;
procedure TfrmStudentLog.FormShow(Sender: TObject);
begin
        DateTimeToString(DateTimeformat, 'dddddd', now());
      Label3.Caption := DateTimeformat;
      StatusBar1.Panels.Items[1].Text := 'Date -- ' + label3.Caption;

end;

procedure TfrmStudentLog.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
      CanClose := false;
end;

end.
