35438-vm/admin_add_vehicle.php
2025-11-03 08:24:38 +00:00

102 lines
4.4 KiB
PHP

<?php
require_once 'includes/admin_header.php';
require_once 'db/config.php';
$name = $type = $capacity = $price_per_day = '';
$errors = [];
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 {
$pdo = db();
$sql = "INSERT INTO vehicles (name, type, capacity, price_per_day, is_available, image_url) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
// Using a placeholder image for now
$placeholder_image = 'assets/images/vehicle_placeholder.png';
$stmt->execute([$name, $type, $capacity, $price_per_day, $is_available, $placeholder_image]);
header("Location: admin_vehicles.php?success=1");
exit;
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
require_once 'includes/admin_sidebar.php';
?>
<h1 class="h2">Add New Vehicle</h1>
<p>Fill out the form to add a new vehicle to the fleet.</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_add_vehicle.php" 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="" disabled <?php if(empty($type)) echo 'selected'; ?>>Select a type</option>
<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" checked>
<label class="form-check-label" for="is_available">Available for rent</label>
</div>
<hr>
<button type="submit" class="btn btn-primary">Add Vehicle</button>
<a href="admin_vehicles.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
<?php
require_once 'includes/admin_footer.php';
?>