<?php $__env->startSection('title', 'سبد خرید'); ?>

<?php $__env->startSection('content'); ?>

    <!-- breadcrumb -->
    <div class="bread-crumb pt-3">
        <div class="container-fluid">
            <div class="content-box">
                <div class="container-fluid">
                    <nav aria-label="breadcrumb" class="my-lg-0 my-2">
                        <ol class="breadcrumb mb-0">
                            <li class="breadcrumb-item"><a href="<?php echo e(route('home')); ?>" class="font-14 text-muted">خانه</a></li>
                            <li class="breadcrumb-item active main-color-one-color font-14" aria-current="page">سبد خرید</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
    <!-- end breadcrumb -->

    <div class="content py-20">
        <div class="container-fluid">

            <!-- step indicator -->
            <div class="line-step-container d-sm-block d-none mb-4">
                <div class="line-step">
                    <div class="line-step-boxs">
                        <div class="line-step-box complete">
                            <a href="<?php echo e(route('cart.index')); ?>">
                                <p>سبد خرید</p>
                                <div class="icon">1</div>
                            </a>
                        </div>
                        <div class="line-step-box disabled">
                            <a href="#">
                                <p>جزییات پرداخت</p>
                                <div class="icon">2</div>
                            </a>
                        </div>
                        <div class="line-step-box disabled">
                            <a href="#">
                                <p>تکمیل سفارش</p>
                                <div class="icon">3</div>
                            </a>
                        </div>
                    </div>
                </div>
            </div>

            <?php if(empty($items)): ?>

                <!-- empty cart -->
                <div class="content-box py-5 text-center">
                    <div class="container-fluid">
                        <i class="bi bi-cart-x" style="font-size: 4rem; color: #ccc;"></i>
                        <h5 class="mt-3 mb-2">سبد خرید شما خالی است</h5>
                        <p class="text-muted mb-4">هنوز محصولی به سبد خرید اضافه نکرده‌اید.</p>
                        <a href="<?php echo e(route('home')); ?>" class="btn border-0 main-color-one-bg rounded-3 px-4">
                            <i class="bi bi-arrow-right me-1 text-white"></i>
                            <span class="text-white">ادامه خرید</span>
                        </a>
                    </div>
                </div>

            <?php else: ?>

                <?php
                    $subtotal = 0;
                    $discount = 0;
                    $total    = 0;
                    foreach ($items as $item) {
                        $price    = (float)($item['variant']->price ?? 0);
                        $final    = (float)($item['variant']->discount_price ?? $price);
                        $qty      = (int)$item['qty'];
                        $subtotal += $price  * $qty;
                        $discount += ($price - $final) * $qty;
                        $total    += $final  * $qty;
                    }
                ?>

                <div class="cart-product">
                    <div class="row gy-4">

                        <!-- cart items -->
                        <div class="col-lg-9">
                            <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                                $variant = $item['variant'];
                                $product = $item['product'];
                                $qty     = $item['qty'];
                                $key     = $item['key'];
                                $price   = (float)($variant->price ?? 0);
                                $final   = (float)($variant->discount_price ?? $price);
                                $imgSrc  = $product->featured_image
                                    ? asset('uploads/products/' . $product->featured_image)
                                    : asset('theme/assets/image/product/laptop-1.jpg');
                            ?>
                            <div class="cart-product-item" data-key="<?php echo e($key); ?>" id="cart-item-<?php echo e($key); ?>">
                                <div class="content-box">
                                    <div class="container-fluid">
                                        <div class="cart-items">
                                            <div class="item">
                                                <div class="row gy-2 align-items-start">
                                                    <div class="col-2 w-100-in-400">
                                                        <a href="<?php echo e(route('product.show', $product->slug)); ?>">
                                                            <div class="image">
                                                                <img src="<?php echo e($imgSrc); ?>" alt="<?php echo e($product->title); ?>" class="img-fluid rounded-3">
                                                            </div>
                                                        </a>
                                                    </div>
                                                    <div class="col-10 w-100-in-400">
                                                        <div class="d-flex justify-content-between align-items-start">
                                                            <div class="d-flex align-items-start flex-column me-2">
                                                                <div class="title d-flex align-items-center flex-wrap">
                                                                    <a href="<?php echo e(route('product.show', $product->slug)); ?>">
                                                                        <h6 class="font-15 mb-0"><?php echo e($product->title); ?>

                                                                            <?php if($variant->discount_price): ?>
                                                                            <span class="badge ms-2 danger-label rounded-pill">
                                                                                <?php echo e(round((1 - $variant->discount_price / $variant->price) * 100)); ?>%
                                                                            </span>
                                                                            <?php endif; ?>
                                                                            <?php if($variant->stock <= 0): ?>
                                                                            <span class="badge bg-danger ms-2 rounded-pill">ناموجود</span>
                                                                            <?php endif; ?>
                                                                        </h6>
                                                                    </a>
                                                                </div>
                                                                <div class="cart-item-feature d-flex align-items-center flex-wrap mt-3">
                                                                    <?php if($variant->name): ?>
                                                                    <div class="item d-flex align-items-center">
                                                                        <div class="icon"><i class="bi bi-tag"></i></div>
                                                                        <div class="saller-name mx-2">ویژگی:</div>
                                                                        <div class="saller-name text-muted"><?php echo e($variant->name); ?></div>
                                                                    </div>
                                                                    <?php endif; ?>
                                                                    <?php if($variant->color): ?>
                                                                    <div class="item d-flex align-items-center">
                                                                        <div class="icon"><i class="bi bi-palette2"></i></div>
                                                                        <div class="saller-name mx-2">رنگ:</div>
                                                                        <div class="saller-name text-muted">
                                                                            <div class="product-meta-color-items mt-0">
                                                                                <label class="btn-light mb-0 px-2 py-1">
                                                                                    <span style="background-color: <?php echo e($variant->color); ?>;"></span>
                                                                                </label>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                    <?php endif; ?>
                                                                </div>
                                                            </div>
                                                            <div class="remove danger-label">
                                                                <a href="#" class="btn-cart-remove" data-key="<?php echo e($key); ?>">
                                                                    <i class="bi bi-trash-fill font-22"></i>
                                                                </a>
                                                            </div>
                                                        </div>
                                                        <div class="action d-flex flex-wrap justify-content-sm-end justify-content-center align-items-center mt-3 gap-3">
                                                            <div class="cart-item-feature d-flex align-items-center flex-wrap">
                                                                <?php if($variant->discount_price): ?>
                                                                <div class="item d-flex align-items-center me-2">
                                                                    <p class="mb-0 old-price font-15"><?php echo e(number_format($variant->price)); ?></p>
                                                                </div>
                                                                <?php endif; ?>
                                                                <div class="item d-flex align-items-center">
                                                                    <p class="mb-0 new-price font-15"><?php echo e(number_format($final)); ?> تومان</p>
                                                                </div>
                                                            </div>
                                                            <div class="counter">
                                                                <input type="text" name="count" class="counter cart-qty-input"
                                                                    data-key="<?php echo e($key); ?>"
                                                                    data-stock="<?php echo e($item['variant']->stock ?? 0); ?>"
                                                                    value="<?php echo e($qty); ?>"
                                                                    <?php echo e(($item['variant']->stock ?? 0) <= 0 ? 'disabled' : ''); ?>>
                                                            </div>
                                                            <div id="cart-err-<?php echo e($key); ?>" class="d-none text-danger small mt-1"></div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>

                        <!-- order summary sidebar -->
                        <div class="col-lg-3">
                            <div class="cart-canvases position-sticky top-0" id="cart-sidebar">
                                <div class="item">
                                    <div class="factor">
                                        <div class="title">
                                            <div class="d-flex align-items-center">
                                                <img src="<?php echo e(asset('theme/assets/image/shopping-bag.png')); ?>" class="img-fluid" alt="">
                                                <h6 class="font-16 mb-0">سفارش شما</h6>
                                            </div>
                                        </div>
                                        <div class="d-flex mb-3 align-items-center justify-content-between">
                                            <p class="fw-bold mb-0 font-14">محصول</p>
                                            <p class="fw-bold mb-0 font-14">قیمت کل</p>
                                        </div>

                                        <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php
                                            $fp       = (float)($item['variant']->discount_price ?? $item['variant']->price ?? 0);
                                            $origP    = (float)($item['variant']->price ?? $fp);
                                        ?>
                                        <div class="factor-item p-2 rounded-3 shadow-sm bg-light d-flex align-items-center justify-content-between"
                                             id="sidebar-item-<?php echo e($item['key']); ?>">
                                            <p class="mb-0 font-13 text-overflow-1" style="max-width:60%">
                                                <span class="fw-bold ms-1 sidebar-item-qty" id="sidebar-qty-<?php echo e($item['key']); ?>">x<?php echo e($item['qty']); ?></span>
                                                <?php echo e($item['product']->title); ?>

                                            </p>
                                            <p class="mb-0 font-13 sidebar-item-total"
                                               data-unit="<?php echo e($fp); ?>"
                                               data-original="<?php echo e($origP); ?>"
                                               data-qty="<?php echo e($item['qty']); ?>">
                                                <?php echo e(number_format($fp * $item['qty'])); ?> تومان
                                            </p>
                                        </div>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                        <?php if($discount > 0): ?>
                                        <div class="factor-item p-2 rounded-3 shadow-sm bg-light d-flex align-items-center justify-content-between" id="sidebar-discount-row">
                                            <p class="mb-0 fw-bold font-13">تخفیف:</p>
                                            <p class="mb-0 font-13 danger-label" id="sidebar-discount"><?php echo e(number_format($discount)); ?> تومان</p>
                                        </div>
                                        <?php endif; ?>

                                        <div class="factor-item p-2 rounded-3 shadow-sm bg-light d-flex align-items-center justify-content-between">
                                            <p class="mb-0 fw-bold font-13">جمع کل:</p>
                                            <p class="mb-0 fw-bold font-14 main-color-one-color" id="sidebar-total"><?php echo e(number_format($total)); ?> تومان</p>
                                        </div>

                                        <div class="action mt-3 d-flex align-items-center justify-content-center">
                                            <a href="<?php echo e(route('checkout.index')); ?>" class="btn border-0 main-color-one-bg rounded-3 d-block w-100 text-white text-center">
                                                تسویه حساب
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

            <?php endif; ?>
        </div>
    </div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
