35251-vm/edit_plan.php
2025-10-26 16:53:16 +00:00

101 lines
3.6 KiB
PHP

<?php
require_once 'db/config.php';
$title = 'Edit Plan - Billing';
$page = 'plans';
require_once 'templates/header.php';
$plan_id = $_GET['id'] ?? null;
$error_message = '';
$success_message = '';
$plan = null;
if (!$plan_id) {
header('Location: plans.php');
exit;
}
try {
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name']);
$price = trim($_POST['price']);
$billing_cycle = trim($_POST['billing_cycle']);
$features = trim($_POST['features']);
if (empty($name) || empty($price) || empty($billing_cycle)) {
$error_message = 'Please fill in all required fields.';
} elseif (!is_numeric($price)) {
$error_message = 'Price must be a number.';
} else {
$features_json = json_encode(array_map('trim', explode("\n", $features)));
$stmt = $pdo->prepare('UPDATE plans SET name = ?, price = ?, billing_cycle = ?, features = ? WHERE id = ?');
$stmt->execute([$name, $price, $billing_cycle, $features_json, $plan_id]);
$success_message = 'Plan updated successfully!';
}
}
$stmt = $pdo->prepare('SELECT * FROM plans WHERE id = ?');
$stmt->execute([$plan_id]);
$plan = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$plan) {
header('Location: plans.php');
exit;
}
} catch (PDOException $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Edit Plan</h1>
</div>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($success_message); ?></div>
<?php endif; ?>
<?php if ($plan): ?>
<form action="edit_plan.php?id=<?php echo htmlspecialchars($plan_id); ?>" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Plan Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($plan['name']); ?>" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="price" class="form-label">Price</label>
<div class="input-group">
<span class="input-group-text">$</span>
<input type="number" step="0.01" class="form-control" id="price" name="price" value="<?php echo htmlspecialchars($plan['price']); ?>" required>
</div>
</div>
<div class="col-md-6 mb-3">
<label for="billing_cycle" class="form-label">Billing Cycle</label>
<select class="form-select" id="billing_cycle" name="billing_cycle" required>
<option value="monthly" <?php echo ($plan['billing_cycle'] === 'monthly') ? 'selected' : ''; ?>>Monthly</option>
<option value="yearly" <?php echo ($plan['billing_cycle'] === 'yearly') ? 'selected' : ''; ?>>Yearly</option>
</select>
</div>
</div>
<div class="mb-3">
<label for="features" class="form-label">Features (one per line)</label>
<textarea class="form-control" id="features" name="features" rows="5" required><?php echo htmlspecialchars(implode("\n", json_decode($plan['features'], true))); ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="plans.php" class="btn btn-secondary">Cancel</a>
</form>
<?php else: ?>
<p>Plan not found.</p>
<?php endif; ?>
<?php
require_once 'templates/footer.php';
?>