@extends('layouts.app')

@section('content')
<div class="container mx-auto py-8">
    <div class="max-w-4xl mx-auto">
        <h1 class="text-3xl font-bold mb-8">Shopping Cart</h1>

        @if ($errors->any())
        <div class="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg">
            <p class="text-red-700 font-semibold">Error:</p>
            <ul class="text-red-600 text-sm mt-2">
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

        @if(session('success'))
        <div class="mb-4 p-4 bg-green-50 border border-green-200 rounded-lg">
            <p class="text-green-700">{{ session('success') }}</p>
        </div>
        @endif

        @if($cart && $cart->items()->count() > 0)
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Cart Items -->
            <div class="lg:col-span-2">
                <div class="bg-white rounded-lg shadow overflow-hidden">
                    @foreach($cart->items as $item)
                    <div class="p-6 border-b flex gap-4 hover:bg-gray-50">
                        @if($item->product->image)
                        <img src="{{ $item->product->image }}" alt="{{ $item->product->name }}" class="w-24 h-24 object-cover rounded">
                        @else
                        <div class="w-24 h-24 bg-gray-200 rounded flex items-center justify-center text-2xl">📦</div>
                        @endif

                        <div class="flex-1">
                            <h3 class="font-semibold text-lg">{{ $item->product->name }}</h3>
                            <p class="text-gray-600 text-sm">Price: ${{ number_format($item->price, 2) }}</p>

                            <div class="flex items-center gap-4 mt-4">
                                <form action="{{ route('cart.update', $item->id) }}" method="POST" class="flex items-center gap-2">
                                    @csrf
                                    <input type="number" name="quantity" value="{{ $item->quantity }}" min="1" class="w-16 px-2 py-1 border rounded">
                                    <button type="submit" class="px-3 py-1 bg-blue-600 text-white rounded text-sm hover:bg-blue-700">Update</button>
                                </form>

                                <form action="{{ route('cart.remove', $item->id) }}" method="POST" class="inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="px-3 py-1 bg-red-600 text-white rounded text-sm hover:bg-red-700">Remove</button>
                                </form>
                            </div>
                        </div>

                        <div class="text-right">
                            <p class="text-xl font-bold">{{ number_format($item->quantity) }}</p>
                            <p class="text-lg font-semibold text-blue-600">${{ number_format($item->subtotal, 2) }}</p>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>

            <!-- Order Summary -->
            <div class="bg-white rounded-lg shadow p-6">
                <h2 class="text-xl font-bold mb-6">Order Summary</h2>

                <div class="space-y-3 pb-6 border-b">
                    <div class="flex justify-between">
                        <span class="text-gray-600">Subtotal:</span>
                        <span>${{ number_format($cart->subtotal, 2) }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-gray-600">Tax:</span>
                        <span>${{ number_format($cart->tax, 2) }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-gray-600">Shipping:</span>
                        <span>${{ number_format($cart->shipping, 2) }}</span>
                    </div>
                </div>

                <div class="flex justify-between text-xl font-bold py-6">
                    <span>Total:</span>
                    <span class="text-blue-600">${{ number_format($cart->total, 2) }}</span>
                </div>

                <a href="{{ route('checkout') }}" class="w-full px-4 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-center font-semibold block">
                    Proceed to Checkout
                </a>

                <a href="{{ route('products') }}" class="w-full px-4 py-3 bg-gray-200 text-gray-800 rounded-lg hover:bg-gray-300 text-center font-semibold block mt-3">
                    Continue Shopping
                </a>
            </div>
        </div>
        @else
        <div class="bg-white rounded-lg shadow p-12 text-center">
            <p class="text-gray-500 text-lg mb-6">Your cart is empty</p>
            <a href="{{ route('products') }}" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
                Start Shopping
            </a>
        </div>
        @endif
    </div>
</div>
@endsection
