<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <ul class="nav nav-pills">
        <li class="nav-item"><a class="nav-link <?= $tab === 'all' ? 'active' : '' ?>" href="<?= site_url('notifications?tab=all') ?>">All</a></li>
        <li class="nav-item"><a class="nav-link <?= $tab === 'unread' ? 'active' : '' ?>" href="<?= site_url('notifications?tab=unread') ?>">Unread <?= $unreadCount > 0 ? '(' . $unreadCount . ')' : '' ?></a></li>
    </ul>
    <?php if ($unreadCount > 0): ?>
        <form action="<?= site_url('notifications/mark-read') ?>" method="post">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-sm btn-outline-secondary">Mark All Read</button>
        </form>
    <?php endif; ?>
</div>

<div class="card">
    <ul class="list-group list-group-flush">
        <?php if (empty($notifications)): ?>
            <li class="list-group-item text-center text-muted py-5"><?= $tab === 'unread' ? 'No unread notifications.' : 'No notifications yet.' ?></li>
        <?php endif; ?>
        <?php foreach ($notifications as $n): ?>
            <li class="list-group-item d-flex justify-content-between align-items-center <?= $n->is_read ? '' : 'bg-light' ?>">
                <div class="d-flex align-items-center gap-2">
                    <?php if (! $n->is_read): ?><span class="badge bg-primary" style="width:8px;height:8px;border-radius:50%;padding:0;"></span><?php endif; ?>
                    <?php if ($n->link): ?>
                        <a href="<?= site_url($n->link) ?>" class="text-decoration-none <?= $n->is_read ? 'text-muted' : 'fw-semibold' ?>"><?= esc($n->message) ?></a>
                    <?php else: ?>
                        <span class="<?= $n->is_read ? 'text-muted' : 'fw-semibold' ?>"><?= esc($n->message) ?></span>
                    <?php endif; ?>
                </div>
                <div class="d-flex align-items-center gap-2">
                    <span class="text-muted small"><?= esc(date('d M Y, g:i a', strtotime($n->created_at))) ?></span>
                    <?php if (! $n->is_read): ?>
                        <form action="<?= site_url('notifications/' . $n->id . '/mark-read') ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-secondary">Mark Read</button>
                        </form>
                    <?php endif; ?>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>
    <?php if (! empty($notifications)): ?>
        <div class="card-footer bg-white d-flex justify-content-between align-items-center">
            <span class="text-muted small">Showing <?= count($notifications) ?> of <?= $pager->getTotal() ?> results</span>
            <?= $pager->links() ?>
        </div>
    <?php endif; ?>
</div>

<?= $this->endSection() ?>
