<?php include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

$id_prestasi = isset($_GET['id_prestasi']) ? intval($_GET['id_prestasi']) : 0;

if ($id_prestasi == 0) {
    header('Location: manage_prestasi.php');
    exit;
}

// First, get the image filename to delete it from upload folder
$query = "SELECT gambar_prestasi FROM tb_prestasi WHERE id_prestasi = $id_prestasi";
$result = mysqli_query($host, $query);
if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $gambar = $row['gambar_prestasi'];

    // Delete the record from database
    $delete_query = "DELETE FROM tb_prestasi WHERE id_prestasi = $id_prestasi";
    if (mysqli_query($host, $delete_query)) {
        // Delete the image file if it exists
        if (!empty($gambar) && file_exists("upload/" . $gambar)) {
            unlink("upload/" . $gambar);
        }
        echo "<script>alert('Prestasi berhasil dihapus!'); window.location.href='manage_prestasi.php';</script>";
    } else {
        echo "<script>alert('Error: " . mysqli_error($host) . "');</script>";
    }
} else {
    echo "<script>alert('Prestasi tidak ditemukan!'); window.location.href='manage_prestasi.php';</script>";
}
?>

