308 lines
15 KiB
PHP
308 lines
15 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
session_start();
|
|
$pdo = db();
|
|
|
|
$id = $_GET['id'] ?? 0;
|
|
$user_id = $_SESSION['user_id'] ?? null;
|
|
|
|
// Increment view count
|
|
$pdo->prepare("UPDATE cars SET view_count = view_count + 1 WHERE id = ?")->execute([$id]);
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$car = $stmt->fetch();
|
|
|
|
if (!$car) {
|
|
header('Location: cars.php');
|
|
exit;
|
|
}
|
|
|
|
// Check approval status: only owner or admin can see pending/rejected
|
|
$is_admin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
|
|
$is_owner = $user_id && $car['owner_id'] == $user_id;
|
|
|
|
if ($car['approval_status'] !== 'approved' && !$is_admin && !$is_owner) {
|
|
header('Location: cars.php');
|
|
exit;
|
|
}
|
|
|
|
$page_title = $car['title'] . " - AFG CARS";
|
|
include 'includes/header.php';
|
|
|
|
// Check if is favorite
|
|
$is_fav = false;
|
|
if ($is_logged_in) {
|
|
$fs = $pdo->prepare("SELECT id FROM favorites WHERE user_id = ? AND car_id = ?");
|
|
$fs->execute([$user_id, $id]);
|
|
$is_fav = (bool)$fs->fetch();
|
|
}
|
|
|
|
$message = '';
|
|
// Handle Booking
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['book_now'])) {
|
|
if (!$is_logged_in) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
$booking_message = $_POST['message'] ?? '';
|
|
$stmt = $pdo->prepare("INSERT INTO bookings (user_id, car_id, message) VALUES (?, ?, ?)");
|
|
$message = $stmt->execute([$user_id, $id, $booking_message]) ? "success" : "error";
|
|
}
|
|
|
|
// Handle Purchase Simulation
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase_now'])) {
|
|
if (!$is_logged_in) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
$bank = $_POST['bank_name'] ?? 'Unknown Bank';
|
|
$transaction_id = 'AFG-' . strtoupper(uniqid());
|
|
|
|
$pdo->beginTransaction();
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO purchases (user_id, car_id, amount, bank_name, transaction_id, status) VALUES (?, ?, ?, ?, ?, 'completed')");
|
|
$stmt->execute([$user_id, $id, $car['price'], $bank, $transaction_id]);
|
|
|
|
$stmt = $pdo->prepare("UPDATE cars SET status = 'sold' WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
$pdo->commit();
|
|
$message = "purchase_success";
|
|
$car['status'] = 'sold'; // Update local state
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
$message = "error";
|
|
}
|
|
}
|
|
|
|
// Handle Review
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_review'])) {
|
|
if (!$is_logged_in) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
$rating = $_POST['rating'] ?? 5;
|
|
$comment = $_POST['comment'] ?? '';
|
|
$stmt = $pdo->prepare("INSERT INTO reviews (user_id, car_id, rating, comment) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$user_id, $id, $rating, $comment]);
|
|
header("Location: car-details.php?id=$id&review=success");
|
|
exit;
|
|
}
|
|
|
|
// Fetch Reviews
|
|
$stmt = $pdo->prepare("SELECT r.*, u.full_name FROM reviews r JOIN users u ON r.user_id = u.id WHERE r.car_id = ? ORDER BY r.created_at DESC");
|
|
$stmt->execute([$id]);
|
|
$reviews = $stmt->fetchAll();
|
|
?>
|
|
|
|
<div class="container py-5">
|
|
<div class="row g-5">
|
|
<!-- Image Gallery -->
|
|
<div class="col-lg-8">
|
|
<div class="position-relative">
|
|
<div class="card border-0 shadow-sm overflow-hidden mb-4 position-relative" style="border-radius: 24px;">
|
|
<img src="<?php echo htmlspecialchars($car['image_url']); ?>" class="img-fluid w-100 <?php echo $car['status'] === 'sold' ? 'opacity-75' : ''; ?>" style="max-height: 500px; object-fit: cover;">
|
|
<?php if ($car['status'] === 'sold'): ?>
|
|
<div class="position-absolute top-50 start-50 translate-middle">
|
|
<h1 class="display-1 fw-bold text-white shadow-lg bg-danger px-5 py-3 rounded-pill opacity-75" style="transform: rotate(-10deg);">SOLD</h1>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php if ($is_logged_in): ?>
|
|
<a href="toggle-favorite.php?id=<?php echo $id; ?>&action=<?php echo $is_fav ? 'remove' : 'add'; ?>"
|
|
class="btn <?php echo $is_fav ? 'btn-danger' : 'btn-white'; ?> position-absolute top-0 end-0 m-4 shadow rounded-circle d-flex align-items-center justify-content-center"
|
|
style="width: 50px; height: 50px; background: white; z-index: 10;">
|
|
<i class="bi <?php echo $is_fav ? 'bi-heart-fill' : 'bi-heart text-danger'; ?> fs-4"></i>
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm p-4 mb-4" style="border-radius: 24px;">
|
|
<div class="d-flex flex-wrap justify-content-between align-items-center mb-4 gap-3">
|
|
<div>
|
|
<h2 class="fw-bold mb-1"><?php echo htmlspecialchars($car['title']); ?></h2>
|
|
<span class="badge bg-light text-muted"><i class="bi bi-eye me-1"></i><?php echo $car['view_count']; ?> views</span>
|
|
<?php if ($car['approval_status'] !== 'approved'): ?>
|
|
<span class="badge bg-warning text-dark ms-2"><?php echo strtoupper($car['approval_status']); ?></span>
|
|
<?php endif; ?>
|
|
</div>
|
|
<span class="price-tag fs-2">$<?php echo number_format($car['price']); ?></span>
|
|
</div>
|
|
|
|
<div class="row g-3 mb-5">
|
|
<div class="col-6 col-md-3">
|
|
<div class="p-3 bg-light rounded-4 text-center">
|
|
<i class="bi bi-calendar-event text-primary mb-2 d-block"></i>
|
|
<span class="text-muted small">Year</span>
|
|
<div class="fw-bold"><?php echo $car['year']; ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6 col-md-3">
|
|
<div class="p-3 bg-light rounded-4 text-center">
|
|
<i class="bi bi-fuel-pump text-primary mb-2 d-block"></i>
|
|
<span class="text-muted small">Fuel Type</span>
|
|
<div class="fw-bold"><?php echo $car['fuel_type']; ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6 col-md-3">
|
|
<div class="p-3 bg-light rounded-4 text-center">
|
|
<i class="bi bi-gear-wide-connected text-primary mb-2 d-block"></i>
|
|
<span class="text-muted small">Transmission</span>
|
|
<div class="fw-bold"><?php echo $car['transmission']; ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-6 col-md-3">
|
|
<div class="p-3 bg-light rounded-4 text-center">
|
|
<i class="bi bi-speedometer2 text-primary mb-2 d-block"></i>
|
|
<span class="text-muted small">Mileage</span>
|
|
<div class="fw-bold"><?php echo number_format($car['mileage']); ?> km</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<h5 class="fw-bold mb-3">Description</h5>
|
|
<p class="text-muted lead mb-0"><?php echo nl2br(htmlspecialchars($car['description'])); ?></p>
|
|
</div>
|
|
|
|
<!-- Reviews Section -->
|
|
<div class="card border-0 shadow-sm p-4" style="border-radius: 24px;">
|
|
<h4 class="fw-bold mb-4">Reviews (<?php echo count($reviews); ?>)</h4>
|
|
|
|
<?php if ($is_logged_in && $car['status'] === 'sold'): ?>
|
|
<form method="POST" class="mb-5 bg-light p-4 rounded-4">
|
|
<h6 class="fw-bold mb-3">Leave a Review</h6>
|
|
<div class="mb-3">
|
|
<label class="form-label small">Rating</label>
|
|
<select name="rating" class="form-select w-auto">
|
|
<option value="5">⭐⭐⭐⭐⭐ (5/5)</option>
|
|
<option value="4">⭐⭐⭐⭐ (4/5)</option>
|
|
<option value="3">⭐⭐⭐ (3/5)</option>
|
|
<option value="2">⭐⭐ (2/5)</option>
|
|
<option value="1">⭐ (1/5)</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<textarea name="comment" class="form-control" rows="3" placeholder="Share your experience..."></textarea>
|
|
</div>
|
|
<button type="submit" name="submit_review" class="btn btn-primary btn-sm px-4">Post Review</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<div class="review-list">
|
|
<?php foreach ($reviews as $review): ?>
|
|
<div class="mb-4 pb-4 border-bottom last-child-no-border">
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<h6 class="fw-bold mb-0"><?php echo htmlspecialchars($review['full_name']); ?></h6>
|
|
<span class="text-warning"><?php echo str_repeat('⭐', $review['rating']); ?></span>
|
|
</div>
|
|
<p class="text-muted small mb-0"><?php echo htmlspecialchars($review['comment']); ?></p>
|
|
<span class="text-muted smaller" style="font-size: 0.75rem;"><?php echo date('M d, Y', strtotime($review['created_at'])); ?></span>
|
|
</div>
|
|
<?php endforeach; if(empty($reviews)) echo "<p class='text-muted italic'>No reviews yet.</p>"; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sidebar -->
|
|
<div class="col-lg-4">
|
|
<div class="sticky-top" style="top: 100px;">
|
|
<?php if ($message === 'success'): ?>
|
|
<div class="alert alert-success border-0 shadow-sm rounded-4 p-4 mb-4 text-center">
|
|
<i class="bi bi-check-circle-fill display-4 d-block mb-3"></i>
|
|
<h5 class="fw-bold">Request Sent!</h5>
|
|
<p class="mb-0 small">Our team will contact you within 24 hours.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($message === 'purchase_success'): ?>
|
|
<div class="alert alert-primary border-0 shadow-sm rounded-4 p-4 mb-4 text-center">
|
|
<i class="bi bi-trophy display-4 d-block mb-3"></i>
|
|
<h5 class="fw-bold">Congratulation!</h5>
|
|
<p class="mb-0 small">You have successfully purchased this car. Our delivery team will contact you soon.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card border-0 shadow-sm p-4 mb-4" style="border-radius: 24px;">
|
|
<h5 class="fw-bold mb-4">Interested in this car?</h5>
|
|
|
|
<?php if ($car['status'] === 'available'): ?>
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Message</label>
|
|
<textarea name="message" class="form-control rounded-3" rows="3" placeholder="I'm interested in this car..."></textarea>
|
|
</div>
|
|
<button type="submit" name="book_now" class="btn btn-outline-primary w-100 py-3 rounded-4 fw-bold mb-3">
|
|
<i class="bi bi-calendar-plus me-2"></i>Schedule Viewing
|
|
</button>
|
|
</form>
|
|
<button class="btn btn-primary w-100 py-3 rounded-4 fw-bold shadow-primary" data-bs-toggle="modal" data-bs-target="#purchaseModal">
|
|
<i class="bi bi-bag-check me-2"></i>Buy Now
|
|
</button>
|
|
<?php else: ?>
|
|
<div class="bg-light p-4 rounded-4 text-center">
|
|
<i class="bi bi-lock fs-1 text-muted mb-3 d-block"></i>
|
|
<h6 class="fw-bold">Listing Inactive</h6>
|
|
<p class="text-muted small mb-0">This car has already been sold and is no longer available for booking or purchase.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm p-4 bg-primary text-white overflow-hidden position-relative" style="border-radius: 24px;">
|
|
<h6 class="fw-bold mb-3">Contact Dealer</h6>
|
|
<div class="d-flex align-items-center gap-3">
|
|
<div class="bg-white text-primary rounded-circle p-2 px-3"><i class="bi bi-telephone"></i></div>
|
|
<div>
|
|
<div class="small opacity-75">Phone</div>
|
|
<div class="fw-bold fs-5">+93 700 123 456</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Purchase Modal -->
|
|
<div class="modal fade" id="purchaseModal" tabindex="-1">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content border-0 rounded-4">
|
|
<div class="modal-header border-0 p-4">
|
|
<h5 class="modal-title fw-bold">Complete Purchase</h5>
|
|
<button type="button" class="btn-close" data-bs-target="#purchaseModal" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body p-4">
|
|
<div class="d-flex align-items-center mb-4 p-3 bg-light rounded-4">
|
|
<img src="<?php echo $car['image_url']; ?>" class="rounded-3 me-3" style="width: 60px; height: 60px; object-fit: cover;">
|
|
<div>
|
|
<h6 class="fw-bold mb-0"><?php echo $car['title']; ?></h6>
|
|
<span class="price-tag">$<?php echo number_format($car['price']); ?></span>
|
|
</div>
|
|
</div>
|
|
|
|
<form method="POST">
|
|
<div class="mb-4">
|
|
<label class="form-label fw-bold small">Select Afghanistan Bank</label>
|
|
<select name="bank_name" class="form-select py-3 rounded-3" required>
|
|
<option value="Da Afghanistan Bank">Da Afghanistan Bank (DAB)</option>
|
|
<option value="Azizi Bank">Azizi Bank</option>
|
|
<option value="Pashtany Bank">Pashtany Bank</option>
|
|
<option value="New Kabul Bank">New Kabul Bank</option>
|
|
<option value="AIB">Afghanistan International Bank (AIB)</option>
|
|
<option value="Ghazanfar Bank">Ghazanfar Bank</option>
|
|
</select>
|
|
<div class="form-text small">This is a simulation. No real money will be charged.</div>
|
|
</div>
|
|
<button type="submit" name="purchase_now" class="btn btn-primary w-100 py-3 rounded-4 fw-bold">Confirm Purchase</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.last-child-no-border:last-child { border-bottom: none !important; }
|
|
.shadow-primary { box-shadow: 0 10px 20px -5px rgba(37, 99, 235, 0.4); }
|
|
</style>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|