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(); ?>
Our team will contact you within 24 hours.
You have successfully purchased this car. Our delivery team will contact you soon.
This car has already been sold and is no longer available for booking or purchase.