<?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: 2rem 0;">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 3rem;">
        <div>
            <h1 style="font-size: 2.5rem; font-weight: 900; margin-bottom: 0.5rem;">User Dashboard</h1>
            <p style="color: var(--text-secondary);">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 style="display: grid; grid-template-columns: 1fr; gap: 4rem;">
        
        <!-- My Listings Section -->
        <section>
            <h2 style="font-size: 1.8rem; font-weight: 800; margin-bottom: 2rem; display: flex; align-items: center; gap: 1rem;">
                <span style="background: var(--primary-color); color: white; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border-radius: 10px; font-size: 1.2rem;">🚗</span>
                My Vehicle Listings
            </h2>
            
            <?php if (empty($my_listings)): ?>
                <div class="glass" style="padding: 4rem; text-align: center;">
                    <p style="color: var(--text-secondary); margin-bottom: 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 style="display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 2rem;">
                    <?php foreach ($my_listings as $car): ?>
                        <div class="glass" style="padding: 0; overflow: hidden; position: relative;">
                            <div style="height: 180px; background-image: url('<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>'); background-size: cover; background-position: center;">
                                <?php if ($car['status'] === 'sold'): ?>
                                    <div style="position: absolute; top: 0; left: 0; width: 100%; height: 180px; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; color: white; font-weight: 900; font-size: 2rem; letter-spacing: 5px;">SOLD</div>
                                <?php endif; ?>
                            </div>
                            <div style="padding: 1.5rem;">
                                <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1rem;">
                                    <div>
                                        <h3 style="font-size: 1.2rem; font-weight: 700; margin-bottom: 0.2rem;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                                        <p style="color: var(--text-secondary); font-size: 0.85rem;"><?= $car['year'] ?> - <?= htmlspecialchars($car['city']) ?></p>
                                    </div>
                                    <span class="badge" style="background: <?= $car['status'] === 'approved' ? 'var(--success)' : ($car['status'] === 'sold' ? '#000' : ($car['status'] === 'rejected' ? 'var(--danger)' : 'var(--primary-color)')) ?>; color: white; padding: 0.3rem 0.6rem; border-radius: 8px; font-size: 0.7rem; text-transform: uppercase; font-weight: 700;">
                                        <?= $car['status'] ?>
                                    </span>
                                </div>
                                <div style="display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--glass-border); padding-top: 1rem; margin-top: 1rem;">
                                    <span style="font-weight: 800; color: var(--primary-color);">$<?= number_format($car['price']) ?></span>
                                    <a href="edit_car.php?id=<?= $car['id'] ?>" style="color: var(--text-secondary); font-size: 0.9rem; font-weight: 600;">Edit Details</a>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </section>

        <!-- My Purchase Requests Section -->
        <section>
            <h2 style="font-size: 1.8rem; font-weight: 800; margin-bottom: 2rem; display: flex; align-items: center; gap: 1rem;">
                <span style="background: var(--primary-color); color: white; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border-radius: 10px; font-size: 1.2rem;">💰</span>
                My Purchase Requests
            </h2>
            
            <?php if (empty($my_purchases)): ?>
                <div class="glass" style="padding: 4rem; text-align: center;">
                    <p style="color: var(--text-secondary); margin-bottom: 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="glass" style="padding: 0; overflow: hidden;">
                    <table style="width: 100%; border-collapse: collapse; text-align: left;">
                        <thead>
                            <tr style="background: rgba(0,0,0,0.03);">
                                <th style="padding: 1.5rem;">Vehicle</th>
                                <th style="padding: 1.5rem;">Price</th>
                                <th style="padding: 1.5rem;">Bank ID</th>
                                <th style="padding: 1.5rem;">Status</th>
                                <th style="padding: 1.5rem;">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($my_purchases as $p): ?>
                                <tr style="border-bottom: 1px solid var(--glass-border);">
                                    <td style="padding: 1.2rem 1.5rem;">
                                        <div style="display: flex; align-items: center; gap: 1rem;">
                                            <img src="<?= htmlspecialchars($p['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>" style="width: 60px; height: 40px; object-fit: cover; border-radius: 4px;">
                                            <div>
                                                <div style="font-weight: 700; font-size: 0.95rem;"><?= htmlspecialchars($p['brand'] . ' ' . $p['model']) ?></div>
                                                <div style="font-size: 0.75rem; color: var(--text-secondary);"><?= $p['year'] ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td style="padding: 1.2rem 1.5rem; font-weight: 700;">$<?= number_format($p['price']) ?></td>
                                    <td style="padding: 1.2rem 1.5rem;"><code style="font-size: 0.8rem;"><?= htmlspecialchars($p['bank_id']) ?></code></td>
                                    <td style="padding: 1.2rem 1.5rem;">
                                        <span style="color: <?= $p['status'] === 'approved' ? 'var(--success)' : ($p['status'] === 'rejected' ? 'var(--danger)' : 'var(--primary-color)') ?>; font-weight: 700; font-size: 0.85rem; text-transform: uppercase;">
                                            <?= $p['status'] ?>
                                        </span>
                                    </td>
                                    <td style="padding: 1.2rem 1.5rem;">
                                        <?php if ($p['status'] === 'approved'): ?>
                                            <a href="receipt.php?id=<?= $p['id'] ?>" class="btn btn-primary" style="padding: 0.4rem 0.8rem; font-size: 0.8rem;">View Receipt</a>
                                        <?php else: ?>
                                            <span style="color: var(--text-secondary); font-size: 0.8rem;">Waiting for review</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </section>

    </div>
</div>

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