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

include 'includes/config.php';

$id = $_GET['id'] ?? null;

if ($id) {
    // Semak sama ada aktiviti berkaitan dengan penyertaan
    $check_stmt = mysqli_prepare($conn, "SELECT COUNT(*) AS count FROM penyertaan WHERE aktiviti_id = ?");
    mysqli_stmt_bind_param($check_stmt, "i", $id);
    mysqli_stmt_execute($check_stmt);
    $check_result = mysqli_stmt_get_result($check_stmt);
    $row = mysqli_fetch_assoc($check_result);

    if ($row['count'] > 0) {
        echo "<script>alert('Aktiviti ini tidak boleh dipadam kerana terdapat penyertaan berkaitan.'); window.location='senarai_aktiviti.php';</script>";
        exit;
    }

    // Padam aktiviti
    $stmt = mysqli_prepare($conn, "DELETE FROM aktiviti WHERE id = ?");
    mysqli_stmt_bind_param($stmt, "i", $id);
    if (mysqli_stmt_execute($stmt)) {
        echo "<script>alert('Aktiviti berjaya dipadam.'); window.location='senarai_aktiviti.php';</script>";
        exit;
    } else {
        echo "<script>alert('Ralat semasa memadam aktiviti.'); window.location='senarai_aktiviti.php';</script>";
        exit;
    }
} else {
    echo "<script>alert('ID aktiviti tidak sah.'); window.location='senarai_aktiviti.php';</script>";
    exit;
}
