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

if (isset($_GET['mark_read'])) {
    $id = (int)$_GET['mark_read'];
    $conn->query("UPDATE doctor_notifications SET is_read = 1 WHERE id = $id AND doctor_id = $doctor_id");
    header("Location: doctor_notifications.php");
    exit();
}

$notifications = $conn->query("SELECT * FROM doctor_notifications WHERE doctor_id = $doctor_id ORDER BY created_at DESC");
$unread_count = $conn->query("SELECT COUNT(*) as cnt FROM doctor_notifications WHERE doctor_id = $doctor_id AND is_read = 0")->fetch_assoc()['cnt'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Doctor Notifications</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <h2>Notifications</h2>
    <?php if($notifications && $notifications->num_rows > 0): ?>
        <?php while($n = $notifications->fetch_assoc()): ?>
            <div class="alert alert-<?= $n['type'] ?>">
                <strong><?= htmlspecialchars($n['title']) ?></strong><br>
                <?= nl2br(htmlspecialchars($n['message'])) ?>
                <small class="d-block"><?= date('M d, Y H:i', strtotime($n['created_at'])) ?></small>
                <?php if(!$n['is_read']): ?>
                    <a href="?mark_read=<?= $n['id'] ?>" class="btn btn-sm btn-primary mt-2">Mark Read</a>
                <?php endif; ?>
            </div>
        <?php endwhile; ?>
    <?php else: ?>
        <p>No notifications.</p>
    <?php endif; ?>
    <a href="dashboard.php" class="btn btn-secondary">Back</a>
</div>
</body>
</html>