<?php

session_start();

if (!isset($_SESSION['user_id'])) {

    echo '<p class="text-danger">Unauthorized access</p>';

    exit;

}



require_once '../config.php';

$conn = getDBConnection();



$action = $_GET['action'] ?? '';

$id = intval($_GET['id'] ?? 0);



if ($action === 'view' && $id > 0) {

    $result = $conn->query("SELECT n.*, s.first_name, s.last_name, s.student_id 

                            FROM notifications n 

                            LEFT JOIN students s ON n.student_id = s.id 

                            WHERE n.id = $id");

    $notification = $result ? $result->fetch_assoc() : null;

    

    if ($notification) {

        ?>

        <div class="container-fluid">

            <div class="row mb-3">

                <div class="col-md-4"><strong>Date/Time:</strong></div>

                <div class="col-md-8"><?php echo date('d M Y H:i:s', strtotime($notification['sent_at'])); ?></div>

            </div>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Type:</strong></div>

                <div class="col-md-8">

                    <span class="badge bg-<?php echo $notification['type'] === 'sms' ? 'info' : 'warning'; ?>">

                        <?php echo strtoupper($notification['type']); ?>

                    </span>

                </div>

            </div>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Recipient:</strong></div>

                <div class="col-md-8"><?php echo htmlspecialchars($notification['recipient']); ?></div>

            </div>

            <?php if ($notification['student_id']): ?>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Student:</strong></div>

                <div class="col-md-8">

                    <?php echo htmlspecialchars($notification['first_name'] . ' ' . $notification['last_name'] . ' (' . $notification['student_id'] . ')'); ?>

                </div>

            </div>

            <?php endif; ?>

            <?php if (!empty($notification['subject'])): ?>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Subject:</strong></div>

                <div class="col-md-8"><?php echo htmlspecialchars($notification['subject']); ?></div>

            </div>

            <?php endif; ?>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Status:</strong></div>

                <div class="col-md-8">

                    <span class="status-badge <?php echo $notification['status']; ?>">

                        <?php echo ucfirst($notification['status']); ?>

                    </span>

                </div>

            </div>

            <div class="row mb-3">

                <div class="col-md-4"><strong>Message:</strong></div>

                <div class="col-md-8">

                    <div class="p-3 bg-light rounded">

                        <?php echo nl2br(htmlspecialchars($notification['message'])); ?>

                    </div>

                </div>

            </div>

        </div>

        <?php

    } else {

        echo '<p class="text-danger">Notification not found</p>';

    }

} else {

    echo '<p class="text-danger">Invalid request</p>';

}



$conn->close();

?>