<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Deleted Notifications';

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

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

/*
|--------------------------------------------------------------------------
| DELETE NOTIFICATION
|--------------------------------------------------------------------------
*/

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

    $delete_id = (int)$_GET['delete'];

    $deleteStmt = $pdo->prepare("

        DELETE FROM super_admin_notifications

        WHERE id = ?

    ");

    $deleteStmt->execute([$delete_id]);

    header('Location: trash.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);

?>

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

                    Deleted Notifications

                </h1>

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

                    Manage and delete notifications permanently

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

                                    Title

                                </th>

                                <th>

                                    School

                                </th>

                                <th width="120">

                                    Type

                                </th>

                                <th width="180">

                                    Date

                                </th>

                                <th width="140">

                                    Action

                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if($notifications): ?>

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

                                    <tr>

                                        <td>

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

                                        </td>

                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>

                                        <td>

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

                                        </td>

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

                                        <td>

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

                                        </td>

                                        <td>

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

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

                                                Delete

                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

                                    <td colspan="6" 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;
}

</style>

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