<?php
include "koneksi.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id = intval($_POST['id']);

    // Cek apakah game dengan ID tersebut ada
    $qry = mysqli_query($koneksi, "SELECT * FROM list_game WHERE id = $id");
    $game = mysqli_fetch_array($qry);

    if ($game) {
        // Hapus gambar jika ada
        $gambar_path = $game['gambar'];
        if (!empty($gambar_path) && file_exists($gambar_path)) {
            unlink($gambar_path); // Hapus gambar dari folder
        }

        // Hapus data game dari database
        $delete = mysqli_query($koneksi, "DELETE FROM list_game WHERE id = $id");

        if ($delete) {
            header("Location: index.php?page=voucher&notif=berhasil_hapus_data");
            exit();
        } else {
            echo "Gagal menghapus game.";
        }
    } else {
        echo "Game tidak ditemukan.";
    }
}
?>
