150 lines
6.0 KiB
PHP
150 lines
6.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_GET['id'])) {
|
|
header("Location: car_list.php");
|
|
exit();
|
|
}
|
|
|
|
$carId = $_GET['id'];
|
|
$pdo = db();
|
|
|
|
// Handle Booking
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['book_now'])) {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
$userId = $_SESSION['user_id'];
|
|
$stmt = $pdo->prepare("INSERT INTO bookings (user_id, car_id) VALUES (?, ?)");
|
|
$stmt->execute([$userId, $carId]);
|
|
$stmt = $pdo->prepare("UPDATE cars SET status = 'reserved' WHERE id = ?");
|
|
$stmt->execute([$carId]);
|
|
$booking_success = "Your booking request has been sent!";
|
|
}
|
|
|
|
// Handle Review
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_review'])) {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
$userId = $_SESSION['user_id'];
|
|
$rating = $_POST['rating'];
|
|
$review = $_POST['review'];
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO reviews (car_id, user_id, rating, review) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$carId, $userId, $rating, $review]);
|
|
$review_success = "Your review has been submitted for approval!";
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ?");
|
|
$stmt->execute([$carId]);
|
|
$car = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$car) {
|
|
header("Location: car_list.php");
|
|
exit();
|
|
}
|
|
|
|
// Fetch Reviews
|
|
$stmt = $pdo->prepare("SELECT r.*, u.username FROM reviews r JOIN users u ON r.user_id = u.id WHERE r.car_id = ? AND r.status = 'approved' ORDER BY r.created_at DESC");
|
|
$stmt->execute([$carId]);
|
|
$reviews = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$projectName = htmlspecialchars($car['make'] . ' ' . $car['model']);
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= $projectName ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
|
|
</head>
|
|
<body>
|
|
<?php include 'partials/navbar.php'; ?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<img src="<?= htmlspecialchars($car['image_url']) ?>" class="img-fluid rounded" alt="<?= $projectName ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h1><?= $projectName ?></h1>
|
|
<p class="lead"><?= htmlspecialchars($car['description']) ?></p>
|
|
<p><strong>Year:</strong> <?= htmlspecialchars($car['year']) ?></p>
|
|
<p><strong>Price:</strong> $<?= number_format($car['price']) ?></p>
|
|
<p><strong>Status:</strong> <span class="badge bg-info"><?= htmlspecialchars($car['status']) ?></span></p>
|
|
|
|
<?php if (isset($booking_success)): ?>
|
|
<div class="alert alert-success"><?= $booking_success ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($car['status'] === 'for_sale'): ?>
|
|
<form method="POST">
|
|
<button type="submit" name="book_now" class="btn btn-success btn-lg">Book Now</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<hr class="my-5">
|
|
|
|
<!-- Reviews Section -->
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<h2>Reviews</h2>
|
|
<?php if (isset($review_success)): ?>
|
|
<div class="alert alert-success"><?= $review_success ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Leave a Review</h5>
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="rating" class="form-label">Rating</label>
|
|
<select name="rating" id="rating" class="form-select" required>
|
|
<option value="5">5 Stars</option>
|
|
<option value="4">4 Stars</option>
|
|
<option value="3">3 Stars</option>
|
|
<option value="2">2 Stars</option>
|
|
<option value="1">1 Star</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="review" class="form-label">Review</label>
|
|
<textarea name="review" id="review" class="form-control" rows="3" required></textarea>
|
|
</div>
|
|
<button type="submit" name="submit_review" class="btn btn-primary">Submit Review</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<p><a href="login.php">Log in</a> to leave a review.</p>
|
|
<?php endif; ?>
|
|
|
|
<?php foreach ($reviews as $review): ?>
|
|
<div class="card mb-3">
|
|
<div class="card-body">
|
|
<h6 class="card-subtitle mb-2 text-muted"><strong><?= htmlspecialchars($review['username']) ?></strong> - <?= date("M d, Y", strtotime($review['created_at'])) ?></h6>
|
|
<p class="card-text">Rating: <?= str_repeat('★', $review['rating']) ?></p>
|
|
<p class="card-text"><?= htmlspecialchars($review['review']) ?></p>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($reviews)): ?>
|
|
<p>No reviews yet.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|