<?php
    $cartItems = Cart::instance('shopping')->content();
    $subtotal = (float) str_replace([',', '.00'], '', Cart::instance('shopping')->subtotal());
    $shipping = Session::get('shipping') ? Session::get('shipping') : 0;
    $discount = Session::get('discount') ? Session::get('discount') : 0;
    $grand    = $subtotal + $shipping - $discount;
    $productIds = $cartItems->pluck('id')->unique()->toArray();
    $cartProducts = !empty($productIds) ? \App\Models\Product::whereIn('id', $productIds)->select('id', 'is_digital', 'free_delivery', 'slug')->get()->keyBy('id') : collect();
    $hasAllFreeDelivery = \App\Http\Controllers\Frontend\ShoppingController::hasAllFreeDeliveryProducts();
    $hasDigitalOnly = $cartItems->isNotEmpty();
    if ($hasDigitalOnly) {
        foreach ($cartItems as $item) {
            $p = $cartProducts->get($item->id);
            $isDig = $p ? (bool)($p->is_digital ?? 0) : (bool)($item->options->is_digital ?? 0);
            if (!$isDig) { $hasDigitalOnly = false; break; }
        }
    }
?>

<style>
    .lp-cart-wrapper {
        font-size: 13px;
    }
    .lp-cart-header,
    .lp-cart-row {
        display: grid;
        grid-template-columns: 52px 1.4fr 0.9fr 0.7fr;
        align-items: center;
        column-gap: 8px;
    }
    .lp-cart-header {
        font-weight: 600;
        color: #6b7280;
        border-bottom: 1px solid #e5e7eb;
        padding-bottom: 6px;
        margin-bottom: 4px;
    }
    .lp-cart-row {
        padding: 8px 0;
        border-bottom: 1px dashed #e5e7eb;
    }
    .lp-cart-row:last-child {
        border-bottom: none;
    }
    .lp-cart-remove-btn {
        border: none;
        background: transparent;
        color: #ef4444;
        cursor: pointer;
        padding: 0;
    }
    .lp-cart-qty-box {
        display: inline-flex;
        align-items: center;
        border: 1px solid #e5e7eb;
        border-radius: 999px;
        overflow: hidden;
    }
    .lp-cart-qty-btn {
        width: 22px;
        height: 22px;
        border: none;
        background: #f3f4f6;
        cursor: pointer;
        font-size: 13px;
        font-weight: 600;
    }
    .lp-cart-qty-val {
        min-width: 22px;
        text-align: center;
        padding: 0 4px;
        font-weight: 600;
    }
    .lp-cart-product {
        display: flex;
        align-items: center;
        gap: 6px;
    }
    .lp-cart-product img {
        width: 34px;
        height: 34px;
        border-radius: 6px;
        object-fit: cover;
    }
    .lp-cart-product-name {
        font-weight: 600;
        color: #111827;
        line-height: 1.3;
    }
    .lp-cart-product-meta {
        font-size: 11px;
        color: #6b7280;
    }
    .lp-cart-totals {
        margin-top: 10px;
        border-top: 1px solid #e5e7eb;
        padding-top: 8px;
    }
    .lp-cart-total-row {
        display: flex;
        justify-content: space-between;
        margin-bottom: 4px;
    }
    .lp-cart-total-row.final {
        margin-top: 6px;
        padding-top: 6px;
        border-top: 1px dashed #e5e7eb;
        font-weight: 700;
        color: #111827;
    }
    @media (max-width: 576px) {
        .lp-cart-header,
        .lp-cart-row {
            grid-template-columns: 40px 1.4fr 0.9fr 0.7fr;
        }
    }
</style>

<?php
    $cartItemsForIncomplete = [];
    foreach ($cartItems as $item) {
        $p = $cartProducts->get($item->id);
        $cartItemsForIncomplete[] = [
            'id' => $item->id,
            'name' => $item->name,
            'qty' => $item->qty,
            'price' => (float) $item->price,
            'image' => asset($item->options->image ?? ''),
            'link' => isset($item->options->slug) ? url('/product/' . $item->options->slug) : '#',
            'is_digital' => (int) ($p->is_digital ?? $item->options->is_digital ?? 0),
            'free_delivery' => (int) ($p->free_delivery ?? 0),
        ];
    }
