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

// Mark notification as read
if (isset($_GET['mark_read'])) {
    $id = (int)$_GET['mark_read'];
    $conn->query("UPDATE patient_notifications SET is_read = 1 WHERE id = $id AND patient_id = '$patient_id'");
    header("Location: notifications.php");
    exit();
}

// Delete notification
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $conn->query("DELETE FROM patient_notifications WHERE id = $id AND patient_id = '$patient_id'");
    header("Location: notifications.php");
    exit();
}

$notifications = $conn->query("SELECT * FROM patient_notifications WHERE patient_id = '$patient_id' ORDER BY created_at DESC");
$unread_count = $conn->query("SELECT COUNT(*) as cnt FROM patient_notifications WHERE patient_id = '$patient_id' AND is_read = 0")->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>Notifications - UHD</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-bell"></i> Notifications</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>Notifications <?php if($unread_count > 0): ?><span class="badge bg-danger"><?= $unread_count ?> new</span><?php endif; ?></h5>
        </div>
        <div class="card-body">
            <?php if($notifications && $notifications->num_rows > 0): ?>
                <?php while($n = $notifications->fetch_assoc()): ?>
                    <div class="alert alert-<?= $n['type'] ?> alert-dismissible fade show">
                        <strong><?= htmlspecialchars($n['title']) ?></strong><br>
                        <?= nl2br(htmlspecialchars($n['message'])) ?>
                        <small class="text-muted d-block mt-1"><?= date('M d, Y H:i', strtotime($n['created_at'])) ?></small>
                        <a href="?mark_read=<?= $n['id'] ?>" class="btn btn-sm btn-outline-success mt-1">Mark Read</a>
                        <a href="?delete=<?= $n['id'] ?>" class="btn btn-sm btn-outline-danger mt-1" onclick="return confirm('Delete?')">Delete</a>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <p class="text-muted text-center">No notifications yet.</p>
            <?php endif; ?>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>