<?php
session_start();
include "../config/koneksi.php";

if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit;
}

// HAPUS DATA
if(isset($_GET['hapus'])){
    $id = $_GET['hapus'];
    mysqli_query($conn, "DELETE FROM pendaftaran WHERE id='$id'");
    header("Location: data_peserta.php");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Data Peserta</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
@media print {
    .no-print {
        display: none;
    }
}
</style>

</head>

<body>

<div class="container mt-5">

<h3>Data Peserta MTCNA</h3>

<!-- Tombol -->
<div class="mb-3 no-print">
    <button onclick="window.print()" class="btn btn-success">🖨 Cetak</button>
</div>

<table class="table table-bordered table-striped mt-3">
<tr>
    <th>No</th>
    <th>Nama</th>
    <th>Email</th>
    <th>No HP</th>
    <th>Kelas</th>
    <th>Tanggal</th>
    <th class="no-print">Aksi</th>
</tr>

<?php
$no = 1;
$data = mysqli_query($conn,"SELECT * FROM pendaftaran ORDER BY id DESC");

while($d = mysqli_fetch_array($data)){
?>

<tr>
    <td><?= $no++ ?></td>
    <td><?= $d['nama'] ?></td>
    <td><?= $d['email'] ?></td>
    <td><?= $d['no_hp'] ?></td>
    <td><?= $d['kelas'] ?></td>
    <td><?= isset($d['tanggal']) ? $d['tanggal'] : '-' ?></td>
    <td class="no-print">
        <a href="?hapus=<?= $d['id'] ?>" 
           class="btn btn-danger btn-sm"
           onclick="return confirm('Yakin ingin menghapus data ini?')">
           Hapus
        </a>
    </td>
</tr>

<?php } ?>

</table>

</div>

</body>
</html>