<?php $this->layout('main'); ?>

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Notifications</h2>
        <form method="post" action="<?= APP_URL ?>/notifications/read-all" class="d-inline">
            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
            <button type="submit" class="btn btn-outline-primary">Mark All Read</button>
        </form>
    </div>

    <!-- Filters -->
    <div class="mb-4">
        <a href="<?= APP_URL ?>/notifications" class="btn btn-sm <?= !$unreadOnly ? 'btn-primary' : 'btn-outline-secondary' ?>">All</a>
        <a href="<?= APP_URL ?>/notifications?filter=unread" class="btn btn-sm <?= $unreadOnly ? 'btn-primary' : 'btn-outline-secondary' ?>">Unread</a>
    </div>

    <!-- Notifications List -->
    <div class="card">
        <ul class="list-group list-group-flush">
            <?php if (empty($notifications)): ?>
                <li class="list-group-item text-center text-muted py-4">No notifications found.</li>
            <?php else: ?>
                <?php foreach ($notifications as $notification): ?>
                    <li class="list-group-item <?= !$notification['is_read'] ? 'bg-light' : '' ?>">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <i class="bi bi-bell-fill text-primary me-2"></i>
                                <strong><?= htmlspecialchars($notification['title']) ?></strong>
                                <p class="mb-1 text-muted"><?= htmlspecialchars($notification['message']) ?></p>
                                <?php 
                                    $data = json_decode($notification['data'], true);
                                    if (!empty($data['link'])): 
                                ?>
                                    <a href="<?= htmlspecialchars($data['link']) ?>" class="btn btn-link btn-sm p-0">View Related</a>
                                <?php endif; ?>
                            </div>
                            <div class="text-end">
                                <small class="text-muted d-block mb-2"><?= htmlspecialchars($notification['created_at']) ?></small>
                                <?php if (!$notification['is_read']): ?>
                                    <form method="post" action="<?= APP_URL ?>/notifications/<?= $notification['id'] ?>/read" class="d-inline">
                                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
                                        <button type="submit" class="btn btn-sm btn-success" title="Mark as read"><i class="bi bi-check"></i></button>
                                    </form>
                                <?php endif; ?>
                                <form method="post" action="<?= APP_URL ?>/notifications/<?= $notification['id'] ?>/delete" class="d-inline">
                                    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
                                    <button type="submit" class="btn btn-sm btn-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                        </div>
                    </li>
                <?php endforeach; ?>
            <?php endif; ?>
        </ul>
    </div>

    <!-- Pagination -->
    <div class="mt-4 d-flex justify-content-center">
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                    <a class="page-link" href="?page=<?= $page - 1 ?><?= $unreadOnly ? '&filter=unread' : '' ?>">Previous</a>
                </li>
                <li class="page-item">
                    <span class="page-link">Page <?= $page ?></span>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page=<?= $page + 1 ?><?= $unreadOnly ? '&filter=unread' : '' ?>">Next</a>
                </li>
            </ul>
        </nav>
    </div>
</div>

