unit unitStudentLog;

interface

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

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

var
  IDNumber : String;
  systemDate : TDateTime;
  DateTimeformat : string;
  statusToken : Integer;
  StudentLog: TStudentLog;

implementation

uses DM, JPEG, unitOccDes, unitLogin, jvuib, jvuiblib, loginForm;

{$R *.dfm}

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

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

          
          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT * FROM VIEWLOGGED WHERE pass 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[1];
          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 TStudentLog.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 TStudentLog.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 TStudentLog.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();
            Label4.Caption := '';
end;

procedure TStudentLog.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 applicants WHERE appid =:a');
        DataModule1.query.Params.AsString[0] := Label10.Caption;
        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 TStudentLog.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 TStudentLog.btnEnterClick(Sender: TObject);
begin
       if (Label4.Caption = txtID.Text) then
        begin
            studentInfo;
        end
end;

procedure TStudentLog.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
           frmOccLogin.ShowModal;
        end
end;

procedure TStudentLog.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;
    StudentLog.Caption := Caption; //puts time on Form Bar
    lblTime.caption := TimeToStr(Time); //alternative method
end;

procedure TStudentLog.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 TStudentLog.FormShow(Sender: TObject);
begin
      DateTimeToString(DateTimeformat, 'dddddd', now());
      Label3.Caption := DateTimeformat;
      StatusBar1.Panels.Items[1].Text := 'Date -- ' + label3.Caption;
end;

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

{function TOccupants.existingOneIdInDate() : boolean;
begin

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

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('SELECT COUNT(DATELOG) FROM LOGGED');
        DataModule1.query.SQL.Add('where DATELOG = :a and STUEDNT_ID = b');
        DataModule1.query.Params.AsString[0] := txtID.Text;
        DataModule1.query.Prepare;
        DataModule1.query.Open();
            if (DataModule1.query.Fields.AsInteger[0] <> 0)  then
            begin
                existingOneIdInDate := true;
                    MessageDlg('You already logged.',mtError,[mbOK],0);
                end
                  else
                    existingOneIdInDate := false;
end;}

end.
