@extends('layouts.store')

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

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Checkout</span>
            <h1>My Cart</h1>
        </div>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="panel">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Price</th>
                        <th>Qty</th>
                        <th>Total</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($items as $item)
                        <tr>
                            <td>
                                <strong>{{ $item->product->name }}</strong>
                                @if ($item->variant)
                                    <small>{{ $item->variant->label() }}</small>
                                @endif
                            </td>
                            <td>{{ \App\Models\SiteSetting::formatPrice($item->unitPrice()) }}</td>
                            <td>
                                <form method="POST" action="{{ route('customer.cart.update', $item) }}" class="quantity-form">
                                    @csrf
                                    @method('PATCH')
                                    <input name="quantity" type="number" min="1" value="{{ $item->quantity }}">
                                    <button class="text-button" type="submit">Update</button>
                                </form>
                            </td>
                            <td>{{ \App\Models\SiteSetting::formatPrice($item->unitPrice() * $item->quantity) }}</td>
                            <td>
                                <form method="POST" action="{{ route('customer.cart.destroy', $item) }}">
                                    @csrf
                                    @method('DELETE')
                                    <button class="text-button danger" type="submit">Remove</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5">Your cart is empty.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="cart-total">
            <span>Subtotal</span>
            <strong>{{ $subtotalDisplay }}</strong>
        </div>
        @if ($items->isNotEmpty())
            <div class="cart-total">
                <a class="button primary" href="{{ route('customer.checkout.create') }}">Proceed to checkout</a>
            </div>
        @endif
    </section>
@endsection
