<?php
session_name('teacher_session');
session_start();
include("includes/dbconnection.php");

date_default_timezone_set('Africa/Lagos');
$ldate = date('d-m-Y h:i:s A', time());
$email = $_SESSION['teacher_email'] ?? '';

if (!empty($email)) {
    $sql = "UPDATE userlog SET logout=:ldate WHERE userEmail=:email ORDER BY id DESC LIMIT 1";
    $query = $dbh->prepare($sql);
    $query->bindParam(':ldate', $ldate, PDO::PARAM_STR);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->execute();
}

$_SESSION['errmsg'] = "You have successfully logged out";

// Unset session variables
unset($_SESSION['mini_sid'], $_SESSION['mini_name'], $_SESSION['mini_lastname'], $_SESSION['mini_email'], $_SESSION['mini_permission']);

// Destroy session
session_unset();
session_destroy();

if (isset($_COOKIE['mini_user_login'])) {
    setcookie("mini_user_login", "", time() - 3600, "/");
}

header("Location: teacher.php");
exit;
?>