?>
<div class="lp-cart-wrapper" data-cart-items="<?php echo e(json_encode($cartItemsForIncomplete)); ?>" data-subtotal="<?php echo e($subtotal); ?>" data-grand="<?php echo e($grand); ?>" data-has-all-free-delivery="<?php echo e($hasAllFreeDelivery ? '1' : '0'); ?>" data-has-digital-only="<?php echo e($hasDigitalOnly ? '1' : '0'); ?>" data-cart-empty="<?php echo e($cartItems->isEmpty() ? '1' : '0'); ?>">
    <div class="lp-cart-header">
        <div class="text-center">ডিলিট</div>
        <div>প্রোডাক্ট</div>
        <div class="text-center">পরিমাণ</div>
        <div class="text-end">মূল্য</div>
    </div>

    <?php $__empty_1 = true; $__currentLoopData = Cart::instance('shopping')->content(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
        <div class="lp-cart-row">
            <div class="text-center">
                <button type="button" class="lp-cart-remove-btn cart_remove" data-id="<?php echo e($value->rowId); ?>">
                    <i class="fas fa-trash-alt"></i>
                </button>
            </div>
            <div>
                <a href="<?php echo e(route('product', $value->options->slug)); ?>" class="lp-cart-product" style="text-decoration:none;">
                    <img src="<?php echo e(asset($value->options->image)); ?>" alt="<?php echo e($value->name); ?>">
                    <div>
                        <div class="lp-cart-product-name"><?php echo e(\Illuminate\Support\Str::limit($value->name, 40)); ?></div>
                        <div class="lp-cart-product-meta">
                            <?php if($value->options->product_size): ?> Size: <?php echo e($value->options->product_size); ?> <?php endif; ?>
                            <?php if($value->options->product_color): ?> <?php if($value->options->product_size): ?> | <?php endif; ?> Color: <?php echo e($value->options->product_color); ?> <?php endif; ?>
                        </div>
                    </div>
                </a>
            </div>
            <div class="text-center">
                <div class="lp-cart-qty-box">
                    <button type="button" class="lp-cart-qty-btn cart_decrement" data-id="<?php echo e($value->rowId); ?>">-</button>
                    <span class="lp-cart-qty-val"><?php echo e($value->qty); ?></span>
                    <button type="button" class="lp-cart-qty-btn cart_increment" data-id="<?php echo e($value->rowId); ?>">+</button>
                </div>
            </div>
            <div class="text-end fw-semibold">
                ৳ <?php echo e($value->price * $value->qty); ?>

            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
        <div class="py-3 text-center" style="color:#6b7280;font-size:13px;">
            কার্টে কোনো পণ্য নেই।
        </div>
    <?php endif; ?>

    <div class="lp-cart-totals">
        <div class="lp-cart-total-row">
            <span>মোট</span>
            <span id="net_total">৳ <strong><?php echo e($subtotal); ?></strong></span>
        </div>
        <div class="lp-cart-total-row">
            <span>ডেলিভারি চার্জ</span>
            <span id="cart_shipping_cost">৳ <strong><?php echo e($shipping); ?></strong></span>
        </div>
        <?php if($discount > 0): ?>
            <div class="lp-cart-total-row" style="color:#16a34a;">
                <span>কুপন ছাড়</span>
                <span id="discount">- ৳ <strong><?php echo e($discount); ?></strong></span>
            </div>
        <?php endif; ?>
        <div class="lp-cart-total-row final">
            <span>সর্বমোট</span>
            <span id="grand_total">৳ <strong><?php echo e($grand); ?></strong></span>
        </div>
    </div>
</div>

<script src="<?php echo e(asset('public/frontEnd/js/jquery-3.6.3.min.js')); ?>"></script>
<!-- cart js start -->
<script>
    $('.cart_store').on('click',function(){
    var id = $(this).data('id'); 
    var qty = $(this).parent().find('input').val();
    if(id){
        $.ajax({
           type:"GET",
           data:{'id':id,'qty':qty?qty:1},
           url:"<?php echo e(route('cart.store')); ?>",
           success:function(data){               
            if(data){
                return cart_count();
            }
           }
        });
     }  
   });

    $('.cart_remove').on('click',function(){
    var id = $(this).data('id');   
    if(id){
        $.ajax({
           type:"GET",
           data:{'id':id},
           url:"<?php echo e(route('cart.remove')); ?>",
           success:function(data){               
            if(data){
                $(".cartlist").html(data);
                return cart_count();
            }
           }
        });
     }  
   });
   

    $('.cart_increment').on('click',function(){
    var id = $(this).data('id');  
    if(id){
        $.ajax({
           type:"GET",
           data:{'id':id},
           url:"<?php echo e(route('cart.increment')); ?>",
           success:function(data){               
            if(data){
                $(".cartlist").html(data);
                return cart_count();
            }
           }
        });
     }  
   });

    $('.cart_decrement').on('click',function(){
    var id = $(this).data('id');  
    if(id){
        $.ajax({
           type:"GET",
           data:{'id':id},
           url:"<?php echo e(route('cart.decrement')); ?>",
           success:function(data){               
            if(data){
                $(".cartlist").html(data);
                return cart_count();
            }
           }
        });
     }  
   });
    
    function cart_count(){
        $.ajax({
           type:"GET",
           url:"<?php echo e(route('cart.count')); ?>",
           success:function(data){               
            if(data){
                $("#cart-qty").html(data);
            }else{
               $("#cart-qty").empty();
            }
           }
        }); 
   };
   
</script>
<!-- cart js end --><?php /**PATH /home/digitalpixelmart/land.digitalpixelmart.shop/resources/views/frontEnd/layouts/ajax/cart.blade.php ENDPATH**/ ?>