<?php
session_start();
if (!isset($_SESSION['username'])) {
    $_SESSION['username'] = "Guest"; 
}
?>
<?php
include 'includes/header.php';
include 'includes/admin-sidebar.php';
require 'config.php'; 

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'super-admin') {
    header("Location: login.php");
    exit();
}

$sql = "SELECT n.id, n.message, n.is_read, n.created_at 
        FROM notifications n 
        WHERE n.user_id = {$_SESSION['user_id']} 
        ORDER BY n.created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notifications</title>
    <link rel="stylesheet" href="includes/css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="main-content">
    <div class="container">
        <h2 class="mt-4 mb-3 text-center">📢 Notifications</h2>

        <div class="card shadow p-3">
            <div class="table-responsive">
                <table class="table table-bordered table-hover text-center">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Message</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($result->num_rows > 0) {
                            $count = 1;
                            while ($row = $result->fetch_assoc()) {
                                echo "<tr>
                                        <td>{$count}</td>
                                        <td>{$row['message']}</td>
                                        <td>" . ($row['is_read'] ? "<span class='badge bg-success'>Read</span>" : "<span class='badge bg-warning'>Unread</span>") . "</td>
                                        <td>" . date("M d, Y - H:i A", strtotime($row['created_at'])) . "</td>
                                      </tr>";
                                $count++;
                            }
                        } else {
                            echo "<tr><td colspan='4' class='text-center text-muted'>No notifications found</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<style>
    .main-content {
        margin-left: 250px; 
        padding: 20px;
    }
    .table-hover tbody tr:hover {
        background-color: #f1f1f1;
    }
</style>

</body>
</html>
