<!DOCTYPE html>
<html>
<head>
<title>grublock | register</title>
<?php
if(isset($_SESSION['user'])) {
	header("Location: /home");
}
require_once 'core/classes.php';
require_once 'core/classes/User.php';
require_once 'core/config.php';

$getitstarted = new PartyStarter;
$getitstarted->buildloggedoutheader();
$passwordconfirmiswrong = false;
$passwordistooshort = false;
$user = new User($con, 0);
$userexists = false;
if(isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['pass'];
	$passwordconfirm = $_POST['passconfirm'];
	$user = new User($con, $user->getID($con, $username));
	if ($password == $passwordconfirm) {
      $passwordconfirmiswrong = false;
	} else {
	 $passwordconfirmiswrong = true;
	}
	
	if (strlen($password) < 8) {
     $passwordistooshort = true;
	} else {
     $passwordistooshort = false;
	}
	if ($passwordistooshort == false && $passwordconfirmiswrong == false) {
			$password = password_hash($password, PASSWORD_DEFAULT);
			$query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username');
			$query->bindParam(':username', $username);
            $query->execute();
			$result = $query->fetchColumn();
			if ($result == 0) {
			$query = $con->prepare('INSERT INTO users (username, password) VALUES (:username, :password)');
			$query->bindParam(':username', $username);
			$query->bindParam(':password', $password);
            $query->execute();
			$_POST['success'] = 1;
			$_SESSION["user"] = $user->getID($con, $username);	
			header("Location: /home");
  	        } else {
              $userexists = true;
  	        }

	}
}
$result = file_get_contents("https://useless-facts.sameerkumar.website/api");
$thefunfact = json_decode($result);
?>
<link rel="stylesheet" href="css/register.css">
</head>
<body>
<form action="" method="post">
<div class="card mb-3 bg-dark">
  <h3 class="card-header bg-dark text-light">Registration</h3>
  </svg>
  <div class="card-body bg-dark text-light">
  <label class="col-form-label col-form-label-sm mt-4 username text-light" for="inputSmall">Username</label>
  <input class="<?php if($userexists == true) {
	  echo "form-control is-invalid";
  } else {
echo "form-control form-control-sm";
  }	?>" type="text" placeholder="Your username" id="inputSmall" for="inputValid" name="username">
  <?php if($userexists == true) { echo '<div class="invalid-feedback" bis_skin_checked="1">Password Confirmation field is invalid.</div>'; } ?>
  <label class="col-form-label col-form-label-sm mt-4 password text-light" for="inputSmall">Password</label>
  <input class="<?php if($passwordconfirmiswrong == true) {
	  echo "form-control is-invalid";
  } else {
echo "form-control form-control-sm";
  }	?>" type="password" placeholder="Your password (Minmum 8 characters)" id="inputSmall" name="pass">
  <?php if($passwordconfirmiswrong == true) {
	  echo '<div class="invalid-feedback" bis_skin_checked="1">Password Confirmation field is invalid.</div>';
  } else if ($passwordistooshort == true) {
	  echo '<div class="invalid-feedback" bis_skin_checked="1">Password is too short. (8 Characters minmum)</div>';
  }?>
  <label class="col-form-label col-form-label-sm mt-4 passwordconfirm" for="inputSmall">Password Confirmation</label>
  <input class="form-control form-control-sm text-light" type="password" placeholder="Enter your password again" id="inputSmall" name="passconfirm">
  <button type="submit" class="btn btn-secondary text-light" name="submit">Register</button>
  </div>
</div>
</form>
<div class="card text-white bg-dark mb-3" style="max-width: 20rem;">
  <div class="card-header">Random useless fact generator</div>
  <div class="card-body">
    <p class="card-text"><?php echo $thefunfact->{"data"}; ?></p>
  </div>
</div>
</body>
</html>
