<?php

session_start();

require_once __DIR__ . '/../../library/config.php';

require_once __DIR__ . '/../../library/super_admin_auth.php';

// allow_super_admin();

$page_title = 'View Notification';

require_once __DIR__ . '/../includes/header.php';

require_once __DIR__ . '/../includes/sidebar.php';

/*
|--------------------------------------------------------------------------
| VALIDATE ID
|--------------------------------------------------------------------------
*/

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

if (!$id) {

    die('Invalid Notification ID');
}

/*
|--------------------------------------------------------------------------
| FETCH NOTIFICATION
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        n.*,
        s.name AS school_name

    FROM super_admin_notifications n

    LEFT JOIN schools s
    ON s.id = n.school_id

    WHERE n.id = ?

    LIMIT 1

");

$stmt->execute([$id]);

$notification = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$notification) {

    die('Notification not found.');
}

/*
|--------------------------------------------------------------------------
| AUTO MARK AS READ
|--------------------------------------------------------------------------
*/

if ((int)$notification['is_read'] === 0) {

    $updateStmt = $pdo->prepare("

        UPDATE super_admin_notifications

        SET is_read = 1

        WHERE id = ?

    ");

    $updateStmt->execute([$id]);

    $notification['is_read'] = 1;
}

?>

<!-- =========================================================
CONTENT HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="d-flex justify-content-between align-items-center mb-3">

            <div>

                <h1 class="font-weight-bold mb-1">

                    View Notification

                </h1>

                <p class="text-muted mb-0">

                    Notification details

                </p>

            </div>

            <div>

                <a
                    href="index.php"
                    class="btn btn-secondary"
                >

                    <i class="fas fa-arrow-left mr-1"></i>

                    Back

                </a>

            </div>

        </div>

    </div>

</section>

<!-- =========================================================
MAIN CONTENT
========================================================= -->

<section class="content">

    <div class="container-fluid">

        <div class="row justify-content-center">

            <div class="col-lg-9">

                <div class="card shadow-sm border-0">

                    <div class="card-body p-4">

                        <!-- TYPE -->

                        <div class="mb-4">

                            <?php if($notification['type'] == 'Success'): ?>

                                <span class="badge badge-success px-3 py-2">

                                    Success

                                </span>

                            <?php elseif($notification['type'] == 'Warning'): ?>

                                <span class="badge badge-warning px-3 py-2">

                                    Warning

                                </span>

                            <?php elseif($notification['type'] == 'Danger'): ?>

                                <span class="badge badge-danger px-3 py-2">

                                    Danger

                                </span>

                            <?php else: ?>

                                <span class="badge badge-info px-3 py-2">

                                    Info

                                </span>

                            <?php endif; ?>

                        </div>

                        <!-- TITLE -->

                        <h3 class="font-weight-bold mb-3">

                            <?= htmlspecialchars($notification['title']) ?>

                        </h3>

                        <!-- MESSAGE -->

                        <div class="notification-message mb-4">

                            <?= nl2br(htmlspecialchars($notification['message'])) ?>

                        </div>

                        <!-- DETAILS -->

                        <div class="row">

                            <div class="col-md-6 mb-3">

                                <strong>

                                    School:

                                </strong>

                                <br>

                                <?= htmlspecialchars($notification['school_name'] ?? '-') ?>

                            </div>

                            <div class="col-md-6 mb-3">

                                <strong>

                                    Status:

                                </strong>

                                <br>

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

                                    <span class="badge badge-success">

                                        Read

                                    </span>

                                <?php else: ?>

                                    <span class="badge badge-danger">

                                        Unread

                                    </span>

                                <?php endif; ?>

                            </div>

                            <div class="col-md-6 mb-3">

                                <strong>

                                    Created At:

                                </strong>

                                <br>

                                <?= date('d M Y h:i A', strtotime($notification['created_at'])) ?>

                            </div>

                            <div class="col-md-6 mb-3">

                                <strong>

                                    Notification ID:

                                </strong>

                                <br>

                                #<?= $notification['id'] ?>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.notification-message {

    background: #f8f9fa;
    padding: 20px;
    border-radius: 14px;
    font-size: 16px;
    line-height: 1.8;
}

.badge {

    border-radius: 30px;
    font-size: 12px;
}

.btn {

    border-radius: 10px;
}

</style>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>