<?php
session_start();

// 1. SECURITY: Redirect if not logged in
if (!isset($_SESSION['username'])) {
    header("Location: index.php");
    exit();
}

// 2. LOGIC: Handle Logout if button is clicked
if (isset($_POST['action']) && $_POST['action'] == 'logout') {
    session_unset();
    session_destroy();
    header("Location: index.php");
    exit();
}
?>

<!-- 3. UI: The Logout Button (Styles included so it looks good everywhere) -->
<div style="background: #f8f9fa; padding: 10px 20px; border-bottom: 1px solid #ddd; display: flex; justify-content: space-between; align-items: center; font-family: sans-serif;">
    <span>Logged in as: <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong></span>
    
    <form method="POST" style="margin: 0;">
        <input type="hidden" name="action" value="logout">
        <button type="submit" style="background: #dc3545; color: white; border: none; padding: 6px 12px; border-radius: 4px; cursor: pointer; font-weight: bold;">
            Logout
        </button>
    </form>
</div>
