<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Products';
$pdo = getDB();

$categoryFilter = isset($_GET['category']) ? (int)$_GET['category'] : null;

$sql = "SELECT p.id, p.name, p.slug, p.description, p.image, c.name AS category_name, c.id AS category_id
        FROM products p
        LEFT JOIN product_categories c ON p.category_id = c.id
        WHERE p.is_active = 1";
$params = [];
if ($categoryFilter) {
    $sql .= " AND p.category_id = ?";
    $params[] = $categoryFilter;
}
$sql .= " ORDER BY p.sort_order, p.id";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $pdo->query("SELECT id, name, slug FROM product_categories WHERE is_active = 1 ORDER BY sort_order");
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

$assetBase = (BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$placeholder = $assetBase . 'assets/images/placeholder-product.svg';

require_once __DIR__ . '/includes/header.php';
?>
<div class="products-page-wrap">
    <header class="products-page-header">
        <div class="container products-page-header-inner">
            <div class="products-page-heading">
                <span class="products-page-eyebrow">Catalogue</span>
                <h1 class="products-page-title">Our Products</h1>
                <p class="products-page-lead">Corrugated boxes and packaging solutions—browse by category or view the full range.</p>
            </div>
            <?php if (!empty($categories)): ?>
            <nav class="products-category-nav" aria-label="Filter by category">
                <a href="<?php echo htmlspecialchars($assetBase); ?>products.php" class="cat-pill <?php echo !$categoryFilter ? 'is-active' : ''; ?>">All products</a>
                <?php foreach ($categories as $cat): ?>
                <a href="<?php echo htmlspecialchars($assetBase); ?>products.php?category=<?php echo (int)$cat['id']; ?>" class="cat-pill <?php echo $categoryFilter == $cat['id'] ? 'is-active' : ''; ?>"><?php echo htmlspecialchars($cat['name']); ?></a>
                <?php endforeach; ?>
            </nav>
            <?php endif; ?>
        </div>
    </header>

    <div class="page-content products-page">
        <div class="container">
            <?php if (empty($products)): ?>
            <div class="products-empty">
                <div class="products-empty-icon">📦</div>
                <h2>No products in this category</h2>
                <p>Try another filter or <a href="<?php echo htmlspecialchars($assetBase); ?>enquiry.php">send an enquiry</a> for custom requirements.</p>
                <a href="<?php echo htmlspecialchars($assetBase); ?>products.php" class="btn-glow btn-primary-modern" style="margin-top:16px;display:inline-block;">View all products</a>
            </div>
            <?php else: ?>
            <p class="products-count"><?php echo count($products); ?> product<?php echo count($products) !== 1 ? 's' : ''; ?> shown</p>
            <div class="products-grid">
                <?php foreach ($products as $i => $p):
                    $imgSrc = !empty($p['image']) ? $assetBase . UPLOAD_URL . 'products/' . $p['image'] : $placeholder;
                ?>
                <article class="product-tile">
                    <a href="<?php echo htmlspecialchars($assetBase); ?>product-detail.php?slug=<?php echo urlencode($p['slug']); ?>" class="product-tile-link">
                        <div class="product-tile-media">
                            <img src="<?php echo htmlspecialchars($imgSrc); ?>" alt="" loading="lazy" onerror="this.src='<?php echo htmlspecialchars($placeholder); ?>'">
                            <?php if (!empty($p['category_name'])): ?>
                            <span class="product-tile-badge"><?php echo htmlspecialchars($p['category_name']); ?></span>
                            <?php endif; ?>
                            <span class="product-tile-overlay">
                                <span class="product-tile-cta">View details</span>
                            </span>
                        </div>
                        <div class="product-tile-body">
                            <h2 class="product-tile-title"><?php echo htmlspecialchars($p['name']); ?></h2>
                            <?php if (!empty($p['description'])): ?>
                            <p class="product-tile-excerpt"><?php echo htmlspecialchars(mb_substr(strip_tags($p['description']), 0, 110)); ?><?php echo mb_strlen(strip_tags($p['description'])) > 110 ? '…' : ''; ?></p>
                            <?php else: ?>
                            <p class="product-tile-excerpt product-tile-excerpt--muted">Durable corrugated packaging. Click for full details.</p>
                            <?php endif; ?>
                        </div>
                    </a>
                </article>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
