<?php
session_start();
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->header();
$passwordiswrong = false;
$userdontexist = false;
$user = new User($con, 0);
if(isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['pass'];
	$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_verify($password, $user->getPassword())) {
	$_SESSION["user"] = $user->getID($con, $username);
	header("Location: /home");
	//echo $_SESSION["user"];
	} else {
	$passwordiswrong = true;
	//echo $password." - sex - ".$user->getPassword($con, $username);
    }
	} else {
	$userdontexist = true;
	}
}
$result = file_get_contents("https://useless-facts.sameerkumar.website/api");
$thefunfact = json_decode($result);
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
</head>
<title><?php echo $pagename; ?> | <?php echo $sitename; ?></title>
<body>
 <div class="container position-absolute top-50 start-50 translate-middle">
  <div class="row align-items-center">
   <div class="col-lg-6 mb-4">
    <div class="card shadow-sm mb-3 d-flex mx-auto">
     <h3 class="card-header">Random useless fact generator</h3>
     <div class="card-body">
      <p class="card-text"> <?php echo $thefunfact->{"data"}; ?> </p>
     </div>
    </div>
   </div>
   <div class="col-lg-6 mb-4">
    <form action="" method="post">
     <div class="card shadow-sm mb-3 d-flex mx-auto">
      <h3 class="card-header">Login</h3>
      </svg>
      <div class="card-body">
       <label class="col-form-label col-form-label-sm mt-4 username" for="inputSmall">Username</label>
       <input class="<?php if($userdontexist == 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($userdontexist == true) {echo '<div class="invalid-feedback" bis_skin_checked="1">User does not exist.</div>'; } ?>
       <label class="col-form-label col-form-label-sm mt-4 password" 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>'; } ?>
		<a href="/register" class="mt-4 float-start">Don't have an account? Register here!</a>
		<button type="submit" class="btn btn-secondary" style="margin-top: 15px; float: right;" name="submit">Login</button>
      </div>
     </div>
    </form>
   </div>
</body>
</html>
