<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Notifications';

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

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

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

if (isset($_GET['read'])) {

    $notification_id = (int) $_GET['read'];

    $update_stmt = $pdo->prepare("

        UPDATE super_admin_notifications

        SET is_read = 1

        WHERE id = ?

    ");

    $update_stmt->execute([$notification_id]);

    header('Location: index.php');

    exit;
}

/*
|--------------------------------------------------------------------------
| FETCH NOTIFICATIONS
|--------------------------------------------------------------------------
*/

$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

    ORDER BY n.id DESC

");

$stmt->execute();

$notifications = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- =========================================================
PAGE HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-3">

            <div class="col-sm-6">

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

                    Notifications

                </h1>

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

                    SaaS alerts and subscription notifications

                </p>

            </div>

        </div>

    </div>

</section>

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

<section class="content">

    <div class="container-fluid">

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

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

                <?php if($notifications): ?>

                    <div class="table-responsive">

                        <table class="table table-hover table-bordered mb-0">

                            <thead class="bg-light">

                                <tr>

                                    <th width="100">

                                        Type

                                    </th>

                                    <th>

                                        Title

                                    </th>

                                    <th>

                                        Message

                                    </th>

                                    <th>

                                        School

                                    </th>

                                    <th width="160">

                                        Date

                                    </th>

                                    <th width="120">

                                        Status

                                    </th>

                                    <th width="120">

                                        Action

                                    </th>

                                </tr>

                            </thead>

                            <tbody>

                                <?php foreach($notifications as $notification): ?>

                                    <tr class="<?= !$notification['is_read'] ? 'unread-row' : '' ?>">

                                        <!-- TYPE -->

                                        <td>

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

                                                <span class="badge badge-success">

                                                    Success

                                                </span>

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

                                                <span class="badge badge-warning">

                                                    Warning

                                                </span>

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

                                                <span class="badge badge-danger">

                                                    Danger

                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-info">

                                                    Info

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                        <!-- TITLE -->

                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>

                                        <!-- MESSAGE -->

                                        <td>

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

                                        </td>

                                        <!-- SCHOOL -->

                                        <td>

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

                                        </td>

                                        <!-- DATE -->

                                        <td>

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

                                        </td>

                                        <!-- STATUS -->

                                        <td>

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

                                                <span class="badge badge-success">

                                                    Read

                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-danger">

                                                    Unread

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                        <!-- ACTION -->

                                        <td>

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

                                                <a
                                                    href="?read=<?= $notification['id'] ?>"
                                                    class="btn btn-sm btn-primary"
                                                >

                                                    Mark Read

                                                </a>

                                            <?php else: ?>

                                                <span class="text-muted">

                                                    Completed

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            </tbody>

                        </table>

                    </div>

                <?php else: ?>

                    <div class="text-center py-5">

                        <i class="fas fa-bell-slash fa-4x text-muted mb-3"></i>

                        <h4>

                            No Notifications Found

                        </h4>

                        <p class="text-muted">

                            All SaaS notifications will appear here.

                        </p>

                    </div>

                <?php endif; ?>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.table {

    border-radius: 14px;
    overflow: hidden;
}

.badge {

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

.btn {

    border-radius: 10px;
}

.unread-row {

    background: #fff8e1;
}

</style>

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