<?php
include "koneksi.php";

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

    // Ambil data gambar sebelum menghapus
    $qry = mysqli_query($koneksi, "SELECT gambar FROM slider_banner WHERE id = $id");
    $slider = mysqli_fetch_array($qry);
    if ($slider) {
        $gambar_path = $slider['gambar'];
        // Hapus file gambar dari folder
        if (file_exists($gambar_path)) {
            unlink($gambar_path);
        }

        // Hapus data dari database
        $delete = mysqli_query($koneksi, "DELETE FROM slider_banner WHERE id = $id");
        if ($delete) {
            header("Location: index.php?page=atur_slider&notif=berhasil_hapus_data");
            exit();
        } else {
            echo "Gagal menghapus data: " . mysqli_error($koneksi);
        }
    } else {
        echo "Data tidak ditemukan.";
    }
}
?>