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

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

$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.deleted_at IS NULL AND (c.status = 'approved' OR c.status = 'sold')";
$params = [];

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

if ($city) {
    $query .= " AND city = ?";
    $params[] = $city;
}

if ($brand) {
    $query .= " AND brand = ?";
    $params[] = $brand;
}

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

// Fetch distinct cities and brands for filters
$cities = $pdo->query("SELECT DISTINCT city FROM cars WHERE status = 'approved'")->fetchAll(PDO::FETCH_COLUMN);
$brands = $pdo->query("SELECT DISTINCT brand FROM cars WHERE status = 'approved'")->fetchAll(PDO::FETCH_COLUMN);
?>

<div class="container" style="padding: 2rem 0;">
    <div style="margin-bottom: 4rem;">
        <h1 style="font-size: 3rem; font-weight: 900; margin-bottom: 1rem;">Premium Inventory</h1>
        <p style="color: var(--text-secondary); font-size: 1.2rem;">Find the perfect vehicle for your lifestyle in our verified marketplace.</p>
    </div>

    <!-- Filters -->
    <div class="glass" style="padding: 2rem; margin-bottom: 4rem;">
        <form method="GET" style="display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 1.5rem; align-items: end;">
            <div class="form-group" style="margin-bottom: 0;">
                <label>Search</label>
                <input type="text" name="search" class="form-control" placeholder="Search brand or model..." value="<?= htmlspecialchars($search) ?>">
            </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="<?= $c ?>" <?= $city === $c ? 'selected' : '' ?>><?= $c ?></option>
                    <?php endforeach; ?>
                </select>
            </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="<?= $b ?>" <?= $brand === $b ? 'selected' : '' ?>><?= $b ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary" style="height: 50px;">Apply Filters</button>
        </form>
    </div>

    <!-- Listings -->
    <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 2.5rem;">
        <?php foreach ($cars as $car): ?>
            <div class="glass car-card" style="padding: 0; overflow: hidden; position: relative;">
                <div style="height: 220px; background-image: url('<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>'); background-size: cover; background-position: center; position: relative;">
                    <?php if ($car['status'] === 'sold'): ?>
                        <div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.65); display: flex; align-items: center; justify-content: center; z-index: 2;">
                            <div style="border: 4px solid #fff; color: #fff; padding: 10px 25px; font-weight: 900; font-size: 2rem; transform: rotate(-15deg); letter-spacing: 4px;">SOLD</div>
                        </div>
                    <?php elseif ($car['is_hot_deal']): ?>
                        <span style="position: absolute; top: 1.5rem; left: 1.5rem; background: var(--danger); color: white; padding: 0.5rem 1rem; border-radius: 30px; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; z-index: 1;">Hot Deal</span>
                    <?php endif; ?>
                </div>
                <div style="padding: 2rem;">
                    <h3 style="font-size: 1.4rem; font-weight: 800; margin-bottom: 0.5rem;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                    <p style="color: var(--text-secondary); margin-bottom: 1.5rem; font-size: 0.95rem; display: flex; align-items: center; gap: 0.5rem;">
                        <span>📍</span> <?= htmlspecialchars($car['city']) ?> • <?= $car['year'] ?>
                    </p>
                    <div style="display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--glass-border); padding-top: 1.5rem;">
                        <span style="font-size: 1.5rem; font-weight: 900; color: var(--primary-color);">$<?= number_format($car['price']) ?></span>
                        <a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-primary btn-sm" style="<?= $car['status'] === 'sold' ? 'background: #555; pointer-events: none;' : '' ?>">View Details</a>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <?php if (empty($cars)): ?>
        <div style="text-align: center; padding: 10rem 0;">
            <div style="font-size: 4rem; margin-bottom: 2rem;">🔍</div>
            <h2 style="font-weight: 800; margin-bottom: 1rem;">No vehicles found</h2>
            <p style="color: var(--text-secondary);">Try adjusting your filters or search terms.</p>
        </div>
    <?php endif; ?>
</div>

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