<?php
include "koneksi.php";

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

    // Ambil data gambar sebelum menghapus
    $qry = mysqli_query($koneksi, "SELECT gambar FROM slider_kanan WHERE id = $id");
    $slider = mysqli_fetch_array($qry);

    if ($slider) {
        $gambar_path = $slider['gambar'];

        // Hapus file gambar dari folder jika ada
        if (!empty($gambar_path) && file_exists($gambar_path)) {
            unlink($gambar_path);
        }

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

        if ($delete) {
            header("Location: index.php?page=slider_kanan&notif=berhasil_hapus_data");
            exit();
        } else {
            header("Location: index.php?page=slider_kanan&notif=gagal_hapus_data");
            exit();
        }
    } else {
        header("Location: index.php?page=slider_kanan&notif=data_tidak_ditemukan");
        exit();
    }
}
?>