@extends('layouts.store')

@section('title', 'Checkout')

@push('styles')
    <style>
        .checkout-layout {
            display: grid;
            grid-template-columns: minmax(0, 1.3fr) minmax(300px, 0.9fr);
            gap: 24px;
            margin-top: 30px;
        }

        .form-card,
        .summary-card {
            background: var(--card);
            border: 1px solid rgba(123, 91, 47, 0.16);
            border-radius: 24px;
            box-shadow: var(--shadow);
        }

        .form-card {
            padding: 24px;
        }

        .field-grid {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 18px;
        }

        .field {
            display: grid;
            gap: 8px;
        }

        .field.full {
            grid-column: 1 / -1;
        }

        label {
            color: var(--gold-deep);
            font-size: 0.95rem;
        }

        input,
        textarea {
            width: 100%;
            padding: 14px 16px;
            border-radius: 18px;
            border: 1px solid rgba(123, 91, 47, 0.22);
            background: rgba(255, 255, 255, 0.72);
            font: inherit;
            color: var(--text);
        }

        textarea {
            min-height: 120px;
            resize: vertical;
        }

        .error-text {
            color: #8f3640;
            font-size: 0.9rem;
        }

        .summary-card {
            padding: 24px;
            height: fit-content;
            position: sticky;
            top: 20px;
        }

        .summary-card h3,
        .form-card h3 {
            margin: 0 0 18px;
            font-size: 1.45rem;
        }

        .summary-item,
        .summary-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
        }

        .summary-item {
            padding: 14px 0;
            border-bottom: 1px solid rgba(123, 91, 47, 0.12);
        }

        .summary-row {
            padding: 12px 0;
            color: var(--muted);
        }

        .summary-total {
            color: var(--gold-deep);
            font-size: 1.5rem;
            font-weight: 700;
        }

        .place-order {
            width: 100%;
            margin-top: 14px;
            padding: 14px 20px;
            color: #fffaf0;
            background: linear-gradient(135deg, var(--gold), var(--gold-deep));
            box-shadow: 0 16px 30px rgba(125, 89, 45, 0.24);
        }

        @media (max-width: 960px) {
            .checkout-layout {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 640px) {
            .field-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
@endpush

@section('content')
    <h2 class="section-title">Checkout</h2>

    <section class="checkout-layout">
        <form id="checkout-form" class="form-card" action="{{ route('checkout.store') }}" method="POST">
            @csrf
            <h3>Collector Details</h3>

            <div class="field-grid">
                <div class="field">
                    <label for="customer_name">Full name</label>
                    <input id="customer_name" name="customer_name" type="text" value="{{ old('customer_name') }}" required>
                    @error('customer_name') <div class="error-text">{{ $message }}</div> @enderror
                </div>

                <div class="field">
                    <label for="email">Email</label>
                    <input id="email" name="email" type="email" value="{{ old('email') }}" required>
                    @error('email') <div class="error-text">{{ $message }}</div> @enderror
                </div>

                <div class="field">
                    <label for="phone">Phone</label>
                    <input id="phone" name="phone" type="text" value="{{ old('phone') }}" required>
                    @error('phone') <div class="error-text">{{ $message }}</div> @enderror
                </div>

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

                <div class="field full">
                    <label for="shipping_address">Shipping address</label>
                    <input id="shipping_address" name="shipping_address" type="text" value="{{ old('shipping_address') }}" required>
                    @error('shipping_address') <div class="error-text">{{ $message }}</div> @enderror
                </div>

                <div class="field">
                    <label for="postal_code">Postal code</label>
                    <input id="postal_code" name="postal_code" type="text" value="{{ old('postal_code') }}" required>
                    @error('postal_code') <div class="error-text">{{ $message }}</div> @enderror
                </div>

                <div class="field full">
                    <label for="notes">Order notes</label>
                    <textarea id="notes" name="notes" placeholder="Packaging requests, gifting notes, delivery timing...">{{ old('notes') }}</textarea>
                    @error('notes') <div class="error-text">{{ $message }}</div> @enderror
                </div>
            </div>
        </form>

        <aside class="summary-card">
            <h3>Order Summary</h3>

            @foreach ($items as $item)
                <div class="summary-item">
                    <div>
                        <strong>{{ $item['product']->name }}</strong>
                        <div class="meta-text">Qty {{ $item['quantity'] }}</div>
                    </div>
                    <span>${{ number_format($item['subtotal'], 2) }}</span>
                </div>
            @endforeach

            <div class="summary-row">
                <span>Subtotal</span>
                <span>${{ number_format($subtotal, 2) }}</span>
            </div>
            <div class="summary-row">
                <span>Shipping</span>
                <span>${{ number_format($shipping, 2) }}</span>
            </div>
            <div class="summary-row">
                <span>Estimated tax</span>
                <span>${{ number_format($tax, 2) }}</span>
            </div>
            <div class="summary-row">
                <span>Total</span>
                <span class="summary-total">${{ number_format($total, 2) }}</span>
            </div>

            <button class="place-order" type="submit" form="checkout-form">Place order</button>
        </aside>
    </section>
@endsection
