<?php

$base = "/SEAT";

error_reporting(E_ALL);
ini_set('display_errors',1);

session_start();

include("config/db.php");

$message = "";
$type = "";

/* =========================================
   REMEMBERED EMAIL
========================================= */

$remember_email = $_COOKIE['remember_email'] ?? '';

/* =========================================
   HASH GENERATOR
========================================= */

if(isset($_GET['hash'])){

echo password_hash(
$_GET['hash'],
PASSWORD_DEFAULT
);

exit;

}

/* =========================================
   LOGIN
========================================= */

if(isset($_POST['login'])){

$email = trim($_POST['email'] ?? "");
$pass  = $_POST['password'] ?? "";

$q = mysqli_query(
$conn,
"SELECT * FROM users WHERE email='$email'"
);

$d = mysqli_fetch_assoc($q);

/* USER NOT FOUND */
if(!$d){

$message = "User not found";
$type = "error";

}

/* ACCOUNT PENDING */
else if($d['status']=="pending"){

$message = "Account waiting for admin approval";
$type = "error";

}

/* ACCOUNT DISABLED */
else if($d['status']=="inactive"){

$message = "Account disabled";
$type = "error";

}

/* WRONG PASSWORD */
else if(!password_verify($pass,$d['password'])){

$message = "Wrong password";
$type = "error";

}

/* LOGIN SUCCESS */
else{

$_SESSION['id'] = $d['id'];
$_SESSION['role'] = $d['role'];

/* =========================================
   REMEMBER EMAIL
========================================= */

if(isset($_POST['remember'])){

setcookie(
"remember_email",
$email,
time() + (86400 * 30),
"/"
);

}else{

/* REMOVE COOKIE */

setcookie(
"remember_email",
"",
time() - 3600,
"/"
);

}

$message = "Login success";
$type = "success";

}

}

?>

<!DOCTYPE html>
<html>

<head>

<title>Login</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#4b1f7a">
<link rel="stylesheet" href="assets/css/auth/login.css">


</head>

<body>

<div class="box">

<div id="toastBox" class="toast-container"></div>

<div class="logo">
<img src="assets/images/logo4.png">
</div>

<h2>Welcome Back</h2>

<p class="desc">
Login to continue to SEAT System
</p>

<form method="post" autocomplete="on">

<!-- EMAIL -->

<input
type="email"
name="email"
autocomplete="email"
placeholder="Enter your email address"
value="<?= htmlspecialchars($remember_email) ?>"
required>

<!-- PASSWORD -->

<input
type="password"
name="password"
autocomplete="current-password"
placeholder="Enter your password"
required>

<div class="forgot">

<a href="modules/User/auth/forgotpass.php">
Forgot Password?
</a>

</div>

<div class="row">

<label class="remember">

<input
type="checkbox"
name="remember"
<?= isset($_COOKIE['remember_email']) ? 'checked' : '' ?>>

Remember me

</label>

</div>

<button name="login">

Sign In

</button>

</form>

<div class="bottom">

Don't have account?

<a href="modules/User/auth/registerType.php">
Sign up
</a>

</div>

</div>

<script>

let message = "<?= $message ?>";
let type = "<?= $type ?>";

if(message != ""){

showToast(message,type);

if(type=="success"){

setTimeout(()=>{

let role = "<?= $_SESSION['role'] ?? "" ?>";

const roleRedirects = {

admin:
"/SEAT/modules/User/admin/dashboard.php",

organizer:
"/SEAT/modules/User/organizer/dashboard.php",

student:
"/SEAT/modules/User/participant/dashboard.php",

staff:
"/SEAT/modules/User/participant/dashboard.php"

};

if(roleRedirects[role]){

window.location = roleRedirects[role];

}else{

window.location = "/SEAT/login.php";

}

},1500);

}

}

/* =========================================
   TOAST
========================================= */

function showToast(msg,type){

let box =
document.getElementById("toastBox");

box.innerHTML="";

let t=document.createElement("div");

t.classList.add("toast");

if(type=="success"){

t.classList.add("toast-success");

}else{

t.classList.add("toast-error");

}

/* ICON */

let icon=document.createElement("span");

icon.classList.add("toast-icon");

if(type=="success"){

icon.innerHTML="✔";

}else{

icon.innerHTML="✖";

}

let text=document.createElement("span");

text.innerText=msg;

t.appendChild(icon);
t.appendChild(text);

box.appendChild(t);

setTimeout(()=>{

t.remove();

},4000);

}

</script>

</body>
</html>