@extends('layouts.app')

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

@section('styles')
<style>
    h1 {
        font-family: 'Abril Fatface', cursive;
        font-size: 2.5rem;
        color: var(--primary-color);
        margin-bottom: 2rem;
    }

    .cart-container {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 2rem;
        margin-bottom: 3rem;
    }



    .empty-cart {
        text-align: center;
        padding: 3rem 2rem;
    }

    .empty-cart p {
        color: #666;
        margin-bottom: 2rem;
        font-size: 1.1rem;
    }

    .cart-item {
        display: grid;
        grid-template-columns: 120px 1fr auto;
        gap: 2rem;
        padding: 1.5rem 0;
        border-bottom: 1px solid #eee;
        align-items: center;
    }

    .cart-item:last-child {
        border-bottom: none;
    }

    .item-image {
        width: 120px;
        height: 120px;
        object-fit: cover;
        border-radius: 4px;
    }

    .item-details h3 {
        color: var(--primary-color);
        margin-bottom: 0.5rem;
        font-family: 'Abril Fatface', cursive;
    }

    .item-details p {
        color: #666;
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }

    .item-price {
        font-weight: 600;
        color: var(--primary-color);
        font-size: 1.1rem;
    }

    .item-actions {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-end;
    }

    .quantity-form {
        display: flex;
        gap: 0.5rem;
        align-items: center;
    }

    .quantity-form input {
        width: 60px;
        padding: 0.4rem;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 0.9rem;
    }

    .quantity-form button {
        background-color: black;
        color: white;
        border: none;
        padding: 0.4rem 0.8rem;
        border-radius: 4px;
        cursor: pointer;
        font-size: 0.8rem;
        font-weight: 600;
    }

    .quantity-form button:hover {
        background-color: #1f3633;
        opacity: 0.9;
    }

    .summary {
        height: fit-content;
    }

    .summary h2 {
        font-family: 'Abril Fatface', cursive;
        font-size: 1.5rem;
        color: var(--primary-color);
        margin-bottom: 1.5rem;
    }

    .summary-row {
        display: flex;
        justify-content: space-between;
        padding: 1rem 0;
        border-bottom: 1px solid #eee;
    }

    .summary-row.total {
        font-family: Abril Fatface;
        font-weight: 400;
        font-size: 2rem;
        color: var(--primary-color);
        border-top: 2px solid var(--primary-color);
        border-bottom: none;
        padding-top: 1.5rem;
        margin-top: 1rem;
    }

    .summary-actions {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        margin-top: 2rem;
    }

    .btn-danger {
        background-color: white;
        color: black;
        font-size: 0.9rem;
        padding: 0.5rem 1rem;
    }

    .btn-danger:hover {
        background-color: #c09191;
    }

    .alert {
        padding: 1rem;
        border-radius: 4px;
        margin-bottom: 2rem;
    }

    .alert-success {
        background-color: #d4e8d4;
        color: var(--primary-color);
        border: 1px solid #a8d4a8;
    }

    .alert-info {
        background-color: #d1ecf1;
        color: #0c5460;
        border: 1px solid #bee5eb;
    }

    @media (max-width: 768px) {
        .cart-container {
            grid-template-columns: 1fr;
        }

        .cart-item {
            grid-template-columns: 80px 1fr;
            gap: 1rem;
        }

        .item-image {
            width: 80px;
            height: 80px;
        }

        .item-actions {
            grid-column: 1 / -1;
            flex-direction: row;
            justify-content: space-between;
        }
    }
</style>
@endsection

