<?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: 6rem 0;">
    <div class="page-header" style="margin-bottom: 5rem;">
        <div>
            <h1 class="fw-black" style="font-size: 4rem; color: #fff; line-height: 1;">Premium Inventory</h1>
            <p class="text-secondary fw-bold" style="font-size: 1.25rem;">Explore our curated selection of verified elite vehicles across Afghanistan.</p>
        </div>
    </div>

    <!-- Filters -->
    <div class="glass mb-4" style="padding: 3.5rem; border-top: 5px solid var(--primary-color);">
        <form method="GET" class="grid" style="grid-template-columns: 2fr 1fr 1fr 1fr; gap: 2rem; align-items: end;">
            <div class="form-group mb-0">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Vehicle Search</label>
                <input type="text" name="search" class="form-control" placeholder="Search brand or model..." value="<?= htmlspecialchars($search) ?>">
            </div>
            <div class="form-group mb-0">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Location</label>
                <select name="city" class="form-control">
                    <option value="">All Regions</option>
                    <?php foreach ($cities as $c): ?>
                        <option value="<?= $c ?>" <?= $city === $c ? 'selected' : '' ?>><?= $c ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group mb-0">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">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 fw-black" style="height: 56px; padding: 0 2rem; letter-spacing: 1px;">APPLY FILTERS</button>
        </form>
    </div>

    <!-- Listings -->
    <div class="grid grid-3 mt-4">
        <?php foreach ($cars as $car): ?>
            <div class="card car-card">
                <div style="height: 240px; 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 class="sold-overlay">
                            <div class="sold-stamp" style="font-size: 2.5rem; padding: 10px 20px; border-width: 5px;">SOLD</div>
                        </div>
                    <?php elseif ($car['is_hot_deal']): ?>
                        <span class="badge badge-danger" style="position: absolute; top: 1.5rem; left: 1.5rem; z-index: 5;">Hot Deal</span>
                    <?php endif; ?>
                </div>
                <div class="card-content" style="padding: 2.5rem;">
                    <div class="flex gap-1 text-secondary text-sm fw-black mb-1" style="text-transform: uppercase; letter-spacing: 1px;">
                        <span>📍 <?= htmlspecialchars($car['city']) ?></span>
                        <span>📅 <?= $car['year'] ?> Model</span>
                    </div>
                    <h3 class="fw-black mb-2" style="font-size: 1.6rem; color: #fff;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                    <div class="flex justify-between align-center mt-2 pt-2" style="border-top: 1px solid var(--glass-border);">
                        <span class="price-tag" style="font-size: 1.8rem;">$<?= number_format($car['price']) ?></span>
                        <?php if ($car['status'] === 'sold'): ?>
                            <a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-outline btn-sm fw-bold" style="opacity: 0.5;">View Info</a>
                        <?php else: ?>
                            <a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-primary btn-sm fw-bold">View Details</a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <?php if (empty($cars)): ?>
        <div class="text-center" style="padding: 12rem 0;">
            <div style="font-size: 5rem; margin-bottom: 2rem; filter: drop-shadow(0 10px 20px rgba(0,0,0,0.3));">🔍</div>
            <h2 class="fw-black mb-1" style="font-size: 2.5rem; color: #fff;">No premium vehicles found</h2>
            <p class="text-secondary fw-bold" style="font-size: 1.1rem;">Try adjusting your filters or refining your search criteria.</p>
        </div>
    <?php endif; ?>
</div>

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