227 lines
10 KiB
PHP
227 lines
10 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: ' . APP_URL . 'cars.php');
|
|
exit;
|
|
}
|
|
|
|
// Check status: only owner or admin can see pending/rejected
|
|
$is_admin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
|
|
$is_owner = $user_id && $car['user_id'] == $user_id;
|
|
|
|
if ($car['status'] !== 'approved' && $car['status'] !== 'sold' && !$is_admin && !$is_owner) {
|
|
header('Location: ' . APP_URL . 'cars.php');
|
|
exit;
|
|
}
|
|
|
|
$page_title = $car['title'] . " - AFG CARS";
|
|
include 'includes/header.php';
|
|
|
|
// Check if is favorite
|
|
$is_fav = false;
|
|
if ($is_logged_in) {
|
|
// Check if table exists first (soft error prevention)
|
|
$stmt = $pdo->query("SHOW TABLES LIKE 'favorites'");
|
|
if ($stmt->fetch()) {
|
|
$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 Purchase Simulation
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase_now'])) {
|
|
if (!$is_logged_in) {
|
|
header('Location: ' . APP_URL . 'login.php');
|
|
exit;
|
|
}
|
|
$bank = $_POST['bank_name'] ?? 'Unknown Bank';
|
|
|
|
$pdo->beginTransaction();
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO purchases (car_id, buyer_id, payment_method, payment_status, total_amount) VALUES (?, ?, ?, 'completed', ?)");
|
|
$stmt->execute([$id, $user_id, $bank, $car['price']]);
|
|
|
|
$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: ' . APP_URL . '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.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']); ?>" 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">SOLD</h1>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</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">Views: <?php echo $car['view_count']; ?></span>
|
|
<?php if ($car['status'] !== 'approved' && $car['status'] !== 'sold'): ?>
|
|
<span class="badge bg-warning text-dark ms-2"><?php echo strtoupper($car['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 text-center">
|
|
<span class="text-muted small">Year</span>
|
|
<div class="fw-bold"><?php echo $car['year']; ?></div>
|
|
</div>
|
|
<div class="col-6 col-md-3 text-center">
|
|
<span class="text-muted small">Fuel</span>
|
|
<div class="fw-bold"><?php echo $car['fuel_type']; ?></div>
|
|
</div>
|
|
<div class="col-6 col-md-3 text-center">
|
|
<span class="text-muted small">Transmission</span>
|
|
<div class="fw-bold"><?php echo $car['transmission']; ?></div>
|
|
</div>
|
|
<div class="col-6 col-md-3 text-center">
|
|
<span class="text-muted small">Mileage</span>
|
|
<div class="fw-bold"><?php echo number_format($car['mileage']); ?> km</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">
|
|
<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">
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<h6 class="fw-bold mb-0"><?php echo htmlspecialchars($review['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>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sidebar -->
|
|
<div class="col-lg-4">
|
|
<div class="sticky-top" style="top: 100px;">
|
|
<?php if ($message === 'purchase_success'): ?>
|
|
<div class="alert alert-primary border-0 shadow-sm rounded-4 p-4 mb-4 text-center">
|
|
<h5 class="fw-bold">Congratulation!</h5>
|
|
<p class="mb-0 small">You have successfully purchased this car.</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">Purchase Options</h5>
|
|
|
|
<?php if ($car['status'] === 'approved'): ?>
|
|
<button class="btn btn-primary w-100 py-3 rounded-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#purchaseModal">
|
|
Buy This Car Now
|
|
</button>
|
|
<?php else: ?>
|
|
<div class="bg-light p-4 rounded-4 text-center">
|
|
<h6 class="fw-bold">Listing Inactive</h6>
|
|
<p class="text-muted small mb-0">This car is currently unavailable.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</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-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body p-4">
|
|
<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="Azizi Bank">Azizi Bank</option>
|
|
<option value="Pashtany Bank">Pashtany Bank</option>
|
|
<option value="New Kabul Bank">New Kabul Bank</option>
|
|
<option value="AIB">AIB</option>
|
|
</select>
|
|
<div class="form-text small">This is an offline simulation.</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>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|