@section('content')
<main class="container">
    <h1>Shopping Cart</h1>

    @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    @if (session('error'))
        <div class="alert alert-info">
            {{ session('error') }}
        </div>
    @endif

    @if(count($items) > 0)
        <div class="cart-container">
            <!-- Cart Items -->
            <div class="cart-items card">
                @foreach($items as $item)
                <div class="cart-item">
                    @if($item['product']->images->count() > 0)
                        <img src="{{ asset('storage/' . $item['product']->images->first()->image_path) }}" alt="{{ $item['product']->name }}" class="item-image">
                    @else
                        <img src="{{ $item['product']->image }}" alt="{{ $item['product']->name }}" class="item-image">
                    @endif
                    
                    <div class="item-details">
                        <h3>{{ $item['product']->name }}{{ $item['is_sample'] ? ' - Sample' : '' }}</h3>
                        <p>{{ $item['product']->category->name }}</p>
                        
                        @php
                            $stockCost = 0;
                            $stockUnit = $item['type'] === 'wallpaper' ? '/m' : '/m²';
                            
                            if (!$item['is_sample'] && $item['stock']) {
                                $stockCost = $item['type'] === 'wallpaper' ? $item['stock']->cost_per_meter : $item['stock']->cost_per_m2;
                            }
                        @endphp
                        
                        @if($item['is_sample'])
                            <p style="font-size: 0.9rem; color: #666;">
                                Sample - Fixed Price: <span class="item-price">R{{ number_format(\App\Services\ShippingService::getSampleCost(), 2) }}</span>
                            </p>
                        @elseif($item['stock'])
                            <p style="font-size: 0.9rem; color: #666;">
                                {{ $item['stock']->name }}: <span class="item-price">R{{ number_format($stockCost, 2) }}{{ $stockUnit }}</span>
                            </p>
                        @endif
                        
                        @if(!$item['is_sample'])
                            @if($item['type'] === 'wallpaper')
                                <p style="color: #666; font-size: 0.9rem;">Length: {{ $item['length'] }}m</p>
                            @elseif($item['type'] === 'mural')
                                <p style="color: #666; font-size: 0.9rem;">Dimensions: {{ $item['width'] }}m × {{ $item['height'] }}m ({{ number_format($item['width'] * $item['height'], 2) }}m²)</p>
                            @endif
                        @endif
                        
                        <p style="color: var(--primary-color); font-weight: 600;">Subtotal: R{{ number_format($item['subtotal'], 2) }}</p>
                    </div>

                    <div class="item-actions">
                        <!-- <form action="{{ route('cart-update') }}" method="POST" class="quantity-form">
                            @csrf
                            <input type="hidden" name="item_key" value="{{ $item['key'] }}">
                            <input type="number" name="quantity" min="1" value="{{ $item['quantity'] }}">
                            <button type="submit">Update</button>
                        </form> -->

                        <form action="{{ route('cart-remove') }}" method="POST" style="margin-top: 0.5rem;">
                            @csrf
                            <input type="hidden" name="item_key" value="{{ $item['key'] }}">
                            <button type="submit" class="btn btn-danger">Remove</button>
                        </form>
                    </div>
                </div>
                @endforeach
            </div>

            <!-- Summary -->
            <div class="summary card">
                <h2>Order Summary</h2>

                <div class="summary-row">
                    <span>Subtotal:</span>
                    <span>R{{ number_format($total, 2) }}</span>
                </div>
                <div class="summary-row">
                    <span>{{ $shippingLabel }}:</span>
                    <span>R {{ number_format($shippingFee, 2) }}</span>
                </div>

                <div class="summary-row total">
                    <span>Total:</span>
                    <span>R {{ number_format($total + $shippingFee, 2) }}</span>
                </div>

                <div class="summary-actions">
                    <a href="{{ route('checkout') }}" class="btn" style="text-decoration: none;">Proceed to Checkout</a>
                    <a href="{{ route('wallpapers') }}" class="btn btn--secondary">Continue Shopping</a>
                </div>

                <form action="{{ route('cart-clear') }}" method="POST" style="margin-top: 1rem;">
                    @csrf
                    <button type="submit" class="btn btn-danger" style="width: 100%;">Clear Cart</button>
                </form>
            </div>
        </div>
    @else
        <div class="empty-cart">
            <p>Your cart is empty</p>
            <a href="{{ route('wallpapers') }}" class="btn">Start Shopping</a>
        </div>
    @endif
</main>
@endsection
