unit UserPage_u;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Grids,
  Vcl.Samples.Calendar, Vcl.Menus, Vcl.ExtCtrls;

type
  TfrmUserPage = class(TForm)
    redtGoles: TRichEdit;
    MainMenu1: TMainMenu;
    Logout1: TMenuItem;
    caleUser: TCalendar;
    Panel1: TPanel;
    btnClockIn: TButton;
    btnClockOut: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ComboBox1Change(Sender: TObject);
    procedure Logout1Click(Sender: TObject);
    procedure caleUserChange(Sender: TObject);
    procedure btnClockInClick(Sender: TObject);
    procedure btnClockOutClick(Sender: TObject);
  private
    { Private declarations }
    var
    iDay : Integer;
  public
    { Public declarations }
    var
    closed : Boolean;
    sUserInUse : string;
  end;

var
  frmUserPage: TfrmUserPage;

implementation

{$R *.dfm}

uses Business_u, Login_u;

procedure TfrmUserPage.btnClockInClick(Sender: TObject);
var
  iConfirm : Integer;
begin
  DataUser.adoUser.SQL.Clear;
  DataUser.adoUser.SQL.Add('INSERT INTO ClockLog(InOut,DateLog,TimeLog,Username) VALUES(:par0,:par1,:par2,:par3)');
  DataUser.adoUser.Parameters.ParamByName('par0').Value:='In';
  DataUser.adoUser.Parameters.ParamByName('par1').Value:=DateToStr(Date);
  DataUser.adoUser.Parameters.ParamByName('par2').Value:=TimeToStr(time);
  DataUser.adoUser.Parameters.ParamByName('par3').Value:=sUserInUse;
  iConfirm:=MessageDlg('Confirm Clock in', mtConfirmation, mbYesNo, 7);
  if iConfirm = 7 then exit;
  if iConfirm = 6 then DataUser.adoUser.ExecSQL;
end;

procedure TfrmUserPage.btnClockOutClick(Sender: TObject);
var
  iConfirm : Integer;
begin
  DataUser.adoUser.SQL.Clear;
  DataUser.adoUser.SQL.Add('INSERT INTO ClockLog(InOut,DateLog,TimeLog,Username) VALUES(:par0,:par1,:par2,:par3)');
  DataUser.adoUser.Parameters.ParamByName('par0').Value:='Out';
  DataUser.adoUser.Parameters.ParamByName('par1').Value:=Date;
  DataUser.adoUser.Parameters.ParamByName('par2').Value:=time;
  DataUser.adoUser.Parameters.ParamByName('par3').Value:=sUserInUse;
  iConfirm:=MessageDlg('Confirm Clock OUT', mtConfirmation, mbYesNo, 7);
  if iConfirm = 7 then exit;
  if iConfirm = 6 then DataUser.adoUser.ExecSQL;
end;

procedure TfrmUserPage.Button1Click(Sender: TObject);
begin
  closed:=True;

end;

procedure TfrmUserPage.caleUserChange(Sender: TObject);
var
  i : Integer;
  Goletext : TextFile;
  sLine : string ;
begin
  iday:=caleUser.Day;
  redtGoles.Text:='';
  //ShowMessage(sUserName+IntToStr(iDay)+'.txt');
  AssignFile(Goletext, sUserInUse+IntToStr(iDay)+'.txt');
  if sUserInUse > '' then
  begin
    if FileExists(sUserInUse+IntToStr(iDay)+'.txt') then
    begin
      redtGoles.Text:='';
      reset(Goletext);
      while not Eof(Goletext) do
      begin
        Readln(Goletext, sLine);
        redtGoles.Lines.Add(sLine);
      end;

    end
    else
    begin
      Rewrite(Goletext);
    end;
  end
  else
  exit;
  CloseFile(Goletext);
end;

procedure TfrmUserPage.ComboBox1Change(Sender: TObject);
begin
//  if ComboBox1.Text = 'LogOut' then
  //  closed:= True;
end;

procedure TfrmUserPage.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;
end;

procedure TfrmUserPage.FormCreate(Sender: TObject);
begin
  iDay:=caleUser.Day;
  closed:=False;
end;

procedure TfrmUserPage.Logout1Click(Sender: TObject);
begin
  frmBusiness.show;
  frmUserPage.Free;
end;

end.
