@extends('web.layouts.layout')
@section('content')

    <main>
        <!-- Start section -->
    @include('web.layouts.header')


        <section class="cart_pg section">
            <div class="container">
                <h2 class="sec_title text-uppercase color-primary fw-bold mb-5">
                    {{ __('web.shopping_cart') }}
                    <img src="{{url('')}}/assets/web/images/title_line.svg" class="d-block " alt="">
                </h2>
                <div class="row">
                    <div class="col-lg-9">
                        @if(count($carts) == 0)
                        <div class="alert alert-light py-4 text-center my-5" role="alert">
                            {{__('web.not_exist_product_cart')}}
                        </div>
                        @else
                        <div class="table-responsive">
                            @include('web.pages.components.cart.cart')
                        </div>
                        @endif
                      
                    </div>
                    <div class="col-lg-3 ">
                        <div class="order_summary">
                            <div class="order_summary_inner">
                                <h5 class="mb-4 fw-bold">{{ __('web.payment_details') }}</h5>
                                <div class="item in-flex mb-0">
                                    <span class="label">{{ __('web.total') }}</span>
                                    <h6 class="price"><b class="color-primary" id="order_total"></b> <small>{{$currency->abbr}}</small></h6>
                                </div>

                            </div>
                        </div>
                    </div>
                    <div class="col-lg-9">
                          <div class="in-flex mt-5 flex-wrap" >
                            <a href="{{route('products')}}" class="butn white_butn fw-bold">
                                <i class="fas fa-angle-left me-3"></i> {{ __('web.continue_shopping') }} 
                            </a>

                            <a href="{{$forward_action}}" class="butn primary_butn fw-bold">
                                {{ __('web.complete_order') }} <i class="fas fa-angle-right ms-3"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </section>


    <!-- Start section -->
        @include('web.layouts.newsletter')
    </main>
@section('inject_js')
    <script>
        let url_edit_cart = "{{url(getCurrentLocale().'/cart/edit')}}"
        let url_delete_cart = "{{url(getCurrentLocale().'/cart/delete')}}"
        let url_calculate_order = "{{route('order.calculate')}}"
    </script>
    <script>
        setTimeout(function(){
            calculate()
        }, 700);
        function calculate()
        {
            $.ajax
            ({
                url : url_calculate_order,
                type: "GET",
                data: '',
                dataType:'json',
                success:function(data)
                {
                    $.each(data.products, function (key, product) {
                        $('#cart'+product.cart_id).html(product.count);
                        $('#product_cost'+product.cart_id).html(product.product_cost);
                        $('#tax_value'+product.cart_id).html(product.tax);
                        $('#product_cost_total'+product.cart_id).html(product.product_total);
                    });
                    $('#order_total').html(data.product_total);
                }
                ,error:function(data)
                {
                    toasterError(Object.values(data.responseJSON.errors)[0]);
                }
            })
        }
        $(".qt-plus").click(function() {
            let newVal = parseInt($(this).parent().children(".qt").html()) + 1;
            let cart_id = $(this).attr('data-id');
            $.ajax
            ({
                url : url_edit_cart+'/'+cart_id,
                type: "POST",
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                data: 'count='+newVal+'&status=plus',
                dataType:'json',
                success:function(data)
                {
                    
                    calculateCart()
                    $('#cart_count').html(data.data.cart_count);
                    if(data.data.cart_count > 0)
                    {
                        $('#cart_count').show();
                    }
                    else
                    {
                        $('#cart_count').hide();
                    }
                    toasterSuccess(data.message);
                    calculate()
                }
                ,error:function(data)
                {
                    toasterError(Object.values(data.responseJSON.errors)[0]);
                }
            })
        });

        $(".qt-minus").click(function() {

            child = $(this).parent().children(".qt");

            if (parseInt(child.html()) > 1) {
                let newVal = parseInt(child.html()) - 1;
                let cart_id = $(this).attr('data-id');
                $.ajax
                ({
                    url : url_edit_cart+'/'+cart_id,
                    type: "POST",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                    data: 'count='+newVal+'&status=minus',
                    dataType:'json',
                    success:function(data)
                    {
                        calculateCart()
                        $('#cart_count').html(data.data.cart_count);
                        if(data.data.cart_count > 0)
                        {
                            $('#cart_count').show();
                        }
                        else
                        {
                            $('#cart_count').hide();
                        }
                        toasterSuccess(data.message);
                        calculate()
                    }
                    ,error:function(data)
                    {
                        toasterError(Object.values(data.responseJSON.errors)[0]);
                    }
                })

            }

            $(this).parent().children(".full-price").addClass("minused");

        });
        function deleteCart(elem)
        {
            let cart_id = $(elem).attr('data-id');
            $.ajax
            ({
                url : url_delete_cart+'/'+cart_id,
                type: 'GET',
                data: '',
                success:function(data)
                {
                    $('#cart_count').html(data.data.count);
                    if(data.data.count > 0)
                    {
                        $('#cart_count').show();
                    }
                    else
                    {
                        $('#cart_count').hide();
                    }
                    calculateCart()
                    $(elem).closest('.cart_row').fadeOut(1000);
                    calculate();
                }
            })
        }
    </script>
@endsection
@stop
