<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

require __DIR__ . '/../../app/config/database.php';

if (!isset($_GET['id'])) {
    die("ID event tidak ditemukan");
}

$event_id = (int) $_GET['id'];

$event = $conn->query("SELECT * FROM events WHERE id=$event_id")->fetch_assoc();

$data = $conn->query("
    SELECT er.*, m.nama, m.no_hp
    FROM event_registrations er
    JOIN members m ON m.id = er.member_id
    WHERE er.event_id = $event_id
");
?>

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


<div class="dashboard-layout">

    <!-- SIDEBAR -->
   <?php
require __DIR__ . '/../layout/sidebar.php';
?>


    <!-- MAIN -->
    <div class="main">

        <!-- HEADER -->
        <div class="dashboard-header">
            <h2>Peserta Event</h2>
        </div>

        <!-- EVENT INFO -->
        <div class="card">
            <div class="event-info">
                <div>
                    <h3><?= htmlspecialchars($event['nama_event'] ?? '-') ?></h3>
                    <p><?= date('d M Y', strtotime($event['tanggal'] ?? 'now')) ?></p>
                    <span><?= htmlspecialchars($event['lokasi'] ?? '-') ?></span>
                </div>

                <a href="/membership/event-export?id=<?= $event_id ?>" class="btn btn-success">
                    Export Excel
                </a>
            </div>
        </div>

        <!-- TABLE -->
        <div class="card">
            <table class="table-modern">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Nama</th>
                        <th>No HP</th>
                        <th>Status</th>
                        <th>Check-in</th>
                        <th>Aksi</th>
                    </tr>
                </thead>

                <tbody>

                <?php $no = 1; ?>
                <?php if ($data->num_rows > 0): ?>
                    <?php while ($row = $data->fetch_assoc()): ?>

                    <tr>
                        <td><?= $no++ ?></td>
                        <td><?= htmlspecialchars($row['nama']) ?></td>
                        <td><?= htmlspecialchars($row['no_hp']) ?></td>

                        <!-- STATUS -->
                        <td>
                            <span class="badge <?= $row['status'] == 'approved' ? 'success' : 'warning' ?>">
                                <?= ucfirst($row['status'] ?? 'pending') ?>
                            </span>
                        </td>

                        <!-- CHECKIN -->
                        <td>
                            <span class="badge <?= $row['checkin'] ? 'success' : 'danger' ?>">
                                <?= $row['checkin'] ? 'Hadir' : 'Belum' ?>
                            </span>
                        </td>

                        <!-- AKSI -->
                        <td class="aksi">
                            <a href="/membership/event-approve?id=<?= $row['id'] ?>&status=approved" class="btn-sm success">✔</a>
                            <a href="/membership/event-approve?id=<?= $row['id'] ?>&status=rejected" class="btn-sm danger">✖</a>
                        </td>
                    </tr>

                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" style="text-align:center;">Belum ada peserta</td>
                    </tr>
                <?php endif; ?>

                </tbody>
            </table>
        </div>

    </div>
</div>