<?php
include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

$id_organisasi = isset($_GET['id_organisasi']) ? intval($_GET['id_organisasi']) : 0;
if ($id_organisasi <= 0) {
    header('Location: manage_organisasi.php');
    exit;
}

// Get the image filename before deleting
$query = "SELECT gambar FROM tb_organisasi WHERE id_organisasi = $id_organisasi";
$result = mysqli_query($host, $query);
if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $gambar = $row['gambar'];

    // Delete the record from database
    $delete_query = "DELETE FROM tb_organisasi WHERE id_organisasi = $id_organisasi";
    if (mysqli_query($host, $delete_query)) {
        // Delete the image file if it exists
        if (!empty($gambar) && file_exists("upload/" . $gambar)) {
            unlink("upload/" . $gambar);
        }
        header('Location: manage_organisasi.php');
        exit;
    } else {
        echo "Error deleting record: " . mysqli_error($host);
    }
} else {
    header('Location: manage_organisasi.php');
    exit;
}
?>

