<?php require dirname(__DIR__) . '/layouts/app.php'; ?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Alertes</p>
        <h1>Notifications</h1>
    </div>
    <div class="heading-actions">
        <?php if (Auth::can('users.view')): ?>
            <a class="button-link secondary" href="<?= url('/notifications/create-alert') ?>">Alerte importante</a>
        <?php endif; ?>
        <form method="post" action="<?= url('/notifications/read-all') ?>">
            <input type="hidden" name="_token" value="<?= csrf_token() ?>">
            <button type="submit">Tout marquer comme lu</button>
        </form>
    </div>
</section>

<?php if (!empty($notice)): ?>
    <div class="alert alert-success"><?= e($notice) ?></div>
<?php endif; ?>

<section class="notification-list">
    <?php foreach ($notifications as $notification): ?>
        <article class="notification-item <?= (int) $notification['is_read'] === 1 ? 'is-read' : '' ?>">
            <div>
                <span class="status-pill notification-type"><?= e(notification_types()[$notification['type']] ?? $notification['type']) ?></span>
                <h2><?= e($notification['title']) ?></h2>
                <p><?= e($notification['message']) ?></p>
                <small><?= e($notification['created_at']) ?></small>
            </div>
            <div class="notification-actions">
                <?php if ($notification['related_type'] === 'reservation'): ?>
                    <a href="<?= url('/reservations/show?id=' . $notification['related_id']) ?>">Voir</a>
                <?php elseif ($notification['related_type'] === 'payment'): ?>
                    <a href="<?= url('/payments/show?id=' . $notification['related_id']) ?>">Voir</a>
                <?php endif; ?>

                <?php if ((int) $notification['is_read'] === 0): ?>
                    <form method="post" action="<?= url('/notifications/read') ?>">
                        <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                        <input type="hidden" name="id" value="<?= (int) $notification['id'] ?>">
                        <button type="submit">Lu</button>
                    </form>
                <?php endif; ?>
            </div>
        </article>
    <?php endforeach; ?>

    <?php if (empty($notifications)): ?>
        <article class="empty-state">Aucune notification pour le moment.</article>
    <?php endif; ?>
</section>

<?php require dirname(__DIR__) . '/partials/pagination.php'; ?>

<?php require dirname(__DIR__) . '/layouts/app_footer.php'; ?>
