<!DOCTYPE html>
<html>
<head>
<?php
require_once 'core/classes.php';
require_once 'core/classes/User.php';
require_once 'core/config.php';

$getitstarted = new PartyStarter;
$getitstarted->buildloggedoutheader();
$passwordiswrong = false;
$user = new User($con, 0);
if(isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['pass'];
	$password = password_hash($password, PASSWORD_DEFAULT);
	$user = new User($con, $user->getID($con, $username));
	
	$query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username');
	$query->bindParam(':username', $username);
    $query->execute();
	$result = $query->fetchColumn();
	if ($result > 0) {
	if ($password == $user->getPassword($con, $username)) {
	$_SESSION["user"] = $user->getID($con, $username);
	header("Location: /home");
	} else {
	$passwordiswrong = true;
	echo $password." - sex - ".$user->getPassword($con, $username);
    }
	}
}
$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" style="max-height: 19rem;">
  <h3 class="card-header bg-dark text-light">Login</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="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 text-light" for="inputSmall">Password</label>
  <input class="<?php if($passwordiswrong == 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($passwordiswrong == true) {
	  echo '<div class="invalid-feedback" bis_skin_checked="1">Password is incorrect.</div>';
  } ?>
  <button type="submit" class="btn btn-secondary text-light" name="submit">Login</button><br>
  </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>