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

<?php echo $__env->make('frontend.layout.header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<style>

body{
    background:#f8fafc;
}

/* PAGE */

.cart-page{

    padding:50px 0;

}

/* TITLE */

.cart-title{

    font-size:38px;
    font-weight:900;
    color:#111827;
    margin-bottom:35px;

}

.cart-title span{

    color:#c59d5f;

}

/* TABLE */

.cart-box{

    background:#fff;
    border-radius:24px;
    padding:25px;
    box-shadow:0 10px 30px rgba(0,0,0,0.06);

}

.table{

    vertical-align:middle;

}

.table thead tr{

    background:#111827;
    color:#fff;

}

.table thead th{

    padding:18px !important;
    border:none !important;
    font-size:14px;
    letter-spacing:1px;
    text-transform:uppercase;

}

.table tbody td{

    padding:20px 15px;
    border-color:#f1f5f9;

}

/* IMAGE */

.cart-image{

    width:85px;
    height:85px;
    border-radius:16px;
    object-fit:cover;
    border:1px solid #eee;

}

/* PRODUCT */

.product-title{

    font-size:18px;
    font-weight:800;
    color:#111827;

}

.product-type{

    color:#6b7280;
    margin-top:6px;
    font-size:14px;

}

/* PRICE */

.cart-price{

    font-size:20px;
    font-weight:500;
    color:#c59d5f;

}

/* QTY */

.qty-box{

    width:80px;
    height:45px;
    border-radius:12px;
    border:1px solid #d1d5db;
    text-align:center;

}

/* CHECKBOX */

.cart-check{

    width:22px;
    height:22px;
    cursor:pointer;

}

/* BUTTONS */

.remove-btn{

    background:#ef4444;
    border:none;
    color:#fff;
    padding:10px 16px;
    border-radius:12px;
    font-weight:700;

}

.order-btn{

    background:linear-gradient(135deg,#111827,#1f2937);
    border:none;
    color:#fff;
    padding:16px 34px;
    border-radius:16px;
    font-size:16px;
    font-weight:800;
    box-shadow:0 10px 25px rgba(0,0,0,0.15);

}

.order-btn:hover{

    opacity:0.95;

}

/* SUMMARY */

.summary-box{

    background:#111827;
    color:#fff;
    padding:30px;
    border-radius:24px;
    height:100%;

}

.summary-title{

    font-size:26px;
    font-weight:900;
    margin-bottom:25px;

}

.summary-row{

    display:flex;
    justify-content:space-between;
    margin-bottom:18px;
    font-size:16px;

}

.summary-total{

    border-top:1px solid rgba(255,255,255,0.1);
    padding-top:18px;
    margin-top:18px;
    font-size:24px;
    font-weight:900;
    color:#fbbf24;

}

/* EMPTY */

.empty-cart{

    background:#fff;
    padding:80px 20px;
    border-radius:24px;
    text-align:center;
    box-shadow:0 10px 30px rgba(0,0,0,0.06);

}

.empty-cart h3{

    font-size:34px;
    font-weight:900;
    color:#111827;
    margin-top:20px;

}

.empty-cart p{

    color:#6b7280;
    margin-top:10px;

}

.custom-success-msg{
    position:fixed;
    top:20px;
    right:20px;
    background:#16a34a;
    color:#fff;
    padding:14px 25px;
    border-radius:10px;
    z-index:99999;
    font-weight:600;
    box-shadow:0 5px 20px rgba(0,0,0,0.2);
}

.custom-error-msg{
    position:fixed;
    top:20px;
    right:20px;
    background:#dc2626;
    color:#fff;
    padding:14px 25px;
    border-radius:10px;
    z-index:99999;
    font-weight:600;
    box-shadow:0 5px 20px rgba(0,0,0,0.2);
}

</style>

<div class="container cart-page">

    <div class="cart-title">

        Shopping <span>Cart</span>

    </div>

   
    <?php if($carts->count()): ?>

    <div class="row g-4">

        

        <div class="col-lg-8">

            <div class="cart-box">

                <table class="table">

                    <thead>

                        <tr>

                            <th width="60">
                                Select
                            </th>

                            <th>
                                Product
                            </th>

                            <th>
                                Price
                            </th>

                            <th width="120">
                                Qty
                            </th>

                            <th width="120">
                                Action
                            </th>

                        </tr>

                    </thead>

                    <tbody>

                        
                        <?php $__currentLoopData = $carts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cart): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>

                            <td>

                                <input
                                    type="checkbox"
                                    class="cart-check product-check"
                                    data-price="<?php echo e($cart->total_price ?? 0); ?>"
                                    data-id="<?php echo e($cart->id); ?>"  value="<?php echo e($cart->id); ?>">

                                    

                            </td>

                            <td>

                                <div class="d-flex align-items-center gap-3">

                                    <img
                                        src="<?php echo e(asset($cart->product->image ?? 'public/img/no_image.jpg')); ?>"
                                        class="cart-image">

                                    <div>

                                        <div class="product-title">

                                            <?php echo e($cart->product->collection); ?>


                                        </div>

                                        <div class="product-category">

                                            <?php echo e($cart->product->new_triple_sku); ?>

                                        

                                        </div>

                                        <div class="product-type">

                                            <?php echo e($cart->product->product_type); ?>


                                        </div>

                                    </div>

                                </div>

                            </td>

                            <td>

                                <div class="cart-price">

                                    $<?php echo e(number_format($cart->total_price ?? 0)); ?>


                                </div>

                            </td>

                            <td>
<!-- 
                                <input
                                    type="number"
                                    value="<?php echo e($cart->qty); ?>"
                                    min="1"
                                    class="form-control qty-box"> -->

                                     <input
                                            type="number"
                                            value="<?php echo e($cart->qty); ?>"
                                            min="1"
                                            class="form-control qty-box"
                                            data-id="<?php echo e($cart->id); ?>"
                                            style="width:90px;">

                            </td>

                            <td>

                                <button
                                    class="remove-btn removeCartBtn"  data-id="<?php echo e($cart->id); ?>">

                                    Remove

                                </button>

                            </td>

                        </tr>

                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    </tbody>

                </table>

            </div>

        </div>

        

        <div class="col-lg-4">

            <div class="summary-box">

                <div class="summary-title">

                    Order Summary

                </div>

                <div class="summary-row">

                    <span>
                        Selected Items
                    </span>

                    <span id="totalItems">
                        0
                    </span>

                </div>

                <div class="summary-row">

                    <span>
                        Estimated Total
                    </span>

                    <span>

                        $<span id="totalPrice">
                            0
                        </span>

                    </span>

                </div>

                <div class="summary-total">

                    Total :
                    $<span id="finalTotal">
                        0
                    </span>

                </div>

                <div id="selectedProducts"></div>
                   <button
                        class="btn btn-dark orderBtn"
                        data-bs-toggle="modal"
                        data-bs-target="#orderModal">

                        Place Order

                    </button>

            </div>

        </div>

    </div>

    <?php else: ?>

    <div class="empty-cart">

        <img
            src="https://cdn-icons-png.flaticon.com/512/2038/2038854.png"
            width="120">

        <h3>
            Your Cart Is Empty
        </h3>

        <p>

            Add products to your cart and place order.

        </p>

    </div>

    <?php endif; ?>

</div>

<!-- ORDER MODAL -->

<div class="modal fade"
id="orderModal"
tabindex="-1">

    <div class="modal-dialog modal-dialog-centered">

        <div class="modal-content border-0 rounded-4">

            <div class="modal-header">

                <h5 class="modal-title">
                    Place Order
                </h5>

                <button
                    type="button"
                    class="btn-close"
                    data-bs-dismiss="modal">
                </button>

            </div>

            <form id="orderForm" action="javascript:void(0);">

                <?php echo csrf_field(); ?>

                <div class="modal-body">

                    <input type="hidden"name="product_id" id="product_id">

                    <div class="mb-3">

                        <label>Name</label>

                        <input type="text"
                        name="name"
                        class="form-control"
                        required>

                    </div>

                    <div class="mb-3">

                        <label>Email</label>

                        <input type="email"
                        name="email"
                        class="form-control" value=" <?php echo e(Auth::user()->email); ?>"
                        required>

                    </div>

                    <div class="mb-3">

                        <label>Message</label>

                        <textarea
                        name="message"
                        class="form-control"
                        rows="4"></textarea>

                    </div>

                </div>

                <div class="modal-footer">

                    <button
                    type="submit"
                    class="btn btn-dark w-100">

                        Send Order

                    </button>

                </div>

            </form>

        </div>

    </div>

</div>

<?php echo $__env->make('frontend.layout.footer', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<script>

$(document).on('click','.removeCartBtn',function(){

    let id = $(this).data('id');

    let button = $(this);

    $.ajax({

        url: "<?php echo e(url('/remove-cart')); ?>/" + id,

        type:'GET',

        success:function(response){

            // REMOVE ROW
            button.closest('tr').remove();

            // UPDATE HEADER COUNT
            if(response.cartCount > 0){

                $('#cartCount')
                    .show()
                    .text(response.cartCount);

            }else{

                $('#cartCount').hide();

            }

            // OPTIONAL TOTAL UPDATE
            $('#grandTotal').text(response.grandTotal);

        }

    });

});


function calculateTotal()
{

    let total = 0;

    let count = 0;

    $('#selectedProducts').html('');

    $('.product-check:checked').each(function(){

        total += parseFloat($(this).data('price'));

        count++;

        $('#selectedProducts').append(`

            <input
                type="hidden"
                name="cart_ids[]"
                value="${$(this).data('id')}">

        `);

    });

    $('#totalItems').text(count);
$('#totalPrice').text(Math.round(total));

$('#finalTotal').text(Math.round(total));
}

$(document).on(
    'change',
    '.product-check',
    function(){

        calculateTotal();

});



// PRODUCT ID SET

$(document).on('click','.orderBtn',function(){

    let selectedProducts = [];

    $('.product-check:checked').each(function(){

        selectedProducts.push($(this).val());

    });

    // SET IDS IN HIDDEN INPUT
    $('#product_id').val(
        selectedProducts.join(',')
    );

});

// ORDER SUBMIT
$('#orderForm').submit(function(e){

    e.preventDefault();

    $.ajax({

        url:"<?php echo e(route('place.order')); ?>",

        type:"POST",

        data:$(this).serialize(),

        beforeSend:function(){

            $('.btn-dark').html('Sending...');

        },

        success:function(response){

            if(response.status == true){

                $('#orderModal').modal('hide');

                $('#orderForm')[0].reset();

                $('body').append(`

                    <div class="custom-success-msg">

                        Order Sent Successfully

                    </div>

                `);

                setTimeout(function(){

                    $('.custom-success-msg').fadeOut();

                },  location.reload(),3000);

            }

            $('.btn-dark').html('Send Order');

        },

        error:function(){

            $('body').append(`

                <div class="custom-error-msg">

                    Something Went Wrong

                </div>

            `);

            setTimeout(function(){

                $('.custom-error-msg').fadeOut();

            },3000);

            $('.btn-dark').html('Send Order');

        }

    });

});



    $(document).on('change','.qty-box',function(){

    let qty = $(this).val();

    let cart_id = $(this).data('id');

    $.ajax({

        url:"<?php echo e(route('cart.qty.update')); ?>",

        type:"POST",

        data:{

            _token:"<?php echo e(csrf_token()); ?>",

            cart_id:cart_id,

            qty:qty

        },

        success:function(response){

            location.reload();

        }

    });

});


</script>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('frontend.layout.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/braintreeeasysof/public_html/popular/resources/views/frontend/cart/cart.blade.php ENDPATH**/ ?>