@extends('layouts.customer')

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

@section('content')
<div class="cart-container">
    <div class="page-header">
        <h1>Shopping Cart</h1>
    </div>

    @if(count($cartItems) > 0)
    <div class="cart-layout">
        <!-- Cart Items -->
        <div class="cart-items-section">
            @foreach($cartItems as $item)
            <div class="cart-item">
                <div class="item-image">
                    @if($item['wine']->image_url)
                        <img src="{{ asset('storage/' . $item['wine']->image_url) }}" alt="{{ $item['wine']->wine_name }}" style="width: 100%; height: 100%; object-fit: cover; border-radius: 8px;">
                    @else
                        <svg fill="currentColor" viewBox="0 0 24 24">
                            <path d="M6 3v11c0 3.87 3.13 7 7 7s7-3.13 7-7V3H6zm12 2v2H6V5h12zm-7 14c-2.76 0-5-2.24-5-5v-5h10v5c0 2.76-2.24 5-5 5z"/>
                            <circle cx="13" cy="11" r="1"/>
                        </svg>
                    @endif
                </div>

                <div class="item-details">
                    <h3 class="item-name">
                        <a href="{{ route('catalog.show', $item['wine']->id) }}">
                            {{ $item['wine']->wine_name }}
                        </a>
                    </h3>
                    <p class="item-variety">{{ $item['wine']->grapeVariety->variety_name ?? 'N/A' }}</p>
                    <p class="item-vintage">Vintage: {{ $item['wine']->vintage }}</p>
                    <p class="item-price-unit">{{ number_format($item['wine']->price_per_bottle, 2) }} € / pcs</p>
                </div>

                <div class="item-quantity">
                    <form action="{{ route('cart.update', $item['wine']->id) }}" method="POST" class="quantity-form">
                        @csrf
                        @method('PATCH')
                        <div class="quantity-control">
                            <button
                                type="button"
                                class="qty-btn-small"
                                onclick="updateQuantity({{ $item['wine']->id }}, {{ $item['quantity'] - 1 }}, {{ $item['wine']->bottles_in_stock }})"
                            >
                                <svg style="width: 16px; height: 16px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 12H4"/>
                                </svg>
                            </button>
                            <input
                                type="number"
                                name="quantity"
                                value="{{ $item['quantity'] }}"
                                min="1"
                                max="{{ $item['wine']->bottles_in_stock }}"
                                class="quantity-input-small"
                                id="qty-{{ $item['wine']->id }}"
                                onchange="this.form.submit()"
                            >
                            <button
                                type="button"
                                class="qty-btn-small"
                                onclick="updateQuantity({{ $item['wine']->id }}, {{ $item['quantity'] + 1 }}, {{ $item['wine']->bottles_in_stock }})"
                            >
                                <svg style="width: 16px; height: 16px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                                </svg>
                            </button>
                        </div>
                    </form>
                </div>

                <div class="item-total">
                    <div class="item-total-price">{{ number_format($item['item_total'], 2) }} €</div>
                    <form action="{{ route('cart.remove', $item['wine']->id) }}" method="POST" class="remove-form">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn-remove">
                            <svg style="width: 18px; height: 18px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                            </svg>
                            Remove
                        </button>
                    </form>
                </div>
            </div>
            @endforeach

            <!-- Clear Cart Button -->
            <div class="cart-actions">
                <form action="{{ route('cart.clear') }}" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn-clear-cart" onclick="return confirm('Are you sure you want to clear the cart?')">
                        <svg style="width: 18px; height: 18px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                        </svg>
                        Clear Cart
                    </button>
                </form>
            </div>
        </div>

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

            <div class="summary-items">
                <div class="summary-row">
                    <span>Products</span>
                    <span>{{ array_sum(array_column($cartItems, 'quantity')) }} pcs</span>
                </div>
                <div class="summary-row">
                    <span>Subtotal</span>
                    <span>{{ number_format($total, 2) }} €</span>
                </div>
                <div class="summary-divider"></div>
                <div class="summary-row summary-total">
                    <span>Total</span>
                    <span>{{ number_format($total, 2) }} €</span>
                </div>
            </div>

            @auth
            <!-- Checkout Form for Authenticated Users -->
            <div class="checkout-section">
                <h3>Billing Information</h3>
                <form action="{{ route('checkout') }}" method="POST" class="checkout-form">
                    @csrf

                    <div class="form-group">
                        <label for="full_name">Full Name *</label>
                        <input
                            type="text"
                            id="full_name"
                            name="full_name"
                            class="form-input"
                            value="{{ old('full_name', auth()->user()->first_name . ' ' . auth()->user()->last_name) }}"
                            required
                        >
                        @error('full_name')
                        <span class="error-message">{{ $message }}</span>
                        @enderror
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="phone">Phone Number *</label>
                            <input
                                type="tel"
                                id="phone"
                                name="phone"
                                class="form-input"
                                value="{{ old('phone') }}"
                                placeholder="+420 123 456 789"
                                required
                            >
                            @error('phone')
                            <span class="error-message">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label for="country">Country *</label>
                            <select
                                id="country"
                                name="country"
                                class="form-input"
                                required
                            >
                                <option value="">Select Country</option>
                                <option value="Czech Republic" {{ old('country') == 'Czech Republic' ? 'selected' : '' }}>Czech Republic</option>
                                <option value="Slovakia" {{ old('country') == 'Slovakia' ? 'selected' : '' }}>Slovakia</option>
                                <option value="Austria" {{ old('country') == 'Austria' ? 'selected' : '' }}>Austria</option>
                                <option value="Germany" {{ old('country') == 'Germany' ? 'selected' : '' }}>Germany</option>
                                <option value="Poland" {{ old('country') == 'Poland' ? 'selected' : '' }}>Poland</option>
                                <option value="Hungary" {{ old('country') == 'Hungary' ? 'selected' : '' }}>Hungary</option>
                                <option value="Other" {{ old('country') == 'Other' ? 'selected' : '' }}>Other</option>
                            </select>
                            @error('country')
                            <span class="error-message">{{ $message }}</span>
                            @enderror
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="street">Street and Number *</label>
                        <input
                            type="text"
                            id="street"
                            name="street"
                            class="form-input"
                            value="{{ old('street') }}"
                            required
                        >
                        @error('street')
                        <span class="error-message">{{ $message }}</span>
                        @enderror
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="city">City *</label>
                            <input
                                type="text"
                                id="city"
                                name="city"
                                class="form-input"
                                value="{{ old('city') }}"
                                required
                            >
                            @error('city')
                            <span class="error-message">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label for="postal_code">Postal Code *</label>
                            <input
                                type="text"
                                id="postal_code"
                                name="postal_code"
                                class="form-input"
                                value="{{ old('postal_code') }}"
                                placeholder="123 45"
                                required
                            >
                            @error('postal_code')
                            <span class="error-message">{{ $message }}</span>
                            @enderror
                        </div>
                    </div>

                    <button type="submit" class="btn-checkout">
                        <svg style="width: 20px; height: 20px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        Complete Order
                    </button>
                </form>
            </div>
            @else
            <!-- Login Prompt for Guests -->
            <div class="login-prompt">
                <svg style="width: 32px; height: 32px; color: #718096;" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"/>
                </svg>
                <p>You must log in to complete your order</p>
                <a href="{{ route('login') }}" class="btn-login">Log In</a>
                <p style="margin-top: 1rem; font-size: 0.9rem;">
                    Don't have an account? <a href="{{ route('register') }}" style="color: #4299e1;">Register</a>
                </p>
            </div>
            @endauth

            <a href="{{ route('catalog.index') }}" class="btn-continue-shopping">
                <svg style="width: 18px; height: 18px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
                </svg>
                Continue Shopping
            </a>
        </div>
    </div>
    @else
    <!-- Empty Cart State -->
    <div class="empty-cart">
        <svg style="width: 120px; height: 120px; color: #cbd5e0;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"/>
        </svg>
        <h2>Your cart is empty</h2>
        <p>Add wine from our catalog</p>
        <a href="{{ route('catalog.index') }}" class="btn-shop-now">
            <svg style="width: 20px; height: 20px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"/>
            </svg>
            Go to Shop
        </a>
    </div>
    @endif
