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

<div class="container" style="padding-top:28px;padding-bottom:48px;">

    <!-- Breadcrumb -->
    <nav class="breadcrumb">
        <a href="<?php echo $baseUrl; ?>/"><i class="fa fa-home"></i></a>
        <span>/</span>
        <?php if ($parent = $category->getParent()): ?>
        <a href="<?php echo $baseUrl; ?>/<?php echo $parent->slug; ?>"><?php echo htmlspecialchars($parent->name); ?></a>
        <span>/</span>
        <?php endif; ?>
        <span style="color:var(--text-1);"><?php echo htmlspecialchars($category->name); ?></span>
    </nav>

    <div class="cat-page-grid">

        <!-- Sidebar -->
        <aside class="sidebar">
            <?php if (!empty($subcategories)): ?>
            <div class="sidebar-card">
                <h4>Alt Kategoriler</h4>
                <ul>
                    <?php foreach ($subcategories as $sub): ?>
                    <li>
                        <a href="<?php echo $baseUrl; ?>/<?php echo $sub->slug; ?>">
                            <?php echo htmlspecialchars($sub->name); ?>
                            <i class="fa fa-chevron-right"></i>
                        </a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>

            <div class="sidebar-card">
                <h4>Tüm Kategoriler</h4>
                <ul>
                    <?php foreach (\App\Models\Category::all(['parent_slug' => null]) as $sidebarCat): ?>
                    <li>
                        <a href="<?php echo $baseUrl; ?>/<?php echo $sidebarCat->slug; ?>"
                           class="<?php echo $category->slug === $sidebarCat->slug ? 'active' : ''; ?>">
                            <?php echo htmlspecialchars($sidebarCat->name); ?>
                        </a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </aside>

        <!-- Main Content -->
        <main>
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:24px;flex-wrap:wrap;gap:12px;">
                <div>
                    <h1 style="font-size:22px;font-weight:800;color:var(--text-1);"><?php echo htmlspecialchars($category->name); ?></h1>
                </div>
                <span style="font-size:13px;color:var(--text-3);"><?php echo $total; ?> ürün listeleniyor</span>
            </div>

            <?php
            $brandsListCat = \App\Models\Settings::get('brands', []);
            $brandMapCat = [];
            foreach ($brandsListCat as $b) { $brandMapCat[$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($brandMapCat[$prod->brand_slug ?? '']) ? $brandMapCat[$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="?page=<?php echo $page - 1; ?>" class="page-link">&laquo; Önceki</a>
                <?php endif; ?>
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="page-link <?php echo $i === $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
                <?php endfor; ?>
                <?php if ($page < $totalPages): ?>
                <a href="?page=<?php echo $page + 1; ?>" class="page-link">Sonraki &raquo;</a>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            <?php else: ?>
            <div class="card" style="text-align:center;padding:60px;color:var(--text-3);">
                <i class="fa fa-print" style="font-size:48px;opacity:0.2;margin-bottom:16px;display:block;"></i>
                <p style="font-size:15px;font-weight:500;margin-bottom:16px;">Bu kategoride henüz ürün eklenmemiş.</p>
                <a href="<?php echo $baseUrl; ?>/" class="btn btn-outline">Ana Sayfaya Dön</a>
            </div>
            <?php endif; ?>
        </main>
    </div>
</div>
<?php include __DIR__ . '/../layout/footer.php'; ?>
