<?php
// shop.php
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/functions.php';

// Get filter parameters
$category_slug = $_GET['category'] ?? '';
$subcategory_slug = $_GET['subcategory'] ?? '';
$search = $_GET['search'] ?? '';

// Build query
$sql = "SELECT p.*, c.name as category_name, s.name as subcategory_name 
        FROM products p 
        LEFT JOIN categories c ON p.category_id = c.id 
        LEFT JOIN subcategories s ON p.subcategory_id = s.id 
        WHERE p.status = 1";

$params = [];

if ($category_slug) {
    $sql .= " AND c.slug = ?";
    $params[] = $category_slug;
}

if ($subcategory_slug) {
    $sql .= " AND s.slug = ?";
    $params[] = $subcategory_slug;
}

if ($search) {
    $sql .= " AND (p.name LIKE ? OR p.description LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$sql .= " ORDER BY p.created_at DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$products = $stmt->fetchAll();

// Get categories for filter
$categories = getCategories($pdo);

include __DIR__ . '/includes/header.php';
?>

<div class="container py-4">
    <div class="row">
        <!-- Sidebar -->
        <div class="col-lg-3 d-none d-lg-block">
            <div class="sidebar-card">
                <h5 class="sidebar-title">Categories</h5>
                <ul class="sidebar-menu">
                    <li><a href="shop.php" class="<?php echo !$category_slug && !$subcategory_slug ? 'active' : ''; ?>">All Products</a></li>
                    <?php foreach ($categories as $cat): ?>
                    <li>
                        <a href="shop.php?category=<?php echo $cat['slug']; ?>" class="<?php echo $category_slug == $cat['slug'] ? 'active' : ''; ?>">
                            <?php echo $cat['name']; ?>
                        </a>
                        <?php 
                        $subs = getSubcategories($pdo, $cat['id']);
                        if (!empty($subs) && $category_slug == $cat['slug']): 
                        ?>
                        <ul class="sub-menu">
                            <?php foreach ($subs as $sub): ?>
                            <li>
                                <a href="shop.php?subcategory=<?php echo $sub['slug']; ?>" class="<?php echo $subcategory_slug == $sub['slug'] ? 'active' : ''; ?>">
                                    - <?php echo $sub['name']; ?>
                                </a>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
        
        <!-- Products -->
        <div class="col-lg-9">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4><?php echo $search ? 'Search Results' : 'All Products'; ?></h4>
                <span class="text-muted"><?php echo count($products); ?> products found</span>
            </div>
            
            <?php if (empty($products)): ?>
            <div class="alert alert-info">No products found.</div>
            <?php else: ?>
            <div class="row g-2 g-lg-3">
                <?php foreach ($products as $product): ?>
                <div class="col-xxl-3 col-xl-4 col-lg-4 col-md-4 col-sm-4 col-4">
                    <div class="product-card">
                        <div class="image">
                            <a href="product.php?slug=<?php echo $product['slug']; ?>">
                                <img src="assets/images/products/<?php echo $product['image'] ?: 'default.jpg'; ?>" alt="<?php echo $product['name']; ?>">
                            </a>
                            <?php if ($product['sale_price']): ?>
                            <span class="discount">-<?php echo round((1 - $product['sale_price']/$product['price']) * 100); ?>%</span>
                            <?php endif; ?>
                        </div>
                        <div class="content">
                            <a href="product.php?slug=<?php echo $product['slug']; ?>" class="name"><?php echo $product['name']; ?></a>
                            <div class="price">
                                <span class="current"><?php echo number_format($product['sale_price'] ?: $product['price']); ?> TK</span>
                                <?php if ($product['sale_price']): ?>
                                <span class="old"><?php echo number_format($product['price']); ?> TK</span>
                                <?php endif; ?>
                            </div>
                            <a href="product.php?slug=<?php echo $product['slug']; ?>" class="btn-order">View Details</a>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
.sidebar-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.sidebar-title {
    font-weight: 600;
    padding-bottom: 10px;
    border-bottom: 2px solid #e2e8f0;
    margin-bottom: 15px;
}

.sidebar-menu {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar-menu > li {
    border-bottom: 1px solid #f1f5f9;
}

.sidebar-menu > li:last-child {
    border-bottom: none;
}

.sidebar-menu a {
    display: block;
    padding: 8px 0;
    color: #64748b;
    transition: all 0.3s ease;
}

.sidebar-menu a:hover,
.sidebar-menu a.active {
    color: #047dc3;
}

.sidebar-menu .sub-menu {
    list-style: none;
    padding-left: 15px;
    margin: 0;
}

.sidebar-menu .sub-menu a {
    padding: 4px 0;
    font-size: 14px;
}
</style>

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