</div>

<style>
    .cart-container {
        max-width: 100%;
    }

    .page-header {
        text-align: center;
        margin-bottom: 3rem;
    }

    .page-header h1 {
        font-size: 2.5rem;
        color: #2d3748;
        font-weight: 700;
    }

    .cart-layout {
        display: grid;
        grid-template-columns: 1fr 400px;
        gap: 2rem;
        align-items: start;
    }

    /* Cart Items Section */
    .cart-items-section {
        display: flex;
        flex-direction: column;
        gap: 1rem;
    }

    .cart-item {
        background: white;
        border-radius: 12px;
        padding: 1.5rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        display: grid;
        grid-template-columns: 100px minmax(200px, 1fr) auto auto;
        gap: 1.5rem;
        align-items: center;
    }

    .item-image {
        background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
        border-radius: 8px;
        width: 100px;
        height: 100px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #cbd5e0;
    }

    .item-image svg {
        width: 50px;
        height: 50px;
    }

    .item-details {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        min-width: 0;
        flex: 1;
    }

    .item-name {
        font-size: 1.25rem;
        font-weight: 600;
        color: #2d3748;
        margin: 0;
        word-wrap: break-word;
        overflow-wrap: break-word;
        hyphens: auto;
    }

    .item-name a {
        color: #2d3748;
        text-decoration: none;
        transition: color 0.2s;
        display: inline-block;
        max-width: 100%;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .item-name a:hover {
        color: #4299e1;
    }

    .item-variety, .item-vintage, .item-price-unit {
        font-size: 0.9rem;
        color: #718096;
        margin: 0;
    }

    .item-quantity {
        display: flex;
        align-items: center;
    }

    .quantity-form {
        margin: 0;
    }

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

    .qty-btn-small {
        background: #f7fafc;
        border: 2px solid #e2e8f0;
        border-radius: 6px;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s;
        color: #4a5568;
    }

    .qty-btn-small:hover {
        background: #edf2f7;
        border-color: #cbd5e0;
    }

    .quantity-input-small {
        width: 60px;
        padding: 0.5rem;
        border: 2px solid #e2e8f0;
        border-radius: 6px;
        text-align: center;
        font-size: 1rem;
        font-weight: 600;
    }

    .quantity-input-small:focus {
        outline: none;
        border-color: #89b4d9;
    }

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

    .item-total-price {
        font-size: 1.5rem;
        font-weight: 700;
        color: #2d3748;
    }

    .remove-form {
        margin: 0;
    }

    .btn-remove {
        background: none;
        border: none;
        color: #e53e3e;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.9rem;
        font-weight: 500;
        transition: all 0.2s;
        padding: 0.25rem 0.5rem;
    }

    .btn-remove:hover {
        color: #c53030;
    }

    .cart-actions {
        display: flex;
        justify-content: flex-end;
        padding: 1rem 0;
    }

    .btn-clear-cart {
        background: #fff5f5;
        color: #e53e3e;
        border: 2px solid #feb2b2;
        padding: 0.75rem 1.5rem;
        border-radius: 8px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .btn-clear-cart:hover {
        background: #fed7d7;
        border-color: #fc8181;
    }

    /* Order Summary */
    .order-summary {
        background: white;
        border-radius: 12px;
        padding: 2rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        position: sticky;
        top: 80px;
    }

    .order-summary h2 {
        font-size: 1.5rem;
        color: #2d3748;
        margin: 0 0 1.5rem 0;
    }

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

    .summary-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 1rem;
        color: #4a5568;
    }

    .summary-divider {
        height: 2px;
        background: #e2e8f0;
        margin: 0.5rem 0;
    }

    .summary-total {
        font-size: 1.5rem;
        font-weight: 700;
        color: #2d3748;
    }

    /* Checkout Section */
    .checkout-section {
        margin-top: 2rem;
        padding-top: 2rem;
        border-top: 2px solid #e2e8f0;
    }

    .checkout-section h3 {
        font-size: 1.25rem;
        color: #2d3748;
        margin: 0 0 1.5rem 0;
    }

    .checkout-form {
        display: flex;
        flex-direction: column;
        gap: 1rem;
    }

    .form-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
    }

    .form-group label {
        font-size: 0.9rem;
        color: #4a5568;
        font-weight: 600;
    }

    .form-input {
        padding: 0.75rem;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        font-size: 1rem;
        transition: all 0.2s;
    }

    .form-input:focus {
        outline: none;
        border-color: #89b4d9;
    }

    .error-message {
        color: #e53e3e;
        font-size: 0.85rem;
    }

    .btn-checkout {
        background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
        color: white;
        padding: 1rem;
        border-radius: 10px;
        border: none;
        font-size: 1.1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.75rem;
        margin-top: 1rem;
    }

    .btn-checkout:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(72, 187, 120, 0.4);
    }

    /* Login Prompt */
    .login-prompt {
        text-align: center;
        padding: 2rem 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1rem;
        margin-top: 2rem;
        border-top: 2px solid #e2e8f0;
    }

    .login-prompt p {
        margin: 0;
        color: #4a5568;
    }

    .btn-login {
        background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
        color: #1a365d;
        padding: 0.75rem 2rem;
        border-radius: 8px;
        font-weight: 600;
        text-decoration: none;
        transition: all 0.2s;
        display: inline-block;
    }

    .btn-login:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(137, 180, 217, 0.4);
    }

    .btn-continue-shopping {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        padding: 0.75rem 1rem;
        background: #f7fafc;
        color: #4a5568;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        text-decoration: none;
        font-weight: 500;
        transition: all 0.2s;
        margin-top: 1rem;
    }

    .btn-continue-shopping:hover {
        background: #edf2f7;
    }

    /* Empty Cart */
    .empty-cart {
        text-align: center;
        padding: 4rem 2rem;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1.5rem;
    }

    .empty-cart h2 {
        font-size: 2rem;
        color: #2d3748;
        margin: 0;
    }

    .empty-cart p {
        font-size: 1.1rem;
        color: #718096;
        margin: 0;
    }

    .btn-shop-now {
        background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
        color: #1a365d;
        padding: 1rem 2rem;
        border-radius: 10px;
        font-weight: 600;
        text-decoration: none;
        transition: all 0.3s;
        display: inline-flex;
        align-items: center;
        gap: 0.75rem;
        font-size: 1.1rem;
        margin-top: 1rem;
    }

    .btn-shop-now:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(137, 180, 217, 0.4);
    }

    /* Tablet Responsive */
    @media (max-width: 768px) {
        .cart-item {
            grid-template-columns: 100px 1fr;
            gap: 1rem;
        }

        .item-quantity, .item-total {
            grid-column: 1 / -1;
        }

        .item-total {
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }
    }

    /* Mobile Responsive */
    @media (max-width: 968px) {
        .cart-layout {
            grid-template-columns: 1fr;
        }

        .item-details {
            min-width: 0;
            overflow: hidden;
        }

        .item-name {
            font-size: 1.1rem;
        }

        .form-row {
            grid-template-columns: 1fr;
        }

        .order-summary {
            position: static;
        }
    }

    @media (max-width: 480px) {
        .cart-item {
            grid-template-columns: 80px 1fr;
        }
    }
</style>

<script>
    function updateQuantity(wineId, newQty, maxStock) {
        if (newQty < 1 || newQty > maxStock) {
            return;
        }

        const input = document.getElementById('qty-' + wineId);
        input.value = newQty;
        input.form.submit();
    }
</script>
@endsection
