38701-vm/user/dashboard.php
Flatlogic Bot 5db61988c3 sadiq
2026-02-23 06:39:28 +00:00

145 lines
8.2 KiB
PHP

<?php
$title = "My Dashboard";
require_once __DIR__ . '/../includes/header.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: ../login.php?msg=Please login to access dashboard");
exit;
}
$db = db();
$user_id = $_SESSION['user_id'];
// Fetch User Info
$userStmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$userStmt->execute([$user_id]);
$user = $userStmt->fetch();
// Fetch User's Car Listings
$carStmt = $db->prepare("SELECT * FROM cars WHERE user_id = ? AND is_deleted = 0 ORDER BY created_at DESC");
$carStmt->execute([$user_id]);
$myCars = $carStmt->fetchAll();
// Fetch Recent Purchases
$pStmt = $db->prepare("SELECT purchases.*, cars.title as car_title FROM purchases JOIN cars ON purchases.car_id = cars.id WHERE purchases.user_id = ? ORDER BY purchase_date DESC");
$pStmt->execute([$user_id]);
$myPurchases = $pStmt->fetchAll();
// Fetch Notifications
$nStmt = $db->prepare("SELECT * FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT 5");
$nStmt->execute([$user_id]);
$notifications = $nStmt->fetchAll();
?>
<div class="container" style="padding: 3rem 0;">
<div style="display: grid; grid-template-columns: 250px 1fr; gap: 3rem;">
<!-- Sidebar Navigation -->
<aside>
<div class="glass-card" style="padding: 2rem;">
<div style="text-align: center; margin-bottom: 2rem;">
<div style="width: 80px; height: 80px; border-radius: 50%; background: var(--bg-glass); display: flex; align-items: center; justify-content: center; font-size: 2rem; font-weight: bold; border: 2px solid var(--primary); margin: 0 auto 1rem;">
<?php echo strtoupper(substr($user['full_name'], 0, 1)); ?>
</div>
<strong><?php echo htmlspecialchars($user['full_name']); ?></strong>
<p style="color: var(--text-muted); font-size: 0.8rem; text-transform: capitalize;"><?php echo $user['role']; ?> Account</p>
</div>
<ul style="display: flex; flex-direction: column; gap: 0.5rem;">
<li><a href="dashboard.php" class="btn btn-primary" style="width: 100%; text-align: left;">Overview</a></li>
<li><a href="my_listings.php" class="btn btn-outline" style="width: 100%; text-align: left;">My Listings</a></li>
<li><a href="add_car.php" class="btn btn-outline" style="width: 100%; text-align: left; border-color: var(--primary); color: var(--primary);">+ Add New Car</a></li>
<li><a href="favorites.php" class="btn btn-outline" style="width: 100%; text-align: left;">Favorites</a></li>
<li><a href="profile.php" class="btn btn-outline" style="width: 100%; text-align: left;">Settings</a></li>
<li><hr style="border: none; border-top: 1px solid var(--border-glass); margin: 0.5rem 0;"></li>
<li><a href="../logout.php" class="btn btn-outline" style="width: 100%; text-align: left; color: var(--danger);">Logout</a></li>
</ul>
</div>
<?php if ($user['role'] === 'admin'): ?>
<div style="margin-top: 2rem; text-align: center;">
<a href="../admin/dashboard.php" class="btn btn-primary" style="width: 100%;">Admin Panel</a>
</div>
<?php endif; ?>
</aside>
<!-- Main Dashboard Content -->
<section>
<h1 style="margin-bottom: 2rem;">Overview</h1>
<!-- Stats Row -->
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; margin-bottom: 3rem;">
<div class="glass-card" style="padding: 1.5rem; text-align: center;">
<h4 style="color: var(--text-muted); margin-bottom: 0.5rem;">Active Listings</h4>
<div style="font-size: 2rem; font-weight: 800;"><?php echo count($myCars); ?></div>
</div>
<div class="glass-card" style="padding: 1.5rem; text-align: center;">
<h4 style="color: var(--text-muted); margin-bottom: 0.5rem;">Purchased Cars</h4>
<div style="font-size: 2rem; font-weight: 800;"><?php echo count($myPurchases); ?></div>
</div>
<div class="glass-card" style="padding: 1.5rem; text-align: center;">
<h4 style="color: var(--text-muted); margin-bottom: 0.5rem;">Favorites</h4>
<div style="font-size: 2rem; font-weight: 800;">0</div>
</div>
</div>
<!-- Recent Activity Table -->
<div class="glass-card" style="padding: 2.5rem; margin-bottom: 3rem;">
<h3>Recent Purchases</h3>
<div style="margin-top: 1.5rem; overflow-x: auto;">
<?php if (empty($myPurchases)): ?>
<p style="color: var(--text-muted); text-align: center; padding: 2rem;">You haven't purchased any cars yet.</p>
<?php else: ?>
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr style="text-align: left; border-bottom: 1px solid var(--border-glass);">
<th style="padding: 1rem;">Transaction ID</th>
<th style="padding: 1rem;">Car</th>
<th style="padding: 1rem;">Bank</th>
<th style="padding: 1rem;">Amount</th>
<th style="padding: 1rem;">Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($myPurchases as $p): ?>
<tr style="border-bottom: 1px solid var(--border-glass); color: var(--text-muted);">
<td style="padding: 1rem; color: var(--primary); font-weight: 600;"><?php echo $p['transaction_id']; ?></td>
<td style="padding: 1rem; color: white;"><?php echo htmlspecialchars($p['car_title']); ?></td>
<td style="padding: 1rem;"><?php echo htmlspecialchars($p['bank_name']); ?></td>
<td style="padding: 1rem;">$<?php echo number_format($p['amount'], 0); ?></td>
<td style="padding: 1rem;"><?php echo date('M d, Y', strtotime($p['purchase_date'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
<!-- Notifications Section -->
<div class="glass-card" style="padding: 2.5rem;">
<h3>Recent Notifications</h3>
<div style="margin-top: 1.5rem;">
<?php if (empty($notifications)): ?>
<p style="color: var(--text-muted); text-align: center; padding: 2rem;">No notifications at this time.</p>
<?php else: ?>
<?php foreach ($notifications as $n): ?>
<div style="padding: 1.5rem; background: var(--bg-glass); border-radius: var(--radius-md); margin-bottom: 1rem; border-left: 4px solid var(--primary);">
<div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
<strong><?php echo htmlspecialchars($n['title']); ?></strong>
<span style="font-size: 0.75rem; color: var(--text-muted);"><?php echo date('M d, H:i', strtotime($n['created_at'])); ?></span>
</div>
<p style="color: var(--text-muted); font-size: 0.9rem;"><?php echo htmlspecialchars($n['message']); ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</section>
</div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>