289 lines
17 KiB
PHP
289 lines
17 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 = '';
|
|
|
|
// 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);
|
|
|
|
// Handle Review POST
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_review'])) {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
$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, $_SESSION['user_id'], $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';
|
|
}
|
|
}
|
|
|
|
$pageTitle = htmlspecialchars($car['make'] . ' ' . $car['model']);
|
|
include 'partials/header.php';
|
|
?>
|
|
|
|
<main class="py-5 bg-light">
|
|
<div class="container">
|
|
<nav aria-label="breadcrumb" class="mb-4">
|
|
<ol class="breadcrumb">
|
|
<li class="breadcrumb-item"><a href="index.php">Home</a></li>
|
|
<li class="breadcrumb-item"><a href="car_list.php">Cars</a></li>
|
|
<li class="breadcrumb-item active" aria-current="page"><?= $pageTitle ?></li>
|
|
</ol>
|
|
</nav>
|
|
|
|
<div class="row g-5">
|
|
<!-- Car Image Gallery -->
|
|
<div class="col-lg-8">
|
|
<div class="card border-0 shadow-lg overflow-hidden rounded-4">
|
|
<div class="position-relative">
|
|
<img src="<?= htmlspecialchars($car['image_url'] ?: 'https://via.placeholder.com/800x600?text=Car+Image') ?>" class="img-fluid w-100 object-fit-cover" style="max-height: 500px;" alt="<?= $pageTitle ?>">
|
|
<?php if ($car['status'] === 'sold'): ?>
|
|
<div class="position-absolute top-0 start-0 w-100 h-100 d-flex align-items-center justify-content-center bg-dark bg-opacity-75 text-white">
|
|
<h1 class="display-1 fw-bold text-uppercase border-4 border border-white p-4">SOLD</h1>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Car Details & Booking -->
|
|
<div class="col-lg-4">
|
|
<div class="card border-0 shadow-sm rounded-4 h-100">
|
|
<div class="card-body p-4 d-flex flex-column">
|
|
<h1 class="h2 fw-bold mb-1"><?= $pageTitle ?></h1>
|
|
<p class="text-muted fs-6 mb-3"><i class="bi bi-geo-alt-fill me-1 text-danger"></i><?= htmlspecialchars($car['city'] . ', ' . $car['province']) ?></p>
|
|
|
|
<div class="d-flex align-items-center justify-content-between mb-4 p-3 bg-light rounded-3">
|
|
<span class="text-muted fw-semibold">Price</span>
|
|
<span class="h3 fw-bold text-primary mb-0">$<?= number_format($car['price']) ?></span>
|
|
</div>
|
|
|
|
<div class="row g-3 fs-6 mb-4">
|
|
<div class="col-6">
|
|
<div class="d-flex align-items-center">
|
|
<div class="icon-square bg-primary bg-opacity-10 text-primary rounded-3 me-2 p-2">
|
|
<i class="bi bi-calendar-event"></i>
|
|
</div>
|
|
<div>
|
|
<small class="text-muted d-block">Year</small>
|
|
<strong><?= htmlspecialchars($car['year']) ?></strong>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6">
|
|
<div class="d-flex align-items-center">
|
|
<div class="icon-square bg-success bg-opacity-10 text-success rounded-3 me-2 p-2">
|
|
<i class="bi bi-speedometer2"></i>
|
|
</div>
|
|
<div>
|
|
<small class="text-muted d-block">Mileage</small>
|
|
<strong><?= number_format($car['mileage']) ?> km</strong>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6">
|
|
<div class="d-flex align-items-center">
|
|
<div class="icon-square bg-info bg-opacity-10 text-info rounded-3 me-2 p-2">
|
|
<i class="bi bi-palette"></i>
|
|
</div>
|
|
<div>
|
|
<small class="text-muted d-block">Color</small>
|
|
<strong><?= htmlspecialchars($car['color'] ?? 'N/A') ?></strong>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6">
|
|
<div class="d-flex align-items-center">
|
|
<div class="icon-square bg-warning bg-opacity-10 text-warning rounded-3 me-2 p-2">
|
|
<i class="bi bi-fuel-pump"></i>
|
|
</div>
|
|
<div>
|
|
<small class="text-muted d-block">Fuel</small>
|
|
<strong>Petrol</strong>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<h6 class="fw-bold">Description</h6>
|
|
<p class="text-muted small mb-0"><?= nl2br(htmlspecialchars($car['description'])) ?></p>
|
|
</div>
|
|
|
|
<div class="mt-auto">
|
|
<?php if (!empty($message)): ?>
|
|
<div class="alert alert-<?= $message_type ?> mb-3"><?= $message ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($car['status'] === 'approved'): ?>
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<button type="button" class="btn btn-primary btn-lg w-100 py-3 fw-bold rounded-pill shadow-sm" data-bs-toggle="modal" data-bs-target="#buyModal">
|
|
<i class="bi bi-cart-check me-2"></i> Buy Now
|
|
</button>
|
|
<?php else: ?>
|
|
<a href="login.php" class="btn btn-primary btn-lg w-100 py-3 fw-bold rounded-pill shadow-sm">Login to Buy</a>
|
|
<?php endif; ?>
|
|
<?php elseif ($car['status'] === 'sold'): ?>
|
|
<button class="btn btn-secondary btn-lg w-100 py-3 fw-bold rounded-pill disabled" disabled>SOLD OUT</button>
|
|
<?php else: ?>
|
|
<div class="alert alert-info text-center">Status: <strong class="text-capitalize"><?= htmlspecialchars($car['status']) ?></strong></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row mt-5">
|
|
<div class="col-12">
|
|
<div class="card border-0 shadow-sm rounded-4">
|
|
<div class="card-body p-4 p-lg-5">
|
|
<h2 class="h4 fw-bold mb-4">Customer Reviews</h2>
|
|
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<div class="bg-light p-4 rounded-3 mb-5">
|
|
<h5 class="fw-bold mb-3">Leave a Review</h5>
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="rating" class="form-label text-muted small text-uppercase fw-bold">Rating</label>
|
|
<div class="input-group">
|
|
<select name="rating" id="rating" class="form-select" required>
|
|
<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>
|
|
<div class="mb-3">
|
|
<label for="review" class="form-label text-muted small text-uppercase fw-bold">Review</label>
|
|
<textarea name="review" id="review" class="form-control" rows="3" placeholder="Share your experience..." required></textarea>
|
|
</div>
|
|
<button type="submit" name="submit_review" class="btn btn-dark px-4 rounded-pill">Submit Review</button>
|
|
</form>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="alert alert-light border text-center mb-5">
|
|
<a href="login.php" class="fw-bold text-decoration-none">Log in</a> to write a review.
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (empty($reviews)): ?>
|
|
<div class="text-center text-muted py-5">
|
|
<i class="bi bi-chat-dots fs-1 mb-3 d-block text-secondary"></i>
|
|
<p>No reviews yet. Be the first to share your thoughts!</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="row g-4">
|
|
<?php foreach ($reviews as $review): ?>
|
|
<div class="col-md-6">
|
|
<div class="card h-100 border bg-light">
|
|
<div class="card-body">
|
|
<div class="d-flex align-items-center mb-3">
|
|
<div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px; font-weight: bold;">
|
|
<?= strtoupper(substr($review['username'], 0, 1)) ?>
|
|
</div>
|
|
<div>
|
|
<h6 class="mb-0 fw-bold"><?= htmlspecialchars($review['username']) ?></h6>
|
|
<small class="text-muted"><?= date("M d, Y", strtotime($review['created_at'])) ?></small>
|
|
</div>
|
|
<div class="ms-auto text-warning">
|
|
<?= str_repeat('★', $review['rating']) . str_repeat('☆', 5 - $review['rating']) ?>
|
|
</div>
|
|
</div>
|
|
<p class="card-text text-muted"><?= nl2br(htmlspecialchars($review['review'])) ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<!-- Buy Modal -->
|
|
<div class="modal fade" id="buyModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow-lg">
|
|
<form action="purchase_process.php" method="POST">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title fw-bold">Complete Purchase</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body p-4">
|
|
<input type="hidden" name="car_id" value="<?= $car['id'] ?>">
|
|
<div class="text-center mb-4">
|
|
<h4 class="text-primary fw-bold">$<?= number_format($car['price']) ?></h4>
|
|
<p class="text-muted"><?= htmlspecialchars($car['title']) ?></p>
|
|
</div>
|
|
|
|
<div class="form-floating mb-3">
|
|
<select name="province" class="form-select" id="provinceSelect" required>
|
|
<option value="">Select Province</option>
|
|
<option value="Kabul">Kabul</option>
|
|
<option value="Herat">Herat</option>
|
|
<option value="Kandahar">Kandahar</option>
|
|
<option value="Mazar-i-Sharif">Mazar-i-Sharif</option>
|
|
<option value="Jalalabad">Jalalabad</option>
|
|
<option value="Other">Other</option>
|
|
</select>
|
|
<label for="provinceSelect">Bank Province</label>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="text" name="account_number" class="form-control" id="accNum" required placeholder="Account Number">
|
|
<label for="accNum">Bank Account Number</label>
|
|
</div>
|
|
|
|
<div class="alert alert-warning d-flex align-items-center small" role="alert">
|
|
<i class="bi bi-info-circle-fill me-2 fs-5"></i>
|
|
<div>By clicking "Confirm", you agree to the terms of sale. This action cannot be undone.</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer bg-light">
|
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary fw-bold px-4">Confirm Purchase</button>
|
|
</div>
|
|
</form>
|
|
</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>
|