{{-- Content block with sidebar component
     Usage: @include('florial::sections.content-block', ['sectionKey' => 'homepage_content_sidebar'])
--}}
@php
    $ts = themeService()->boot();
    $section = $ts->section($sectionKey ?? '');
    if (!$section) return;

    $settings = $ts->settings($sectionKey);
    $heading = $ts->trans($sectionKey, 'heading');
    $body = $ts->trans($sectionKey, 'body');
    $layout = $settings['layout'] ?? 'full';

    // Sidebar menu
    $sidebarMenuTag = null;
    if ($layout === 'with_sidebar') {
        $menuId = $settings['menu_id'] ?? null;
        if ($menuId) {
            $sidebarMenu = \ViartasCore\Core\Models\Menu::find($menuId);
            $sidebarMenuTag = $sidebarMenu?->tag;
        }
    }

    // Products for this section
    $categoryId = $settings['category_id'] ?? null;
    $itemsCount = $settings['items_count'] ?? 12;
    $source = $settings['source'] ?? 'latest';

    $productsQuery = \ViartasShop\Products\Models\Accounting\Product::where('is_visible', true);
    if ($categoryId) {
        $categoryIds = \ViartasShop\ProductCategories\Models\Accounting\ProductCategory::find($categoryId)
            ?->descendantsAndSelf()
            ->pluck('id')
            ->toArray() ?? [$categoryId];

        $productsQuery->whereIn('product_category_id', $categoryIds);
    }

    $productsQuery->orderByDesc('created_at');
    $products = $productsQuery->with(['category', 'variants.attributeValues.attr', 'media'])->limit($itemsCount)->get();
@endphp

<section class="product-section">
    <div class="container-fluid-lg">
        <div class="row g-sm-4 g-3">

            @if($layout === 'with_sidebar' && $sidebarMenuTag)
                {{-- Sidebar --}}
                <div class="col-xxl-3 col-xl-4 d-none d-xl-block">
                    <div class="p-sticky">
                        {!! menu($sidebarMenuTag) !!}
                    </div>
                </div>
                <div class="col-xxl-9 col-xl-8">
                    @else
                        <div class="col-12">
                            @endif

                            @if($heading)
                                <div class="title title-flex mb-1">
                                    <div>
                                        <h2>{{ $heading }}</h2>
                                        <span class="title-leaf">
                            <svg class="icon-width">
                                <use xlink:href="{{ theme()->publicPath('svg/leaf.svg') }}#leaf"></use>
                            </svg>
                        </span>
                                        @if($body)
                                            <p>{{ strip_tags($body) }}</p>
                                        @endif
                                    </div>
                                </div>
                            @endif

                            @if($products->isNotEmpty())
                                <div class="section-b-space-1">
                                    <div class="row g-sm-4 g-3 mt-1 mb-4 row-cols-xxl-4 row-cols-xl-4 row-cols-lg-4 row-cols-md-4 row-cols-2 product-list-section">
                                        @foreach($products as $product)
                                            <div>
                                                <div class="product-box-3 h-100 wow fadeInUp">
                                                    <div class="product-header">
                                                        <div class="product-image">
                                                            <a href="{{ $product->getRoute() ?? '#' }}">
                                                                @if($product->mainImageUrl('card-3x4'))
                                                                    <img src="{{ $product->mainImageUrl('card-3x4') }}"
                                                                         class="img-fluid blur-up lazyload" alt="{{ $product->title }}">
                                                                @else
                                                                    <img src="{{ theme()->publicPath('images/placeholder.png') }}"
                                                                         class="img-fluid blur-up lazyload" alt="{{ $product->title }}">
                                                                @endif
                                                            </a>
                                                        </div>
                                                    </div>

                                                    <div class="product-footer">
                                                        <div class="product-detail">
                                                            @if($product->category)
                                                                <span class="span-name">{{ $product->category->title }}</span>
                                                            @endif
                                                            <a href="{{ $product->getRoute() ?? '#' }}">
                                                                <h5 class="name">{{ $product->title }}</h5>
                                                            </a>

                                                            @if($product->variants->isNotEmpty())
                                                                @php
                                                                    $lengthAttribute = $product->variants->first()
                                                                        ?->attributeValues?->first()?->attr;
                                                                @endphp
                                                                @if($lengthAttribute)
                                                                    <div class="stem-sizes-label mt-3">
                                                                        <i class="fa-solid fa-ruler-vertical me-1"></i>
                                                                        {{ $lengthAttribute->getLocalizedTitle() ?? '' }}
                                                                    </div>
                                                                @endif
                                                                <div class="stem-grid mt-2">
                                                                    @foreach($product->variants as $variant)
                                                                        @php
                                                                            $inStock = \ViartasShop\Warehouses\Models\WarehouseStock::where('sku', $variant->sku)
                                                                                ->where('quantity', '>', 0)->exists();
                                                                        @endphp
                                                                        <div class="stem-option {{ $inStock ? '' : 'stem-option--unavailable' }}"
                                                                             @if($inStock) onclick="selectStem(this)" @endif
                                                                             data-price="{{ $variant->price->formatAsPrice() }}">
                                                                            <div class="stem-option__height">{{ $variant->name }}</div>
                                                                            <div class="stem-option__price">{{ $variant->price->formatAsPrice() }}</div>
                                                                            @unless($inStock)
                                                                                <span class="stem-option__nera">{{ __('globals.out_stock') }}</span>
                                                                            @endunless
                                                                        </div>
                                                                    @endforeach
                                                                </div>
                                                            @else
                                                                <h5 class="price mt-3">
                                                                    <span class="theme-color">{{ $product->price->formatAsPrice() }}</span>
                                                                </h5>
                                                            @endif

                                                            <div class="add-to-cart-box mt-3">
                                                                <button class="btn btn-add-cart addcart-button">
                                                                    <i class="fa-solid fa-basket-shopping"></i>
                                                                    {{ __('globals.add_to_cart') }}
                                                                </button>
                                                            </div>

                                                                <div class="stem-note text-center mt-2">
                                                                    Kaina už 1 vnt. · surisime gražią puokštę!
                                                                </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>
                                </div>
                            @endif

                        </div>
                </div>
        </div>
</section>
