<?php include __DIR__ . '/../layout/header.php'; ?>

<div class="container">
    <div style="border-bottom: 2px solid var(--border-color); padding-bottom: 10px; margin-bottom: 25px;">
        <h2 style="font-size: 24px; font-weight: 700; color: var(--text-primary);">
            <?php if (!empty($q)): ?>
                Arama Sonuçları: <span style="font-weight: 500; color: var(--text-secondary);">"<?php echo htmlspecialchars($q); ?>"</span>
            <?php elseif (isset($_GET['filter']) && $_GET['filter'] === 'featured'): ?>
                En Çok Tercih Edilenler
            <?php elseif (isset($_GET['filter']) && $_GET['filter'] === 'new'): ?>
                Yeni Ürünler
            <?php else: ?>
                Tüm Ürünler
            <?php endif; ?>
        </h2>
        <span style="font-size: 14px; color: var(--text-muted);"><?php echo $total; ?> eşleşen ürün bulundu.</span>
    </div>

    <?php
    $brandsListSearch = \App\Models\Settings::get('brands', []);
    $brandMapSearch = [];
    foreach ($brandsListSearch as $b) { $brandMapSearch[$b['slug']] = $b['name']; }
    ?>
    <?php if (!empty($products)): ?>
        <div class="products-grid">
            <?php foreach ($products as $prod): ?>
                <article class="product-card">
                    <div class="product-image">
                        <a href="<?php echo $baseUrl; ?>/<?php echo $prod->slug; ?>">
<img src="<?php echo $prod->getMainImage($baseUrl); ?>" alt="<?php echo htmlspecialchars($prod->name); ?>" loading="lazy">
                        </a>
                        <button class="quick-add" onclick="addToCart('<?php echo $prod->id; ?>', 1)">
                            <i class="fa fa-shopping-basket"></i> Sepete Ekle
                        </button>
                    </div>
                    <div class="product-body">
                        <div class="product-brand"><?php echo htmlspecialchars(isset($brandMapSearch[$prod->brand_slug ?? '']) ? $brandMapSearch[$prod->brand_slug ?? ''] : ''); ?></div>
                        <h4 class="product-title">
                            <a href="<?php echo $baseUrl; ?>/<?php echo $prod->slug; ?>">
                                <?php echo htmlspecialchars($prod->name); ?>
                            </a>
                        </h4>
                        <div class="product-price"><?php echo $prod->getFormattedPrice(); ?></div>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
        <?php if ($totalPages > 1): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
            <a href="?q=<?php echo urlencode($q); ?>&amp;page=<?php echo $page - 1; ?>" class="page-link">&laquo; Önceki</a>
            <?php endif; ?>
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <a href="?q=<?php echo urlencode($q); ?>&amp;page=<?php echo $i; ?>" class="page-link <?php echo $i === $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
            <?php if ($page < $totalPages): ?>
            <a href="?q=<?php echo urlencode($q); ?>&amp;page=<?php echo $page + 1; ?>" class="page-link">Sonraki &raquo;</a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    <?php else: ?>
        <div style="background-color: var(--bg-secondary); border: 1px dashed var(--border-color); border-radius: var(--radius-md); padding: 50px; text-align: center; color: var(--text-secondary);">
            <i class="fa fa-search" style="font-size: 50px; opacity: 0.2; margin-bottom: 20px;"></i>
            <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 10px;">Sonuç Bulunamadı</h3>
            <p>Aradığınız kelimeye uygun bir ürün veya model bulunamadı. Lütfen farklı anahtar kelimelerle deneyin.</p>
            <a href="<?php echo $baseUrl; ?>/" class="btn-primary" style="margin-top: 20px; display: inline-block;">Ana Sayfaya Dön</a>
        </div>
    <?php endif; ?>
</div>
<?php include __DIR__ . '/../layout/footer.php'; ?>
