<?php

session_start();

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

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

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

// allow_super_admin();

$page_title = 'Notifications';

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

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

/*
|--------------------------------------------------------------------------
| 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);

?>

<!-- =========================================================
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">

                    Notifications

                </h1>

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

                    Manage all SaaS notifications

                </p>

            </div>

            <div class="d-flex">

                <a
                    href="mark_all_read.php"
                    class="btn btn-primary mr-2"
                >

                    <i class="fas fa-check-double mr-1"></i>

                    Mark All Read

                </a>

                <a
                    href="notification_settings.php"
                    class="btn btn-info mr-2"
                >

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

                    Settings

                </a>

                <a
                    href="trash.php"
                    class="btn btn-danger"
                >

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

                    Trash

                </a>

            </div>

        </div>

    </div>

</section>

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

<section class="content">

    <div class="container-fluid">

        <?php if(isset($_SESSION['success'])): ?>

            <div class="alert alert-success">

                <?= $_SESSION['success']; ?>

            </div>

            <?php unset($_SESSION['success']); ?>

        <?php endif; ?>

        <?php if(isset($_SESSION['error'])): ?>

            <div class="alert alert-danger">

                <?= $_SESSION['error']; ?>

            </div>

            <?php unset($_SESSION['error']); ?>

        <?php endif; ?>

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

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

                <div class="table-responsive">

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

                        <thead class="bg-light">

                            <tr>

                                <th width="80">

                                    ID

                                </th>

                                <th width="120">

                                    Type

                                </th>

                                <th>

                                    Title

                                </th>

                                <th>

                                    School

                                </th>

                                <th width="180">

                                    Date

                                </th>

                                <th width="120">

                                    Status

                                </th>

                                <th width="180">

                                    Action

                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if($notifications): ?>

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

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

                                        <!-- ID -->

                                        <td>

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

                                        </td>

                                        <!-- 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>

                                            <br>

                                            <small class="text-muted">

                                                <?= htmlspecialchars(substr($notification['message'], 0, 70)) ?>...

                                            </small>

                                        </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>

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

                                                <i class="fas fa-eye"></i>

                                            </a>

                                            <a
                                                href="delete.php?id=<?= $notification['id'] ?>"
                                                class="btn btn-sm btn-danger"
                                                onclick="return confirm('Delete this notification?')"
                                            >

                                                <i class="fas fa-trash"></i>

                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

                                    <td colspan="7" class="text-center py-5">

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

                                        <h5>

                                            No Notifications Found

                                        </h5>

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </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'; ?>