196 lines
9.5 KiB
PHP
196 lines
9.5 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
header("Location: car_list.php");
|
|
exit();
|
|
}
|
|
|
|
$carId = (int)$_GET['id'];
|
|
$pdo = db();
|
|
$message = '';
|
|
$message_type = '';
|
|
|
|
// Handle POST requests (Booking or Review)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
$userId = $_SESSION['user_id'];
|
|
|
|
// Booking Logic
|
|
if (isset($_POST['book_now'])) {
|
|
try {
|
|
$pdo->beginTransaction();
|
|
$stmt = $pdo->prepare("INSERT INTO bookings (user_id, car_id, status) VALUES (?, ?, 'pending')");
|
|
$stmt->execute([$userId, $carId]);
|
|
$stmt = $pdo->prepare("UPDATE cars SET status = 'reserved' WHERE id = ? AND status = 'approved'");
|
|
$stmt->execute([$carId]);
|
|
$pdo->commit();
|
|
$message = "Your booking request has been sent! The car is reserved for you pending admin approval.";
|
|
$message_type = 'success';
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
error_log("Booking failed: " . $e->getMessage());
|
|
$message = "There was an error processing your booking. Please try again.";
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
|
|
// Review Logic
|
|
if (isset($_POST['submit_review'])) {
|
|
$rating = filter_input(INPUT_POST, 'rating', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 5]]);
|
|
$review_text = trim(filter_input(INPUT_POST, 'review', FILTER_SANITIZE_SPECIAL_CHARS));
|
|
|
|
if ($rating && !empty($review_text)) {
|
|
$stmt = $pdo->prepare("INSERT INTO reviews (car_id, user_id, rating, review) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$carId, $userId, $rating, $review_text]);
|
|
$message = "Your review has been submitted and is pending approval.";
|
|
$message_type = 'success';
|
|
} else {
|
|
$message = "Invalid rating or review text.";
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch car details
|
|
$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 approved 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 ?> - Car Sells Afghanistan</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
|
|
</head>
|
|
<body>
|
|
<?php include 'partials/navbar.php'; ?>
|
|
|
|
<main class="section-padding">
|
|
<div class="container">
|
|
<div class="row g-5">
|
|
<!-- Car Image Gallery -->
|
|
<div class="col-lg-7">
|
|
<img src="<?= htmlspecialchars($car['image_url'] ?: 'https://via.placeholder.com/800x600?text=Car+Image') ?>" class="img-fluid rounded shadow-lg w-100" alt="<?= $projectName ?>">
|
|
</div>
|
|
|
|
<!-- Car Details & Booking -->
|
|
<div class="col-lg-5">
|
|
<h1 class="h2 mb-2"><?= $projectName ?></h1>
|
|
<p class="text-muted fs-5 mb-4"><i class="bi bi-geo-alt-fill me-2"></i><?= htmlspecialchars($car['city'] . ', ' . $car['province']) ?></p>
|
|
|
|
<p class="lead mb-4"><?= htmlspecialchars($car['description']) ?></p>
|
|
|
|
<div class="card p-4 mb-4">
|
|
<h3 class="h5 mb-3">Key Specifications</h3>
|
|
<div class="row g-3 fs-6">
|
|
<div class="col-6"><i class="bi bi-calendar-event me-2 text-primary"></i> <strong>Year:</strong> <?= htmlspecialchars($car['year']) ?></div>
|
|
<div class="col-6"><i class="bi bi-palette me-2 text-primary"></i> <strong>Color:</strong> <?= htmlspecialchars($car['color'] ?? 'N/A') ?></div>
|
|
<div class="col-6"><i class="bi bi-speedometer2 me-2 text-primary"></i> <strong>Mileage:</strong> <?= number_format($car['mileage']) ?> km</div>
|
|
<div class="col-6"><i class="bi bi-fuel-pump me-2 text-primary"></i> <strong>Fuel:</strong> Petrol</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="text-end mb-4">
|
|
<span class="display-5 fw-bold" style="color: var(--secondary-color);">$<?= number_format($car['price']) ?></span>
|
|
</div>
|
|
|
|
<?php if (!empty($message)): ?>
|
|
<div class="alert alert-<?= $message_type ?>"><?= $message ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($car['status'] === 'approved'): ?>
|
|
<form method="POST" class="d-grid">
|
|
<button type="submit" name="book_now" class="btn btn-primary btn-lg">Book This Car Now</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<div class="alert alert-info text-center">This car is currently <strong class="text-capitalize"><?= htmlspecialchars($car['status']) ?></strong> and cannot be booked.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<hr class="my-5">
|
|
|
|
<!-- Reviews Section from previous steps remains unchanged -->
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-8">
|
|
<h2 class="text-center mb-5">Customer Reviews</h2>
|
|
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<div class="card mb-5">
|
|
<div class="card-body p-4">
|
|
<h4 class="card-title mb-3">Leave Your Feedback</h4>
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="rating" class="form-label">Your Rating (1-5)</label>
|
|
<select name="rating" id="rating" class="form-select" required>
|
|
<option value="">Select a rating</option>
|
|
<option value="5">★★★★★ (Excellent)</option>
|
|
<option value="4">★★★★☆ (Great)</option>
|
|
<option value="3">★★★☆☆ (Good)</option>
|
|
<option value="2">★★☆☆☆ (Fair)</option>
|
|
<option value="1">★☆☆☆☆ (Poor)</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="review" class="form-label">Your Review</label>
|
|
<textarea name="review" id="review" class="form-control" rows="4" placeholder="Share your experience with this car..." required></textarea>
|
|
</div>
|
|
<button type="submit" name="submit_review" class="btn btn-primary">Submit Review</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<p class="text-center"><a href="login.php">Log in</a> to share your experience.</p>
|
|
<?php endif; ?>
|
|
|
|
<?php if (empty($reviews)): ?>
|
|
<p class="text-center text-muted">Be the first to write a review for this car!</p>
|
|
<?php else: ?>
|
|
<?php foreach ($reviews as $review): ?>
|
|
<div class="card mb-3">
|
|
<div class="card-body d-flex">
|
|
<div class="flex-shrink-0 me-3">
|
|
<img src="https://i.pravatar.cc/60?u=<?= htmlspecialchars($review['username']) ?>" alt="" class="rounded-circle">
|
|
</div>
|
|
<div>
|
|
<h5 class="mt-0 mb-1"><?= htmlspecialchars($review['username']) ?></h5>
|
|
<div class="text-warning mb-2">
|
|
<?= str_repeat('★', $review['rating']) . str_repeat('☆', 5 - $review['rating']) ?>
|
|
</div>
|
|
<p class="mb-1"><?= nl2br(htmlspecialchars($review['review'])) ?></p>
|
|
<small class="text-muted"><?= date("F j, Y", strtotime($review['created_at'])) ?></small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?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>
|