@extends('frontend.layouts.master')

@section('title','Cart Page')

@section('main-content')

<!-- Breadcrumbs -->
<div class="breadcrumbs">
    <div class="container">
        <ul class="bread-list">
            <li><a href="{{ route('home') }}">Home <i class="ti-arrow-right"></i></a></li>
            <li class="active">Cart</li>
        </ul>
    </div>
</div>

<div class="shopping-cart section">
    <div class="container">

        @if(Helper::cartHasSubscription() && Helper::cartHasProduct())
            <div class="alert alert-warning mb-3">
                Products and subscriptions must be ordered separately.
            </div>
        @endif

        <form action="{{ route('cart.update') }}" method="POST">
            @csrf

            <table class="table shopping-summery">
                <thead>
                    <tr>
                        <th>PRODUCT</th>
                        <th>NAME</th>
                        <th class="text-center">UNIT PRICE</th>
                        <th class="text-center">QUANTITY</th>
                        <th class="text-center">TOTAL</th>
                        <th class="text-center"><i class="ti-trash"></i></th>
                    </tr>
                </thead>

                <tbody>
                @forelse(Helper::getAllProductFromCart() as $key => $cart)
                    <tr>
                        <td>
                            @if($cart->item_type === 'product')
                                @php $photo = explode(',', $cart->product->photo); @endphp
                                <img src="{{ $photo[0] }}" width="60">
                            @else
                                <img src="{{ asset('frontend/images/subscription.png') }}" width="60">
                            @endif
                        </td>

                        <td>
                            @if($cart->item_type === 'product')
                                <a href="{{ route('product-detail',$cart->product->slug) }}">
                                    {{ $cart->product->title }}
                                </a>
                            @else
                                <strong>{{ $cart->subscription->package_name }}</strong><br>
                                <small class="text-muted">Subscription Kit</small>
                            @endif
                        </td>

                        <td class="text-center">
                            Rs. {{ number_format($cart->price,2) }}
                        </td>

                        <td class="text-center">
                            @if($cart->item_type === 'product')
                                <input type="text"
                                       name="quant[{{$key}}]"
                                       value="{{ $cart->quantity }}"
                                       class="input-number">
                                <input type="hidden" name="qty_id[]" value="{{ $cart->id }}">
                            @else
                                <span class="badge badge-info">1 Kit</span>
                            @endif
                        </td>

                        <td class="text-center">
                            Rs. {{ number_format($cart->amount,2) }}
                        </td>

                        <td class="text-center">
                            <a href="{{ route('cart-delete',$cart->id) }}">
                                <i class="ti-trash"></i>
                            </a>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="text-center">
                            Cart is empty.
                            <a href="{{ route('product-grids') }}">Continue shopping</a>
                        </td>
                    </tr>
                @endforelse
                </tbody>
            </table>

            @if(Helper::cartHasProduct())
                <button class="btn btn-primary float-right">Update Cart</button>
            @endif
        </form>

        <div class="total-amount mt-4">
            <ul>
                <li>
                    Subtotal
                    <span>Rs. {{ number_format(Helper::totalCartPrice(),2) }}</span>
                </li>
            </ul>

            <div class="button5">
                <a href="{{ route('checkout') }}" class="btn">Checkout</a>
                <a href="{{ route('product-grids') }}" class="btn">Continue Shopping</a>
            </div>
        </div>

    </div>
</div>

@endsection
