126 lines
4.8 KiB
PHP
126 lines
4.8 KiB
PHP
<?php
|
|
require_once 'includes/admin_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
header('Location: admin_vehicles.php');
|
|
exit;
|
|
}
|
|
|
|
$name = $type = $capacity = $price_per_day = '';
|
|
$is_available = 0;
|
|
$errors = [];
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM vehicles WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$vehicle = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$vehicle) {
|
|
header('Location: admin_vehicles.php?error=notfound');
|
|
exit;
|
|
}
|
|
|
|
$name = $vehicle['name'];
|
|
$type = $vehicle['type'];
|
|
$capacity = $vehicle['capacity'];
|
|
$price_per_day = $vehicle['price_per_day'];
|
|
$is_available = $vehicle['is_available'];
|
|
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$type = trim($_POST['type'] ?? '');
|
|
$capacity = trim($_POST['capacity'] ?? '');
|
|
$price_per_day = trim($_POST['price_per_day'] ?? '');
|
|
$is_available = isset($_POST['is_available']) ? 1 : 0;
|
|
|
|
if (empty($name)) {
|
|
$errors[] = 'Vehicle name is required.';
|
|
}
|
|
if (empty($type)) {
|
|
$errors[] = 'Vehicle type is required.';
|
|
}
|
|
if (!filter_var($capacity, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]])) {
|
|
$errors[] = 'Capacity must be a positive number.';
|
|
}
|
|
if (!filter_var($price_per_day, FILTER_VALIDATE_INT, ["options" => ["min_range" => 0]])) {
|
|
$errors[] = 'Price must be a non-negative number.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$sql = "UPDATE vehicles SET name = ?, type = ?, capacity = ?, price_per_day = ?, is_available = ? WHERE id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$name, $type, $capacity, $price_per_day, $is_available, $id]);
|
|
|
|
header("Location: admin_vehicles.php?success=2");
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
require_once 'includes/admin_sidebar.php';
|
|
?>
|
|
|
|
<h1 class="h2">Edit Vehicle</h1>
|
|
<p>Update the details for the vehicle below.</p>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Vehicle Details</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form action="admin_edit_vehicle.php?id=<?php echo $id; ?>" method="POST">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Vehicle Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="type" class="form-label">Vehicle Type</label>
|
|
<select class="form-select" id="type" name="type" required>
|
|
<option value="mpv" <?php if($type === 'mpv') echo 'selected'; ?>>MPV</option>
|
|
<option value="suv" <?php if($type === 'suv') echo 'selected'; ?>>SUV</option>
|
|
<option value="sedan" <?php if($type === 'sedan') echo 'selected'; ?>>Sedan</option>
|
|
<option value="minibus" <?php if($type === 'minibus') echo 'selected'; ?>>Minibus</option>
|
|
</select>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="capacity" class="form-label">Capacity (seats)</label>
|
|
<input type="number" class="form-control" id="capacity" name="capacity" value="<?php echo htmlspecialchars($capacity); ?>" required min="1">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="price_per_day" class="form-label">Price per Day (Rp)</label>
|
|
<input type="number" class="form-control" id="price_per_day" name="price_per_day" value="<?php echo htmlspecialchars($price_per_day); ?>" required min="0">
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 form-check">
|
|
<input type="checkbox" class="form-check-input" id="is_available" name="is_available" value="1" <?php if($is_available) echo 'checked'; ?>>
|
|
<label class="form-check-label" for="is_available">Available for rent</label>
|
|
</div>
|
|
<hr>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
<a href="admin_vehicles.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
require_once 'includes/admin_footer.php';
|
|
?>
|