<?php
$page_title = "Koleksi Produk Batu Alam";
$page_desc = "Cari berbagai batu alam andesit, travertine, candi, marmer, dan paras jogja di PT Namora Nexa Niaga. Hubungi kami untuk harga kustom.";
require_once 'includes/header.php';

if (!isset($settings['menu_products_visible']) || $settings['menu_products_visible'] == 0) {
    header("Location: index.php");
    exit;
}

// Category filter
$cat_filter = isset($_GET['cat']) ? trim($_GET['cat']) : '';

// Fetch all available categories for the filter links
try {
    $stmt = $pdo->prepare("SELECT name FROM categories ORDER BY name ASC");
    $stmt->execute();
    $categories = $stmt->fetchAll(PDO::FETCH_COLUMN);
} catch (Exception $e) {
    $categories = [];
}

// Fetch products based on category filter
try {
    if ($cat_filter != '') {
        $stmt = $pdo->prepare("SELECT * FROM products WHERE category = ? ORDER BY id DESC");
        $stmt->execute([$cat_filter]);
    } else {
        $stmt = $pdo->prepare("SELECT * FROM products ORDER BY id DESC");
        $stmt->execute();
    }
    $products = $stmt->fetchAll();
} catch (Exception $e) {
    $products = [];
}
?>

<!-- Page Header Banner -->
<section style="background: linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.8)), url('assets/images/hero_bg.jpg') no-repeat center center/cover; padding: 120px 0 60px; text-align: center; color: var(--text-light);">
    <div class="container">
        <h1 style="font-size: 42px; font-weight: 800; margin-bottom: 10px;">Produk Kami</h1>
        <p style="color: var(--primary); font-weight: 600; letter-spacing: 2px; text-transform: uppercase; font-size: 14px;">Batu Alam Berkualitas Tinggi</p>
    </div>
</section>

<!-- Product List Section -->
<section class="bg-alt">
    <div class="container">
        <!-- Category Filter Pills -->
        <div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-bottom: 50px;">
            <a href="products.php" class="btn-secondary" style="border-color: <?php echo $cat_filter == '' ? 'var(--primary)' : 'var(--border-color)'; ?>; background-color: <?php echo $cat_filter == '' ? 'var(--primary)' : 'transparent'; ?>; color: <?php echo $cat_filter == '' ? '#fff' : 'var(--text-dark)'; ?>; padding: 8px 20px; font-size: 14px; font-weight: 600; border-radius: 30px;">
                Semua Kategori
            </a>
            <?php foreach ($categories as $cat): ?>
                <a href="products.php?cat=<?php echo urlencode($cat); ?>" class="btn-secondary" style="border-color: <?php echo $cat_filter == $cat ? 'var(--primary)' : 'var(--border-color)'; ?>; background-color: <?php echo $cat_filter == $cat ? 'var(--primary)' : 'transparent'; ?>; color: <?php echo $cat_filter == $cat ? '#fff' : 'var(--text-dark)'; ?>; padding: 8px 20px; font-size: 14px; font-weight: 600; border-radius: 30px;">
                    <?php echo htmlspecialchars($cat); ?>
                </a>
            <?php endforeach; ?>
        </div>
        
        <!-- Products Grid -->
        <div class="grid-3">
            <?php if (count($products) > 0): ?>
                <?php foreach ($products as $product): ?>
                    <div class="product-card">
                        <div class="product-img-wrapper">
                            <span class="product-category"><?php echo htmlspecialchars($product['category']); ?></span>
                            <?php 
                            $img_src = '';
                            if (!empty($product['image']) && file_exists('uploads/' . $product['image'])) {
                                $img_src = 'uploads/' . $product['image'];
                            } elseif (!empty($product['image']) && file_exists('assets/images/' . $product['image'])) {
                                $img_src = 'assets/images/' . $product['image'];
                            } else {
                                $img_src = get_product_fallback_image($product['category'], $product['name']);
                            }
                            ?>
                            <img src="<?php echo $img_src; ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                        </div>
                        <div class="product-info">
                            <h3 class="product-title"><?php echo htmlspecialchars($product['name']); ?></h3>
                            <p class="product-description"><?php echo htmlspecialchars($product['description']); ?></p>
                            <div class="product-specs">
                                <span>Finishing: <strong><?php echo htmlspecialchars($product['finishing']); ?></strong></span>
                                <span>Ukuran: <strong><?php echo htmlspecialchars($product['size_available']); ?></strong></span>
                            </div>
                            <a href="product-detail.php?id=<?php echo $product['id']; ?>" class="product-link">Detail Produk</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div style="grid-column: 1/-1; text-align: center; padding: 60px 0;">
                    <i class="fas fa-box-open" style="font-size: 48px; color: var(--border-color); margin-bottom: 20px;"></i>
                    <p style="color: var(--text-muted); font-size: 16px;">Maaf, produk untuk kategori ini belum tersedia.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>

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