$(function () {
    var csrfToken = '<?php echo e(csrf_token()); ?>';
    var updateUrl = '<?php echo e(url("cart/update")); ?>';
    var removeUrl = '<?php echo e(url("cart/remove")); ?>';

    function numFmt(n) {
        return Math.round(n).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    }

    function recalcTotals() {
        var total    = 0;
        var discount = 0;
        $('.sidebar-item-total').each(function () {
            var unit     = parseFloat($(this).data('unit'))     || 0;
            var original = parseFloat($(this).data('original')) || unit;
            var qty      = parseInt($(this).data('qty'))        || 1;
            total    += unit * qty;
            discount += (original - unit) * qty;
        });
        $('#sidebar-total').text(numFmt(total) + ' تومان');
        if (discount > 0) {
            $('#sidebar-discount').text(numFmt(discount) + ' تومان');
            $('#sidebar-discount-row').show();
        } else {
            $('#sidebar-discount-row').hide();
        }
    }

    // update quantity on touchspin change
    $(document).on('change', '.cart-qty-input', function () {
        var $input  = $(this);
        var key     = $input.data('key');
        var qty     = parseInt($input.val()) || 1;
        var prevQty = parseInt($input.data('prev-qty')) || qty;
        $input.data('prev-qty', qty);

        // sidebar qty label (2x, 3x …)
        $('#sidebar-qty-' + key).text('x' + qty);

        // sidebar item total
        var $totalEl = $('#sidebar-item-' + key + ' .sidebar-item-total');
        $totalEl.data('qty', qty);
        $totalEl.text(numFmt(parseFloat($totalEl.data('unit')) * qty) + ' تومان');

        recalcTotals();
        $('#cart-err-' + key).addClass('d-none').text('');

        $.post(updateUrl + '/' + key, { _token: csrfToken, qty: qty })
            .fail(function (xhr) {
                var msg = (xhr.responseJSON && xhr.responseJSON.message)
                    ? xhr.responseJSON.message
                    : 'خطایی رخ داد.';
                $input.val(prevQty);
                $input.data('prev-qty', prevQty);
                $('#sidebar-qty-' + key).text('x' + prevQty);
                $totalEl.data('qty', prevQty);
                $totalEl.text(numFmt(parseFloat($totalEl.data('unit')) * prevQty) + ' تومان');
                recalcTotals();
                $('#cart-err-' + key).removeClass('d-none').text(msg);
            });
    });

    // remove item
    $(document).on('click', '.btn-cart-remove', function (e) {
        e.preventDefault();
        var key = $(this).data('key');
        var $row = $('#cart-item-' + key);
        var $sidebarItem = $('#sidebar-item-' + key);

        $.post(removeUrl + '/' + key, { _token: csrfToken }, function () {
            $row.fadeOut(300, function () {
                $(this).remove();
                $sidebarItem.remove();
                recalcTotals();
                if ($('.cart-product-item').length === 0) {
                    location.reload();
                }
            });
        });
    });
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('site.layouts.site-layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/louishop/public_html/engine/resources/views/site/cart.blade.php ENDPATH**/ ?>