<?php
require_once __DIR__ . '/includes/header.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

$pdo = db();
$user_id = $_SESSION['user_id'];

// Fetch user's car listings
$stmt = $pdo->prepare("
    SELECT c.*, ci.image_path 
    FROM cars c 
    LEFT JOIN car_images ci ON c.id = ci.car_id AND ci.is_main = 1 
    WHERE c.user_id = ? AND c.deleted_at IS NULL 
    ORDER BY c.created_at DESC
");
$stmt->execute([$user_id]);
$my_listings = $stmt->fetchAll();

// Fetch user's purchase requests
$stmt = $pdo->prepare("
    SELECT p.*, c.brand, c.model, c.year, c.price, ci.image_path 
    FROM purchases p 
    JOIN cars c ON p.car_id = c.id 
    LEFT JOIN car_images ci ON c.id = ci.car_id AND ci.is_main = 1 
    WHERE p.user_id = ? 
    ORDER BY p.created_at DESC
");
$stmt->execute([$user_id]);
$my_purchases = $stmt->fetchAll();
?>

<div class="container" style="padding: 4rem 0;">
    <div class="page-header">
        <div>
            <h1>User Dashboard</h1>
            <p>Manage your vehicle listings and track your purchase requests.</p>
        </div>
        <a href="add_car.php" class="btn btn-primary">List New Vehicle</a>
    </div>

    <div class="grid" style="grid-template-columns: 1fr; gap: 5rem;">
        
        <!-- My Listings Section -->
        <section>
            <h2 class="section-title">
                <span class="section-icon">🚗</span>
                My Vehicle Listings
            </h2>
            
            <?php if (empty($my_listings)): ?>
                <div class="box text-center">
                    <p class="text-secondary mb-2" style="font-size: 1.2rem;">You haven't listed any vehicles yet.</p>
                    <a href="add_car.php" class="btn btn-outline">Start Selling Today</a>
                </div>
            <?php else: ?>
                <div class="grid grid-3">
                    <?php foreach ($my_listings as $car): ?>
                        <div class="card" style="position: relative;">
                            <div style="height: 200px; background-image: url('<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>'); background-size: cover; background-position: center;">
                                <?php if ($car['status'] === 'sold'): ?>
                                    <div class="sold-overlay">SOLD</div>
                                <?php endif; ?>
                            </div>
                            <div class="card-content">
                                <div class="flex justify-between align-center mb-2">
                                    <div>
                                        <h3 class="fw-black mb-1" style="font-size: 1.4rem;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                                        <p class="text-secondary text-sm fw-bold"><?= $car['year'] ?> • <?= htmlspecialchars($car['city']) ?></p>
                                    </div>
                                    <span class="badge badge-<?= $car['status'] === 'approved' ? 'success' : ($car['status'] === 'sold' ? 'info' : ($car['status'] === 'rejected' ? 'danger' : 'primary')) ?>">
                                        <?= $car['status'] ?>
                                    </span>
                                </div>
                                <div class="flex justify-between align-center mt-2 pt-2" style="border-top: 1px solid var(--glass-border);">
                                    <span class="price-tag">$<?= number_format($car['price']) ?></span>
                                    <a href="edit_car.php?id=<?= $car['id'] ?>" class="text-gold fw-bold text-sm" style="text-decoration: none;">
                                        Edit Details →
                                    </a>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </section>

        <!-- My Purchase Requests Section -->
        <section>
            <h2 class="section-title">
                <span class="section-icon">💰</span>
                My Purchase Requests
            </h2>
            
            <?php if (empty($my_purchases)): ?>
                <div class="box text-center">
                    <p class="text-secondary mb-2" style="font-size: 1.2rem;">You haven't made any purchase requests yet.</p>
                    <a href="cars.php" class="btn btn-outline">Browse Marketplace</a>
                </div>
            <?php else: ?>
                <div class="box" style="padding: 0; overflow: hidden;">
                    <div class="table-container">
                        <table>
                            <thead>
                                <tr>
                                    <th>Vehicle</th>
                                    <th>Price</th>
                                    <th>Bank ID</th>
                                    <th>Status</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($my_purchases as $p): ?>
                                    <tr>
                                        <td>
                                            <div class="flex align-center gap-1">
                                                <img src="<?= htmlspecialchars($p['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>" style="width: 80px; height: 50px; object-fit: cover; border-radius: 8px;">
                                                <div>
                                                    <div class="fw-black" style="font-size: 1.1rem; color: #fff;"><?= htmlspecialchars($p['brand'] . ' ' . $p['model']) ?></div>
                                                    <div class="text-sm text-secondary fw-bold"><?= $p['year'] ?> Model</div>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="price-tag">$<?= number_format($p['price']) ?></td>
                                        <td><code class="text-gold fw-bold" style="background: rgba(255,255,255,0.05); padding: 0.4rem 0.8rem; border-radius: 6px;"><?= htmlspecialchars($p['bank_id']) ?></code></td>
                                        <td>
                                            <span class="badge badge-<?= $p['status'] === 'approved' ? 'success' : ($p['status'] === 'rejected' ? 'danger' : 'primary') ?>">
                                                <?= $p['status'] ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php if ($p['status'] === 'approved'): ?>
                                                <a href="receipt.php?id=<?= $p['id'] ?>" class="btn btn-primary btn-sm">View Receipt</a>
                                            <?php else: ?>
                                                <span class="text-secondary text-sm fw-bold">Under Review</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            <?php endif; ?>
        </section>

    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>