38395-vm/user/dashboard.php
Flatlogic Bot 4ad8ad11c8 sadiq
2026-02-13 08:34:16 +00:00

160 lines
8.5 KiB
PHP

<?php
$page_title = "User Dashboard - AFG CARS";
include '../includes/header.php';
if (!isset($_SESSION['user_id'])) {
header('Location: ../login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$pdo = db();
// Fetch user's cars
$stmt = $pdo->prepare("SELECT * FROM cars WHERE owner_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$my_cars = $stmt->fetchAll();
// Fetch user's bookings (where they booked a car)
$stmt = $pdo->prepare("SELECT b.*, c.title as car_title, c.image_url FROM bookings b JOIN cars c ON b.car_id = c.id WHERE b.user_id = ? ORDER BY b.created_at DESC");
$stmt->execute([$user_id]);
$my_bookings = $stmt->fetchAll();
// Fetch user's purchases
$stmt = $pdo->prepare("SELECT p.*, c.title as car_title FROM purchases p JOIN cars c ON p.car_id = c.id WHERE p.user_id = ? ORDER BY p.created_at DESC");
$stmt->execute([$user_id]);
$my_purchases = $stmt->fetchAll();
?>
<div class="container py-5">
<div class="row g-4">
<!-- Sidebar -->
<div class="col-lg-3">
<div class="card border-0 shadow-sm p-4 rounded-4 sticky-top" style="top: 100px;">
<div class="text-center mb-4">
<div class="bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 80px; height: 80px;">
<span class="fs-1 fw-bold"><?php echo strtoupper(substr($_SESSION['full_name'], 0, 1)); ?></span>
</div>
<h5 class="fw-bold mb-0"><?php echo $_SESSION['full_name']; ?></h5>
<p class="text-muted small"><?php echo $_SESSION['email']; ?></p>
</div>
<div class="list-group list-group-flush border-0">
<a href="#my-listings" class="list-group-item list-group-item-action border-0 px-0 active"><i class="bi bi-car-front me-2"></i> My Listings</a>
<a href="#my-bookings" class="list-group-item list-group-item-action border-0 px-0"><i class="bi bi-calendar-check me-2"></i> My Bookings</a>
<a href="#my-purchases" class="list-group-item list-group-item-action border-0 px-0"><i class="bi bi-bag-check me-2"></i> Purchases</a>
<a href="/user/add-car.php" class="list-group-item list-group-item-action border-0 px-0 text-primary"><i class="bi bi-plus-circle me-2"></i> Sell New Car</a>
</div>
</div>
</div>
<!-- Main Content -->
<div class="col-lg-9">
<!-- Stats -->
<div class="row g-3 mb-5">
<div class="col-md-4">
<div class="bg-white p-4 rounded-4 shadow-sm border-start border-primary border-4">
<p class="text-muted mb-1">My Cars</p>
<h2 class="fw-bold mb-0"><?php echo count($my_cars); ?></h2>
</div>
</div>
<div class="col-md-4">
<div class="bg-white p-4 rounded-4 shadow-sm border-start border-success border-4">
<p class="text-muted mb-1">Bookings</p>
<h2 class="fw-bold mb-0"><?php echo count($my_bookings); ?></h2>
</div>
</div>
<div class="col-md-4">
<div class="bg-white p-4 rounded-4 shadow-sm border-start border-info border-4">
<p class="text-muted mb-1">Purchases</p>
<h2 class="fw-bold mb-0"><?php echo count($my_purchases); ?></h2>
</div>
</div>
</div>
<h3 class="fw-bold mb-4" id="my-listings">My Car Listings</h3>
<?php if (empty($my_cars)): ?>
<div class="bg-white p-5 rounded-4 shadow-sm text-center">
<i class="bi bi-emoji-frown fs-1 text-muted mb-3"></i>
<p class="text-muted">You haven't listed any cars yet.</p>
<a href="/user/add-car.php" class="btn btn-primary">Start Selling</a>
</div>
<?php else: ?>
<div class="row g-4">
<?php foreach ($my_cars as $car): ?>
<div class="col-md-6">
<div class="card car-card">
<div class="position-relative">
<img src="<?php echo $car['image_url']; ?>" class="card-img-top" alt="...">
<?php if ($car['status'] === 'sold'): ?>
<div class="sold-badge">SOLD</div>
<?php elseif ($car['approval_status'] === 'pending'): ?>
<div class="pending-badge">PENDING APPROVAL</div>
<?php elseif ($car['approval_status'] === 'rejected'): ?>
<div class="badge bg-danger position-absolute top-0 start-0 m-3">REJECTED</div>
<?php endif; ?>
</div>
<div class="card-body">
<h5 class="fw-bold mb-1"><?php echo $car['title']; ?></h5>
<p class="text-muted small mb-2"><i class="bi bi-geo-alt me-1"></i><?php echo $car['location']; ?></p>
<div class="d-flex justify-content-between align-items-center">
<span class="price-tag">$<?php echo number_format($car['price']); ?></span>
<a href="/car-details.php?id=<?php echo $car['id']; ?>" class="btn btn-sm btn-outline-primary">View</a>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<h3 class="fw-bold mt-5 mb-4" id="my-bookings">My Bookings</h3>
<div class="bg-white rounded-4 shadow-sm overflow-hidden">
<table class="table table-hover mb-0">
<thead class="bg-light">
<tr>
<th>Car</th>
<th>Status</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($my_bookings as $booking): ?>
<tr>
<td><?php echo htmlspecialchars($booking['car_title']); ?></td>
<td><span class="badge bg-<?php echo $booking['status'] === 'confirmed' ? 'success' : ($booking['status'] === 'pending' ? 'warning' : 'danger'); ?>"><?php echo ucfirst($booking['status']); ?></span></td>
<td class="small text-muted"><?php echo date('M d, Y', strtotime($booking['created_at'])); ?></td>
</tr>
<?php endforeach; if(empty($my_bookings)) echo "<tr><td colspan='3' class='text-center py-4 text-muted'>No bookings found</td></tr>"; ?>
</tbody>
</table>
</div>
<h3 class="fw-bold mt-5 mb-4" id="my-purchases">My Purchases</h3>
<div class="bg-white rounded-4 shadow-sm overflow-hidden">
<table class="table table-hover mb-0">
<thead class="bg-light">
<tr>
<th>Car</th>
<th>Bank</th>
<th>Amount</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($my_purchases as $purchase): ?>
<tr>
<td><?php echo htmlspecialchars($purchase['car_title']); ?></td>
<td><?php echo htmlspecialchars($purchase['bank_name']); ?></td>
<td class="fw-bold">$<?php echo number_format($purchase['amount']); ?></td>
<td class="small text-muted"><?php echo date('M d, Y', strtotime($purchase['created_at'])); ?></td>
</tr>
<?php endforeach; if(empty($my_purchases)) echo "<tr><td colspan='4' class='text-center py-4 text-muted'>No purchases found</td></tr>"; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include '../includes/footer.php'; ?>