<?php
include 'koneksi.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $package_id = $_POST['package_id'];
    $package_name = $_POST['package_name'];
    $description = $_POST['description'];
    $price = $_POST['price'];

    $durationValue = $_POST['durationValue'];
    $durationUnit = $_POST['durationUnit'];

    $duration = $durationValue . " " . $durationUnit;

    if (empty($package_id) || empty($package_name) || empty($description) || empty($price) || empty($durationValue) || empty($durationUnit)) {
        die("Semua field harus diisi!");
    }

    $sql = "UPDATE membership_packages SET 
                package_name = ?, 
                description = ?, 
                price = ?, 
                duration = ? 
            WHERE package_id = ?";

    $stmt = $conn->prepare($sql);

    if ($stmt === false) {
        die("Gagal prepare statement: " . $conn->error);
    }

    $stmt->bind_param("ssisi", $package_name, $description, $price, $duration, $package_id);

    if ($stmt->execute()) {
        header("Location: ../Paket/p_membership.php?status=update_sukses");
        exit();
    } else {
        echo "Gagal update: " . $stmt->error;
    }

    $stmt->close();
} else {
    header("Location: ../Paket/p_membership.php");
    exit();
}

$conn->close();
?>
