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

$pdo = db();
$search = $_GET['q'] ?? '';
$brand = $_GET['brand'] ?? '';
$city = $_GET['city'] ?? '';

$query = "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.status = 'approved'";
$params = [];

if ($search) {
    $query .= " AND (c.brand LIKE ? OR c.model LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}
if ($brand) {
    $query .= " AND c.brand = ?";
    $params[] = $brand;
}
if ($city) {
    $query .= " AND c.city = ?";
    $params[] = $city;
}

$query .= " ORDER BY c.created_at DESC";
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$cars = $stmt->fetchAll();

$brands = $pdo->query("SELECT DISTINCT brand FROM cars WHERE status = 'approved'")->fetchAll(PDO::FETCH_COLUMN);
$cities = ['Kabul', 'Herat', 'Mazar-i-Sharif', 'Kandahar', 'Jalalabad', 'Kunduz', 'Ghazni', 'Balkh'];
?>

<div class="container" style="padding-top: 2rem;">
    <h1 class="section-title">Premium Marketplace</h1>
    
    <form class="glass" style="padding: 2.5rem; margin-bottom: 4rem; display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1.5rem; align-items: end; border-left: 4px solid var(--primary-color);">
        <div class="form-group" style="margin-bottom: 0;">
            <label>Keyword</label>
            <input type="text" name="q" value="<?= htmlspecialchars($search) ?>" class="form-control" placeholder="Search brand or model...">
        </div>
        <div class="form-group" style="margin-bottom: 0;">
            <label>Brand</label>
            <select name="brand" class="form-control">
                <option value="">All Brands</option>
                <?php foreach ($brands as $b): ?>
                    <option value="<?= htmlspecialchars($b) ?>" <?= $brand == $b ? 'selected' : '' ?>><?= htmlspecialchars($b) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group" style="margin-bottom: 0;">
            <label>City</label>
            <select name="city" class="form-control">
                <option value="">All Cities</option>
                <?php foreach ($cities as $c): ?>
                    <option value="<?= htmlspecialchars($c) ?>" <?= $city == $c ? 'selected' : '' ?>><?= htmlspecialchars($c) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="display: flex; gap: 0.5rem;">
            <button type="submit" class="btn btn-primary" style="flex: 2;">Apply Filters</button>
            <a href="cars.php" class="btn btn-outline" style="flex: 1; text-align: center; display: flex; align-items: center; justify-content: center; padding: 0;">✕</a>
        </div>
    </form>

    <div class="grid">
        <?php if (empty($cars)): ?>
            <div style="grid-column: 1/-1; text-align: center; padding: 6rem; background: rgba(255,255,255,0.02); border-radius: 20px;">
                <div style="font-size: 4rem; margin-bottom: 1.5rem; opacity: 0.3;">🚗💨</div>
                <h2 style="color: var(--text-secondary);">No vehicles found</h2>
                <p style="color: var(--text-secondary); margin-bottom: 2rem;">We couldn't find any cars matching your current filters.</p>
                <a href="cars.php" class="btn btn-primary">Clear all filters</a>
            </div>
        <?php else: ?>
            <?php foreach ($cars as $car): ?>
                <div class="car-card glass">
                    <div class="car-img-container" style="overflow: hidden; height: 240px; position: relative;">
                        <div class="car-img" style="background-image: url('<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>'); background-size: cover; background-position: center; height: 100%;"></div>
                        <?php if ($car['is_hot_deal']): ?>
                            <div style="position: absolute; top: 1rem; left: 1rem; background: var(--primary-color); color: #000; padding: 0.4rem 1rem; border-radius: 50px; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; letter-spacing: 1px;">Hot Deal</div>
                        <?php endif; ?>
                    </div>
                    <div class="car-info">
                        <div class="car-meta">
                            <span>📅 <?= htmlspecialchars($car['year']) ?></span>
                            <span>📍 <?= htmlspecialchars($car['city']) ?></span>
                        </div>
                        <h3><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                        <div class="car-price">$<?= number_format($car['price']) ?></div>
                        <a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-outline" style="width: 100%; text-align: center;">View Details</a>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

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