<?php
session_start();
include('includes/config.php');

// Log the logout activity if admin was logged in
if (isset($_SESSION['admin_id'])) {
    $admin_id = $_SESSION['admin_id'];
    $admin_name = $_SESSION['admin_name'] ?: 'Unknown';
    $admin_role = $_SESSION['admin_role'] ?: 'admin';
    
    // Log admin activity
    $log_sql = "INSERT INTO admin_activities (admin_id, admin_name, admin_role, action, description, ip_address, user_agent) 
               VALUES ('$admin_id', '$admin_name', '$admin_role', 'Logout', 
               'Admin logged out of the system', '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_USER_AGENT']}')";
    mysqli_query($conn, $log_sql);
}

// Destroy all session data
session_destroy();

// Clear any cookies
setcookie('admin_remember', '', time() - 3600, '/');

// Redirect to login page
header('Location: login.php?message=You have been logged out successfully.');
exit();
?>