<?php

session_start();

require_once "../../config/koneksi.php";

if (!isset($_SESSION['user_id'])) {
    header("Location: ../login.php");
    exit;
}

$id = $_GET['id'];

// Ambil nama gambar
$query = mysqli_query($koneksi, "SELECT gambar FROM galeri WHERE id='$id'");
$data = mysqli_fetch_assoc($query);

// Hapus file gambar jika ada
if (!empty($data['gambar'])) {

    $file = "../../uploads/galeri/" . $data['gambar'];

    if (file_exists($file)) {
        unlink($file);
    }
}

// Hapus data dari database
$hapus = mysqli_query($koneksi, "DELETE FROM galeri WHERE id='$id'");

if ($hapus) {
    header("Location: index.php");
    exit;
} else {
    echo mysqli_error($koneksi);
}
