<!-- Category Filter Sidebar -->
<div class="category-filter-sidebar">
    <div class="category-filter-header">
        <h5 class="mb-0">
            <i class="bi bi-filter"></i> Filter by Category
        </h5>
        <button class="btn-close-sidebar d-md-none" type="button" onclick="toggleSidebar()">
            <i class="bi bi-x-lg"></i>
        </button>
    </div>

    <form id="categoryFilterForm" method="GET" action="<?php echo e($formAction); ?>">
        <!-- Preserve search query if exists -->
        <?php if(!empty(request('search'))): ?>
            <input type="hidden" name="search" value="<?php echo e(request('search')); ?>">
        <?php endif; ?>

        <!-- Preserve other query parameters -->
        <?php $__currentLoopData = request()->except(['categories', 'page', 'search']); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <input type="hidden" name="<?php echo e($key); ?>" value="<?php echo e($value); ?>">
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

        <div class="category-filter-content">
            <?php if($categoriesWithCounts->count() > 0): ?>
                <?php $__currentLoopData = $categoriesWithCounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php
                        $hasSubcategories = $category->children && $category->children->count() > 0;
                        $isExpanded = false;
                        // Check if any subcategory or this category is selected
                        if (!empty($selectedCategoryIds)) {
                            $isExpanded = in_array($category->id, $selectedCategoryIds) || 
                                         $category->children->pluck('id')->intersect($selectedCategoryIds)->count() > 0;
                        }
                    ?>
                    
                    <div class="category-item">
                        <div class="category-checkbox-wrapper">
                            <div class="form-check">
                                <input class="form-check-input category-checkbox" 
                                       type="checkbox" 
                                       name="categories[]" 
                                       value="<?php echo e($category->id); ?>" 
                                       id="cat_<?php echo e($category->id); ?>"
                                       <?php echo e(in_array($category->id, $selectedCategoryIds ?? []) ? 'checked' : ''); ?>

                                       onchange="submitFilterForm()">
                                <label class="form-check-label" for="cat_<?php echo e($category->id); ?>">
                                    <span class="category-name"><?php echo e($category->name); ?></span>
                                    <span class="category-count">(<?php echo e($category->product_count ?? 0); ?>)</span>
                                </label>
                            </div>
                            <?php if($hasSubcategories): ?>
                                <button type="button" 
                                        class="btn-toggle-subcategories" 
                                        onclick="toggleSubcategories(<?php echo e($category->id); ?>)"
                                        aria-expanded="<?php echo e($isExpanded ? 'true' : 'false'); ?>">
                                    <i class="bi bi-chevron-<?php echo e($isExpanded ? 'down' : 'right'); ?>"></i>
                                </button>
                            <?php endif; ?>
                        </div>

                        <?php if($hasSubcategories): ?>
                            <div class="subcategories-list <?php echo e($isExpanded ? 'expanded' : ''); ?>" id="subcategories_<?php echo e($category->id); ?>">
                                <?php $__currentLoopData = $category->children; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $subcategory): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div class="subcategory-item">
                                        <div class="form-check">
                                            <input class="form-check-input subcategory-checkbox" 
                                                   type="checkbox" 
                                                   name="categories[]" 
                                                   value="<?php echo e($subcategory->id); ?>" 
                                                   id="subcat_<?php echo e($subcategory->id); ?>"
                                                   <?php echo e(in_array($subcategory->id, $selectedCategoryIds ?? []) ? 'checked' : ''); ?>

                                                   onchange="submitFilterForm()">
                                            <label class="form-check-label" for="subcat_<?php echo e($subcategory->id); ?>">
                                                <span class="category-name"><?php echo e($subcategory->name); ?></span>
                                                <span class="category-count">(<?php echo e($subcategory->product_count ?? 0); ?>)</span>
                                            </label>
                                        </div>
                                    </div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <?php else: ?>
                <p class="text-muted text-center py-3">No categories available</p>
            <?php endif; ?>
        </div>

        <?php if(!empty($selectedCategoryIds)): ?>
            <div class="category-filter-footer">
                <button type="button" class="btn btn-sm btn-outline-secondary w-100" onclick="clearFilters()">
                    <i class="bi bi-x-circle"></i> Clear Filters
                </button>
            </div>
        <?php endif; ?>
    </form>
</div>

<!-- Mobile Sidebar Toggle Button -->
<div class="sidebar-toggle-btn d-md-none">
    <button class="btn btn-primary" type="button" onclick="toggleSidebar()">
        <i class="bi bi-funnel"></i> Filter
    </button>
</div>

<!-- Sidebar Overlay for Mobile -->
<div class="sidebar-overlay d-md-none" id="sidebarOverlay" onclick="toggleSidebar()"></div>

<?php /**PATH /var/www/html/shaa_jewels/live/resources/views/partials/category-filter-sidebar.blade.php ENDPATH**/ ?>