<?php
// File: /admin/hapus_pendaftar.php
session_start();
if (!isset($_SESSION['admin'])) {
  header("Location: login.php");
  exit;
}

include '../config/koneksi.php';

$id = intval($_GET['id']);
$query = mysqli_query($conn, "SELECT * FROM pendaftar WHERE id = $id");
$data = mysqli_fetch_assoc($query);

if ($data) {
  // Hapus file terkait
  @unlink("../uploads/" . $data['file_kk']);
  @unlink("../uploads/" . $data['file_akte']);
  @unlink("../uploads/" . $data['file_ktp']);
  if (!empty($data['file_sertifikat'])) {
    @unlink("../uploads/" . $data['file_sertifikat']);
  }

  // Hapus data dari database
  $hapus = mysqli_query($conn, "DELETE FROM pendaftar WHERE id = $id");
  if ($hapus) {
    echo "<script>alert('Data berhasil dihapus.'); window.location='data_pendaftar.php';</script>";
  } else {
    echo "<script>alert('Gagal menghapus data.'); window.history.back();</script>";
  }
} else {
  echo "<script>alert('Data tidak ditemukan.'); window.history.back();</script>";
}