unit AdminAdd_u;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Login_u, System.math,
  Vcl.Tabs, Vcl.Grids, Vcl.Samples.Calendar, Vcl.ComCtrls, Vcl.DBCtrls, Data.DB,
  Vcl.DBGrids;

type
  TFrmAdminAdd = class(TForm)
    edtUsername: TEdit;
    edtPassword: TEdit;
    edtIDNum: TEdit;
    Panel1: TPanel;
    chkAdmin: TCheckBox;
    btnGeneratePass: TButton;
    btnAddUser: TButton;
    redtGoles: TRichEdit;
    caleAdmin: TCalendar;
    btnGole: TButton;
    cbxUsers: TComboBox;
    procedure btnGeneratePassClick(Sender: TObject);
    procedure edtUsernameDblClick(Sender: TObject);
    procedure edtIDNumDblClick(Sender: TObject);
    procedure edtIDNumEnter(Sender: TObject);
    procedure edtUsernameEnter(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnAddUserClick(Sender: TObject);
    procedure cbxUsersEnter(Sender: TObject);
    procedure cbxUsersChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure caleAdminChange(Sender: TObject);
    procedure btnGoleClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    var
      sRandomPass, sAdmin, sUserName  : string;
      iCount, iRandomNum, iRandomChar, iRandomUp, iRandomLow, iDay : Integer;
      Goletext : TextFile;


  end;

var
  FrmAdminAdd: TFrmAdminAdd;

implementation

{$R *.dfm}

procedure TFrmAdminAdd.btnAddUserClick(Sender: TObject);
var
  sUser, sPass, sID : string;
  iConfirm : Integer;
begin
  iConfirm:=0;
  sUser:='';
  sPass:='';
  sID:='';
  sAdmin:= BoolToStr(chkAdmin.Checked);

  if Length(edtUsername.Text) >= 7  then
    sUser:= edtUsername.Text
  else
    begin
      ShowMessage('User Name must be 7 or more caractars');
      exit
    end;

  if Length(edtPassword.Text) = 12 then
    sPass:=edtPassword.Text
  else
    begin
      ShowMessage('Press Random pass generator');
      exit
    end;

  if Length(edtIDNum.Text) = 13 then
    sID:=edtIDNum.Text
  else
    begin
      ShowMessage('ID Number must be 13 digets long');
      exit
    end;

  DataUser.adoUser.Close;
  DataUser.adoUser.SQL.Clear;
  DataUser.adoUser.SQL.Add('INSERT INTO Login(Username,IDnum,pass,Admin) VALUES(:par0,:par1,:par2,:par3)');
  DataUser.adoUser.Parameters.ParamByName('par0').Value:=sUser;
  DataUser.adoUser.Parameters.ParamByName('par1').Value:=sID;
  DataUser.adoUser.Parameters.ParamByName('par2').Value:=sPass;
  DataUser.adoUser.Parameters.ParamByName('par3').Value:= sAdmin;
  iConfirm:= MessageDlg('Confirm Adding Acount', mtConfirmation, mbYesNo, 7);
  if iConfirm = 7 then exit;
  if iConfirm = 6 then DataUser.adoUser.ExecSQL;

end;

procedure TFrmAdminAdd.btnGeneratePassClick(Sender: TObject);
var
  arrRandomChar : array[1..3] of Integer;
  i : Integer;
begin
  sRandomPass := '';
  iCount := 0;
  iRandomNum:= 0;
  iRandomUP:= 0;
  iRandomLow:= 0;
  edtPassword.ReadOnly:=False;
  while iCount < 12 do
    begin
      Randomize;
      iRandomNum := RandomRange(48, 58);
      iRandomUp := RandomRange(65, 91);
      iRandomLow := RandomRange(97, 123);
      arrRandomChar[1]:= iRandomNum;
      arrRandomChar[2]:= iRandomUp;
      arrRandomChar[3]:= iRandomLow;
      i := random(3)+1;
      iRandomChar:= arrRandomChar[i];
      sRandomPass:= sRandomPass + Chr(iRandomChar);
      iCount:= iCount + 1;
    end;
  edtPassword.ReadOnly:=True;
  edtPassword.Text:=sRandomPass;



end;

procedure TFrmAdminAdd.btnGoleClick(Sender: TObject);
var
  i : Integer;
begin
  AssignFile(Goletext, sUserName+IntToStr(iDay)+'.txt');
  Rewrite(Goletext);
  for i := 0 to redtGoles.Lines.Count do
  begin
    Writeln(goletext, redtGoles.Lines[i]);
  end;
  CloseFile(Goletext);

end;

procedure TFrmAdminAdd.caleAdminChange(Sender: TObject);
var
  sLines : string;
begin
  redtGoles.Text:='';
  iDay:= caleAdmin.Day;
  //ShowMessage(sUserName+IntToStr(iDay)+'.txt');
  AssignFile(Goletext, sUserName+IntToStr(iDay)+'.txt');
  if sUserName > '' then
  begin
    if FileExists(sUserName+IntToStr(iDay)+'.txt') then
    begin
      redtGoles.Text:='';
      reset(Goletext);
      while not Eof(Goletext) do
      begin
        Readln(Goletext, sLines);
        redtGoles.Lines.Add(sLines);
      end;

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

procedure TFrmAdminAdd.cbxUsersChange(Sender: TObject);
var
  iUser : Integer;
begin
  iUser := cbxUsers.ItemIndex;
  sUserName := cbxUsers.Items[iUser];

end;

procedure TFrmAdminAdd.cbxUsersEnter(Sender: TObject);
begin
  DataUser.adoUser.Close;
  DataUser.adoUser.SQL.Clear;
  DataUser.adoUser.SQL.Add('SELECT * FROM Login');
  DataUser.adoUser.Open;
  DataUser.adoUser.First;
  cbxUsers.Items.Clear;
  while not DataUser.adoUser.Eof do
  begin
    cbxUsers.Items.Add(DataUser.adoUser.FieldByName('Username').AsString);
    DataUser.adoUser.Next;
  end;
end;

procedure TFrmAdminAdd.edtIDNumDblClick(Sender: TObject);
begin
  edtIDNum.Text:= '';
end;

procedure TFrmAdminAdd.edtIDNumEnter(Sender: TObject);
begin
  edtIDNum.Hint:= 'Dubbel tapp to clear';
  edtIDNum.ShowHint := True;
end;

procedure TFrmAdminAdd.edtUsernameDblClick(Sender: TObject);
begin
  edtUsername.Text:= '';
end;

procedure TFrmAdminAdd.edtUsernameEnter(Sender: TObject);
begin
  edtUsername.Hint:= 'Dubbel tap to clear';
  edtUsername.ShowHint:= True;
end;

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

procedure TFrmAdminAdd.FormCreate(Sender: TObject);
begin
  redtGoles.Text:='';
  sUserName:='';
end;

end.
