<?php
$page_title = "Katalog Kue Premium";
require_once __DIR__ . '/includes/header.php';

// Ambil data kategori unik dari produk
$cat_query = $db->query("SELECT DISTINCT category FROM products WHERE is_active = 1");
$categories = $cat_query->fetchAll(PDO::FETCH_COLUMN);

// Filter berdasarkan kategori jika dipilih
$category_filter = isset($_GET['category']) ? $_GET['category'] : '';

if ($category_filter) {
    $stmt = $db->prepare("SELECT * FROM products WHERE is_active = 1 AND category = ? ORDER BY id DESC");
    $stmt->execute([$category_filter]);
    $products = $stmt->fetchAll();
} else {
    $products = $db->query("SELECT * FROM products WHERE is_active = 1 ORDER BY id DESC")->fetchAll();
}
?>

<section class="hero">
    <div class="hero-content">
        <p class="hero-subtitle">Toko Kue Edelys</p>
        <h1>Manjakan Lidah Anda Dengan Kelezatan Autentik</h1>
        <p>Setiap adonan di Toko Kue Edelys dipanggang dengan penuh cinta menggunakan bahan-bahan premium pilihan tanpa bahan pengawet. Kami menghadirkan cita rasa bintang lima untuk momen spesial Anda.</p>
        <a href="#katalog" class="btn"><i class="fa-solid fa-cookie-bite"></i> Jelajahi Kue Kami</a>
    </div>
</section>

<main id="katalog">
    <div class="catalog-header">
        <h2 class="section-title">Katalog Kue Pilihan</h2>
        
        <div class="filter-container">
            <a href="index.php#katalog" class="filter-btn <?php echo !$category_filter ? 'active' : ''; ?>">Semua</a>
            <?php foreach ($categories as $cat): ?>
                <a href="index.php?category=<?php echo urlencode($cat); ?>#katalog" 
                   class="filter-btn <?php echo $category_filter == $cat ? 'active' : ''; ?>">
                    <?php echo htmlspecialchars($cat); ?>
                </a>
            <?php endforeach; ?>
        </div>
    </div>

    <?php if (count($products) > 0): ?>
        <div class="product-grid">
            <?php foreach ($products as $p): ?>
                <div class="product-card">
                    <div class="product-image-container">
                        <img src="<?php echo htmlspecialchars($p['image_url']); ?>" alt="<?php echo htmlspecialchars($p['name']); ?>">
                        
                        <?php if ($p['stock'] <= 0): ?>
                            <span class="product-badge out-of-stock">Habis</span>
                        <?php elseif ($p['stock'] <= 5): ?>
                            <span class="product-badge low-stock">Tinggal <?php echo $p['stock']; ?></span>
                        <?php else: ?>
                            <span class="product-badge ready">Tersedia</span>
                        <?php endif; ?>
                    </div>
                    
                    <div class="product-info">
                        <span class="product-category"><?php echo htmlspecialchars($p['category']); ?></span>
                        <h3 class="product-name"><?php echo htmlspecialchars($p['name']); ?></h3>
                        <p class="product-desc"><?php echo htmlspecialchars($p['description']); ?></p>
                        
                        <div class="product-footer">
                            <div class="product-price">
                                Rp <?php echo number_format($p['price'], 0, ',', '.'); ?>
                            </div>
                            <div>
                                <a href="product.php?id=<?php echo $p['id']; ?>" class="btn" style="padding: 0.5rem 1.2rem; font-size: 0.9rem;">
                                    Detail <i class="fa-solid fa-circle-info"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div style="text-align: center; padding: 4rem 1rem; background: white; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); border: 1px solid var(--border-color);">
            <i class="fa-solid fa-cookie" style="font-size: 3.5rem; color: var(--accent-color); margin-bottom: 1rem;"></i>
            <h3>Kue Tidak Ditemukan</h3>
            <p style="color: var(--text-secondary); margin-top: 0.5rem;">Maaf, tidak ada produk kue di kategori ini untuk saat ini.</p>
        </div>
    <?php endif; ?>
</main>

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