<?php
require_once 'config.php';
require_once 'includes/product.php';

$pageTitle = 'Shop All Products';
$productModel = new Product();

$category = isset($_GET['category']) ? $_GET['category'] : null;
$search = isset($_GET['search']) ? $_GET['search'] : null;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 12;
$offset = ($page - 1) * $limit;

$products = $productModel->getAll($category, $search, $limit, $offset);
$totalProducts = $productModel->getCount($category);
$totalPages = ceil($totalProducts / $limit);
$categories = $productModel->getCategories();

require_once 'includes/header.php';
?>

<!-- Products Page -->
<section class="products-section" style="padding-top: calc(var(--header-height) + var(--spacing-xl));">
    <div class="container">
        <div class="section-header">
            <h1 class="section-title"><?php echo $category ? htmlspecialchars($category) : 'All Products'; ?></h1>
            <p style="color: var(--medium-gray);"><?php echo $totalProducts; ?> products</p>
        </div>
        
        <!-- Filters -->
        <div class="filters">
            <select class="filter-select" onchange="filterCategory(this.value)">
                <option value="">All Categories</option>
                <?php foreach ($categories as $cat): ?>
                    <option value="<?php echo urlencode($cat); ?>" <?php echo $category === $cat ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($cat); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            
            <form method="GET" style="display: flex; gap: var(--spacing-sm);">
                <?php if ($category): ?>
                    <input type="hidden" name="category" value="<?php echo htmlspecialchars($category); ?>">
                <?php endif; ?>
                <input type="text" name="search" class="search-input" placeholder="Search products..." value="<?php echo htmlspecialchars($search ?? ''); ?>">
                <button type="submit" class="btn btn-secondary">Search</button>
            </form>
        </div>
        
        <?php if (empty($products)): ?>
            <div style="text-align: center; padding: var(--spacing-xxl);">
                <p style="color: var(--medium-gray); font-size: 1.125rem;">No products found.</p>
                <a href="<?php echo SITE_URL; ?>/products.php" class="btn btn-primary" style="margin-top: var(--spacing-lg);">View All Products</a>
            </div>
        <?php else: ?>
            <div class="product-grid">
                <?php foreach ($products as $product): ?>
                    <div class="product-card">
                        <a href="<?php echo SITE_URL; ?>/product.php?id=<?php echo $product['id']; ?>">
                            <div class="product-image">
                                <img src="<?php echo htmlspecialchars($product['image_url']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                                <div class="product-overlay">
                                    <span class="btn btn-primary">View Details</span>
                                </div>
                            </div>
                            <div class="product-info">
                                <div class="product-category"><?php echo htmlspecialchars($product['category']); ?></div>
                                <h3 class="product-name"><?php echo htmlspecialchars($product['name']); ?></h3>
                                <div class="product-price"><?php echo formatPrice($product['price']); ?></div>
                            </div>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        
        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?>
                    <a href="?page=<?php echo $page - 1; ?><?php echo $category ? '&category=' . urlencode($category) : ''; ?><?php echo $search ? '&search=' . urlencode($search) : ''; ?>">&laquo;</a>
                <?php endif; ?>
                
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <a href="?page=<?php echo $i; ?><?php echo $category ? '&category=' . urlencode($category) : ''; ?><?php echo $search ? '&search=' . urlencode($search) : ''; ?>" <?php echo $page === $i ? 'class="active"' : ''; ?>>
                        <?php echo $i; ?>
                    </a>
                <?php endfor; ?>
                
                <?php if ($page < $totalPages): ?>
                    <a href="?page=<?php echo $page + 1; ?><?php echo $category ? '&category=' . urlencode($category) : ''; ?><?php echo $search ? '&search=' . urlencode($search) : ''; ?>">&raquo;</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<script>
function filterCategory(category) {
    if (category) {
        window.location.href = '<?php echo SITE_URL; ?>/products.php?category=' + category;
    } else {
        window.location.href = '<?php echo SITE_URL; ?>/products.php';
    }
}
</script>

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