<?php
require_once '../includes/config.php';

// Update admins table — mark offline, save logout time, clear session data
if (isset($_SESSION['admin_id'])) {
    try {
        $pdo->prepare("
            UPDATE admins
            SET is_online      = 0,
                logout_at      = NOW(),
                session_id     = NULL,
                last_active_at = NOW(),
                current_page   = NULL
            WHERE id = ?
        ")->execute([$_SESSION['admin_id']]);
    } catch (Exception $e) {}
}

// Destroy admin session variables
unset(
    $_SESSION['admin_id'],
    $_SESSION['admin_name'],
    $_SESSION['admin_email'],
    $_SESSION['admin_login_time'],
    $_SESSION['admin_last_ping']
);
session_destroy();

header('Location: ' . SITE_URL . '/admin/login.php');
exit;