<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

// Pastikan ID ada
if (!isset($_GET['id'])) {
    header("Location: semua_kegiatan.php");
    exit;
}

$id = intval($_GET['id']);

// Ambil dulu data untuk hapus foto
$q = $koneksi->prepare("SELECT foto FROM kegiatan WHERE id = ?");
$q->bind_param("i", $id);
$q->execute();
$res = $q->get_result();

if ($res->num_rows == 0) {
    header("Location: semua_kegiatan.php?msg=notfound");
    exit;
}

$data = $res->fetch_assoc();
$foto = $data['foto'];

// Hapus dari database
$del = $koneksi->prepare("DELETE FROM kegiatan WHERE id = ?");
$del->bind_param("i", $id);

if ($del->execute()) {

    // Hapus foto jika ada di folder
    if (!empty($foto)) {
        $filePath = __DIR__ . "/../../assets/img/kegiatan/" . $foto;
        if (file_exists($filePath)) {
            unlink($filePath);
        }
    }

    header("Location: semua_kegiatan.php?deleted=1");
    exit;
} else {
    header("Location: semua_kegiatan.php?error=1");
    exit;
}

?>
