<?php
include 'koneksi.php';

if (isset($_POST['id_paket'])) {
    $id_paket = $_POST['id_paket'];

    // Cek referensi di membersip_order
    $cek_order = mysqli_query($conn, "SELECT COUNT(*) AS total_order FROM membersip_order WHERE membership_id = '$id_paket'");
    $data_order = mysqli_fetch_assoc($cek_order);

    // Cek referensi di member_subscriptions
    $cek_sub = mysqli_query($conn, "SELECT COUNT(*) AS total_sub FROM member_subscriptions WHERE package_id = '$id_paket'");
    $data_sub = mysqli_fetch_assoc($cek_sub);

    if ($data_order['total_order'] == 0 && $data_sub['total_sub'] == 0) {
        // Paket tidak dipakai di mana pun: Hapus
        $query = "DELETE FROM membership_packages WHERE package_id = '$id_paket'";
        if (mysqli_query($conn, $query)) {
            echo "<script>alert('Paket berhasil dihapus.'); window.location.href='../Paket/p_membership.php';</script>"; // TC120
        } else {
            echo "<script>alert('Gagal menghapus paket: " . mysqli_error($conn) . "');</script>";
        }
    } else {
        echo "<script>alert('Gagal menghapus paket karena sedang dipakai.'); window.location.href='../Paket/p_membership.php';</script>"; // TC121
    }
} else {
    echo "<script>alert('ID paket tidak ditemukan.'); window.location.href='mainP_M.php';</script>";
}

?>
