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

$result = $conn->query("SELECT * FROM events ORDER BY tanggal DESC");

$events = [];

while ($row = $result->fetch_assoc()) {
    $events[] = $row;
}
?>

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

<div class="main">

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

    <!-- ================= TAMBAH EVENT ================= -->
    <div class="card">

        <h3>Tambah Event</h3>

        <form action="/membership/event-store" method="POST" enctype="multipart/form-data">

            <div class="form-group">
                <input type="text" name="nama_event" placeholder="Nama Event" required>
            </div>

            <div class="form-group">
                <textarea name="deskripsi" placeholder="Deskripsi"></textarea>
            </div>

            <div class="form-group">
                <input type="date" name="tanggal" required>
            </div>

            <div class="form-group">
                <input type="text" name="lokasi" placeholder="Lokasi" required>
            </div>

            <div class="form-group">
                <input type="file" name="foto">
            </div>

            <button class="btn btn-primary">Simpan Event</button>

        </form>

    </div>

    <!-- ================= LIST EVENT ================= -->
    <div class="card">
        <h3>Daftar Event</h3>
    
        <table class="table">
            <thead>
                <tr>
                    <th>Foto</th>
                    <th>Nama</th>
                    <th>Tanggal</th>
                    <th>Lokasi</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
    
            <?php if (!empty($events)): ?>
                <?php foreach ($events as $row): ?>
                    <tr>
    
                        <!-- FOTO -->
                        <td>
                            <?php if (!empty($row['foto'])): ?>
                                <img src="/membership/public/assets/uploads/<?= $row['foto'] ?>" 
                                     style="width:60px; height:80px; object-fit:cover; border-radius:8px;">
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
    
                        <!-- NAMA -->
                        <td><?= htmlspecialchars($row['nama_event']) ?></td>
    
                        <!-- TANGGAL -->
                        <td><?= date('d M Y', strtotime($row['tanggal'])) ?></td>
    
                        <!-- LOKASI -->
                        <td><?= htmlspecialchars($row['lokasi']) ?></td>
    
                        <!-- AKSI -->
                        <td>
                            <a href="/membership/event-edit?id=<?= $row['id'] ?>" 
                               class="btn btn-warning">Edit</a>
    
                            <a href="/membership/event-delete?id=<?= $row['id'] ?>" 
                               class="btn btn-danger"
                               onclick="return confirm('Yakin hapus event ini?')">
                               Hapus
                            </a>
    
                            <a href="/membership/event-qr?id=<?= $row['id'] ?>" 
                               class="btn btn-primary">
                               QR Code
                            </a>
                            
                            <a href="/membership/event-peserta?id=<?= $row['id'] ?>" 
                               class="btn btn-primary">
                               Lihat Peserta
                            </a>
                        </td>
    
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="5" style="text-align:center;">Tidak ada event</td>
                </tr>
            <?php endif; ?>
    
            </tbody>
        </table>
    </div>

</div>

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