<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

// Get recent notifications sent to patients
$patient_notifications = $conn->query("SELECT * FROM patient_notifications ORDER BY created_at DESC LIMIT 50");
$patient_count = $conn->query("SELECT COUNT(*) as cnt FROM patient_notifications")->fetch_assoc()['cnt'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Notifications - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>body{background:#0a3d2f;}.navbar{background:#064e3b;}.card{border-radius:20px;background:rgba(255,255,255,0.95);}</style>
</head>
<body>
<nav class="navbar navbar-dark"><div class="container"><span class="navbar-brand"><i class="fas fa-list"></i> Notification History</span><a href="dashboard.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-success text-white">
            <h5>Sent Notifications (Total: <?= $patient_count ?>)</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-dark">
                        <tr><th>ID</th><th>Patient ID</th><th>Title</th><th>Message</th><th>Type</th><th>Read</th><th>Date</th></tr>
                    </thead>
                    <tbody>
                        <?php if($patient_notifications && $patient_notifications->num_rows > 0): ?>
                            <?php while($n = $patient_notifications->fetch_assoc()): ?>
                                <tr>
                                    <td><?= $n['id'] ?></td>
                                    <td><?= $n['patient_id'] ?></td>
                                    <td><?= htmlspecialchars($n['title']) ?></td>
                                    <td><?= htmlspecialchars(substr($n['message'], 0, 50)) ?>...</td>
                                    <td><span class="badge bg-<?= $n['type'] ?>"><?= $n['type'] ?></span></td>
                                    <td><?= $n['is_read'] ? 'Yes' : 'No' ?></td>
                                    <td><?= date('M d, H:i', strtotime($n['created_at'])) ?></td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td colspan="7" class="text-center">No notifications sent yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>