<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include '_db.php';

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $plan_title = $_POST['plan_title'];
    $minimum = $_POST['minimum'];
    $maximum = $_POST['maximum'];
    $profit = $_POST['profit'];
    $roi = $_POST['roi'];
    $days = $_POST['days'];
    $plan_type = $_POST['plan_type']; // Get the plan type

    if (isset($_POST['btnSave'])) {
        // Insert data into the database
        $sql = $conn->query("INSERT INTO plans (plan_title, minimum, maximum, profit, roi, days, plan_type) 
        VALUES ('$plan_title', '$minimum', '$maximum', '$profit', '$roi', '$days', '$plan_type')");
        
        if ($sql) {
            header("Location: manage-plans.php");
        } else {
            echo "Database error: " . $conn->error;
        }
    }

    if (isset($_POST['btnSaveUpdate'])) {
        $id = $_POST['id'];
        $sql = $conn->query("UPDATE plans SET plan_title='$plan_title', minimum='$minimum', maximum='$maximum', profit='$profit', roi='$roi', days='$days', plan_type='$plan_type' WHERE id='$id'");
        
        if ($sql) {
            header("Location: manage-plans.php");
        } else {
            echo "Database error: " . $conn->error;
        }
    }
}
?>
