<?php
    /** @var \App\Models\HomeSection $section */
    $menu = $section->menu;
?>

<?php if($menu): ?>
    <?php
        // প্রতি সেকশনের প্রডাক্ট limit (DB column: max_products)
        $limit = $section->max_products ?? 8;

        // ওই main category (menu) থেকে latest পণ্য
        $products = \App\Models\Product::where('category_id', $menu->id)
            ->latest()
            ->take($limit)
            ->get();
    ?>

    <?php if($products->isNotEmpty()): ?>
        <section class="home-block">
            <div class="container">
                <div class="home-block-header d-flex justify-content-between align-items-center flex-wrap gap-2">
                    <div>
                        
                        <h2 class="home-block-title">
                            <?php echo e($section->title ?: $menu->name); ?>

                        </h2>

                        
                        <?php if($section->subtitle): ?>
                            <p class="home-block-subtitle">
                                <?php echo e($section->subtitle); ?>

                            </p>
                        <?php endif; ?>

                        
                        <?php if($menu->submenus && $menu->submenus->count()): ?>
                            <div class="home-block-submenus">
                                <?php $__currentLoopData = $menu->submenus; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $submenu): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <a href="<?php echo e(route('menu.products', ['name' => $submenu->name, 'slug' => $submenu->slug])); ?>"
                                       class="home-submenu-link">
                                        <?php echo e($submenu->name); ?>

                                    </a>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>
                        <?php endif; ?>
                    </div>

                    
                    <div class="mt-2 mt-md-0">
                        <a href="<?php echo e(route('main-menu-products', $menu->slug)); ?>"
                           class="btn btn-sm btn-outline-primary home-block-more">
                            সব দেখুন
                        </a>
                    </div>
                </div>

                
                <div class="row g-3 g-md-4">
                    <?php $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $hasDiscount = !is_null($product->discount_price) && $product->discount_price > 0;
                            $basePrice   = $product->price ?: 0;
                            $finalPrice  = $hasDiscount ? $product->discount_price : $basePrice;
                            $discountPercent = $hasDiscount && $basePrice > 0
                                ? round((($basePrice - $product->discount_price) / $basePrice) * 100)
                                : null;
                        ?>

                        <div class="col-6 col-md-3 col-lg-2">
                            <a href="<?php echo e(route('details', $product->slug)); ?>" class="home-product-card-link">
                                <div class="home-product-card h-100 d-flex flex-column">
                                    <div class="home-product-img-wrap">
                                        <img src="<?php echo e($product->thumbnail_url); ?>"
                                             alt="<?php echo e($product->alt); ?>"
                                             class="img-fluid">
                                        <?php if($discountPercent): ?>
                                            <span class="home-discount-badge"><?php echo e($discountPercent); ?>% OFF</span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="home-product-body">
                                        <p class="home-product-name mb-1">
                                            <?php echo e(Str::limit($product->name, 40)); ?>

                                        </p>
                                        <div class="home-product-price-wrap">
                                            <span class="home-price-main">
                                                ৳ <?php echo e(number_format($finalPrice, 0)); ?>

                                            </span>
                                            <?php if($hasDiscount): ?>
                                                <span class="home-price-old">
                                                    ৳ <?php echo e(number_format($basePrice, 0)); ?>

                                                </span>
                                            <?php endif; ?>
                                        </div>
                                        <span class="home-price-tag">In Stock</span>
                                    </div>
                                </div>
                            </a>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </div>
            </div>
        </section>
    <?php endif; ?>
<?php endif; ?>
<?php /**PATH /home/emdadtwa/sefcard.com/resources/views/frontend/partials/home-section-block.blade.php ENDPATH**/ ?>