<?php
function na_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$notifications = $notifications ?? [];
$stats = $stats ?? [];
$q = $_GET['q'] ?? '';
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-bell-circle-check"></i> Notification Audit</h3>
            <div class="opacity-75">Monitor all in-app notifications for this conference.</div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4"><div class="card border-0 shadow-sm" style="border-radius:20px;"><div class="card-body text-center"><div class="fs-2 fw-bold"><?= (int)($stats['total'] ?? 0) ?></div><div class="text-muted">Total</div></div></div></div>
        <div class="col-md-4"><div class="card border-0 shadow-sm" style="border-radius:20px;"><div class="card-body text-center"><div class="fs-2 fw-bold text-danger"><?= (int)($stats['unread'] ?? 0) ?></div><div class="text-muted">Unread</div></div></div></div>
        <div class="col-md-4"><div class="card border-0 shadow-sm" style="border-radius:20px;"><div class="card-body text-center"><div class="fs-2 fw-bold text-success"><?= (int)($stats['read_count'] ?? 0) ?></div><div class="text-muted">Read</div></div></div></div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="get" action="/easychair/admin/notification-audit" class="row g-2">
                <div class="col-md-10">
                    <input name="q" value="<?= na_h($q) ?>" class="form-control" placeholder="Search user email, submission no, title, event...">
                </div>
                <div class="col-md-2">
                    <button class="btn btn-primary w-100">Search</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>User</th>
                            <th>Submission</th>
                            <th>Event</th>
                            <th>Title</th>
                            <th>Read</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($notifications as $n): ?>
                        <tr>
                            <td><?= (int)$n['id'] ?></td>
                            <td>
                                <?= na_h(trim($n['user_name']) ?: $n['email']) ?>
                                <div class="text-muted small"><?= na_h($n['email']) ?></div>
                            </td>
                            <td>
                                <?= na_h($n['submission_no'] ?? '') ?>
                                <div class="text-muted small"><?= na_h($n['submission_title'] ?? '') ?></div>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?= na_h($n['notification_type']) ?></span>
                                <span class="badge bg-secondary"><?= na_h($n['event_key']) ?></span>
                            </td>
                            <td><?= na_h($n['title']) ?></td>
                            <td>
                                <span class="badge <?= empty($n['is_read']) ? 'bg-danger' : 'bg-success' ?>">
                                    <?= empty($n['is_read']) ? 'Unread' : 'Read' ?>
                                </span>
                            </td>
                            <td><?= na_h($n['created_at']) ?></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$notifications): ?>
                        <tr><td colspan="7" class="text-center text-muted py-4">No notifications found.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
