<?php if(isset($_GET['msg'])): ?>

    <?php if($_GET['msg'] == 'success'): ?>
        <div class="alert success">Berhasil daftar event 🎉</div>
    <?php elseif($_GET['msg'] == 'already'): ?>
        <div class="alert warning">Kamu sudah daftar event ini</div>
    <?php endif; ?>

<?php endif; ?>
<?php
require __DIR__ . '/../layout/header.php';
require __DIR__ . '/../layout/sidebar.php';
?>

<div class="main">

    <div class="header">Event</div>

    <div class="grid-event">

        <?php foreach ($events as $event): ?>

            <div class="event-card">

                <!-- IMAGE -->
                <div class="event-img">

                    <img 
                        src="/membership/public/assets/uploads/<?= $event['foto'] ?>"
                        data-img="/membership/public/assets/uploads/<?= $event['foto'] ?>"
                        class="flyer-click"
                        onerror="this.src='/membership/public/assets/img/event-default.jpg'">

                </div>

                <!-- CONTENT -->
                <div class="event-content">

                    <h4><?= $event['nama_event'] ?></h4>

                    <span class="event-category">Event</span>

                    <div class="event-date">
                        <?= date('d M Y', strtotime($event['tanggal'])) ?>
                    </div>

                    <?php
                        // cek sudah daftar atau belum
                        $check = $conn->query("
                            SELECT id FROM event_registrations 
                            WHERE event_id = {$event['id']} 
                            AND member_id = {$_SESSION['user']['id']}
                        ");
                        
                        $isRegistered = $check->num_rows > 0;
                        ?>
                        
                        <?php if($isRegistered): ?>
                            <button class="btn-event" style="background:#94a3b8;">
                                Sudah Daftar
                            </button>
                        <?php else: ?>
                            <button 
                                class="btn-event btn-daftar"
                                data-id="<?= $event['id'] ?>">
                                Daftar
                            </button>
                        <?php endif; ?>

                </div>

            </div>

        <?php endforeach; ?>

    </div>

</div>

<!-- MODAL -->
<div id="flyerModal" class="flyer-modal">
    <span class="flyer-close">&times;</span>
    <img id="flyerImage" class="flyer-content">
</div>

<?php require __DIR__ . '/../layout/footer.php'; ?>