<?php
require_once 'core/classes.php';
use classes\PartyStarter as PartyStarter;
require_once 'core/config.php';

$getitstarted = new PartyStarter;
$getitstarted->buildloggedoutheader();
$passwordconfirmiswrong = false;
$passwordistooshort = false;
if(isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['pass'];
	$passwordconfirm = $_POST['passconfirm'];
	echo "ur username: " . $username;
	echo "<br/>";
	echo "ur password: " . $password;
	echo "<br/>";
	echo "ur passwordconfirm: " . $passwordconfirm;
	echo "<br/>";
	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);
			echo $password;
			echo "<br/>";
			$query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username');
			$query->bindParam(':username', $username);
            $query->execute();
			$result = $query->fetchColumn();
			if ($result == 0) {
  	        echo "No results, continue. ";
			$query = $con->prepare('INSERT INTO users (username, password) VALUES (:username, :password)');
			$query->bindParam(':username', $username);
			$query->bindParam(':password', $password);
            $query->execute();
  	        } else {
  	        echo "Nope, you stay.";
  	        }

	}
}
$result = file_get_contents("https://useless-facts.sameerkumar.website/api");
$thefunfact = json_decode($result);
?>
<div class="bg-dark">
<link rel="stylesheet" href="css/register.css">
<form action="" method="post">
<div class="card mb-3">
  <h3 class="card-header">Registration</h3>
  </svg>
  <div class="card-body">
  <label class="col-form-label col-form-label-sm mt-4 username" for="inputSmall">Username</label>
  <input class="form-control form-control-sm" type="text" placeholder="Your username" id="inputSmall" for="inputValid" name="username">
  <label class="col-form-label col-form-label-sm mt-4 password" 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" type="password" placeholder="Enter your password again" id="inputSmall" name="passconfirm">
  <button type="submit" class="btn btn-secondary" 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>
</div>
