@extends('layouts.dasht')
@section('content')
<div class="max-w-7xl mx-auto">
    <div class="mb-8">
        <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Complete Your Order</h1>
        <p class="mt-2 text-gray-600 dark:text-gray-400">Securely complete your purchase of {{ $car->name }}.</p>
    </div>

    @if(session('error'))
    <div class="bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 rounded-lg p-4 mb-6">
        <p class="text-red-700 dark:text-red-400">{{ session('error') }}</p>
    </div>
    @endif

    @if($errors->any())
    <div class="bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 rounded-lg p-4 mb-6">
        <ul class="list-disc list-inside text-red-700 dark:text-red-400 text-sm">
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif

    <form action="{{ route('cars.place-order', $car) }}" method="POST">
        @csrf
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            {{-- Left: Form Steps --}}
            <div class="lg:col-span-2 space-y-6">
                {{-- Step 1: Contact Information --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
                    <div class="flex items-center gap-3 mb-6">
                        <div class="w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center text-sm font-bold">1</div>
                        <h2 class="text-lg font-bold text-gray-900 dark:text-white">Contact Information</h2>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Full Name *</label>
                            <input type="text" name="billing_name" value="{{ old('billing_name', $user->name . ' ' . ($user->l_name ?? '')) }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Email *</label>
                            <input type="email" name="billing_email" value="{{ old('billing_email', $user->email) }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Phone</label>
                            <input type="text" name="billing_phone" value="{{ old('billing_phone', $user->phone) }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Company</label>
                            <input type="text" name="billing_company" value="{{ old('billing_company') }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent">
                        </div>
                    </div>
                </div>

                {{-- Step 2: Billing Address --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
                    <div class="flex items-center gap-3 mb-6">
                        <div class="w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center text-sm font-bold">2</div>
                        <h2 class="text-lg font-bold text-gray-900 dark:text-white">Billing Address</h2>
                    </div>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Street Address *</label>
                            <input type="text" name="billing_address" value="{{ old('billing_address') }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent" required>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">City *</label>
                                <input type="text" name="billing_city" value="{{ old('billing_city') }}"
                                       class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent" required>
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">State</label>
                                <input type="text" name="billing_state" value="{{ old('billing_state') }}"
                                       class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Postal Code</label>
                                <input type="text" name="billing_postal_code" value="{{ old('billing_postal_code') }}"
                                       class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent">
                            </div>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Country *</label>
                            <input type="text" name="billing_country" value="{{ old('billing_country', $user->country ?? '') }}"
                                   class="w-full px-4 py-2.5 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary-500 focus:border-transparent" required>
                        </div>
                    </div>
                </div>

                {{-- Step 3: Payment Method --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
                    <div class="flex items-center gap-3 mb-6">
                        <div class="w-8 h-8 bg-primary-500 text-white rounded-full flex items-center justify-center text-sm font-bold">3</div>
                        <h2 class="text-lg font-bold text-gray-900 dark:text-white">Payment Method</h2>
                    </div>
                    <p class="text-sm text-gray-500 dark:text-gray-400 mb-4">Select your preferred payment method. Full payment details will be shown on the next page.</p>
                    <div class="space-y-3">
                        @foreach($paymentMethods as $method)
                        <label class="flex items-center gap-4 p-4 rounded-lg border border-gray-200 dark:border-gray-600 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors has-[:checked]:border-primary-500 has-[:checked]:bg-primary-50 dark:has-[:checked]:bg-primary-900/20">
                            <input type="radio" name="payment_method" value="{{ $method->name }}"
                                   class="w-4 h-4 text-primary-500 focus:ring-primary-500" {{ old('payment_method') == $method->name ? 'checked' : '' }} required>
                            <div class="flex-1">
                                <div class="font-medium text-gray-900 dark:text-white">{{ $method->name }}</div>
                                @if($method->methodtype == 'crypto')
                                    <div class="text-xs text-gray-500 dark:text-gray-400 mt-1">Cryptocurrency Payment</div>
                                @else
                                    <div class="text-xs text-gray-500 dark:text-gray-400 mt-1">Bank Transfer</div>
                                @endif
                            </div>
                        </label>
                        @endforeach
                    </div>
                </div>
            </div>

            {{-- Right: Order Summary --}}
            <div>
                <div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6 sticky top-4">
                    <h2 class="text-lg font-bold text-gray-900 dark:text-white mb-4">Order Summary</h2>

                    {{-- Car Preview --}}
                    <div class="mb-4">
                        @if($car->photos->count() > 0)
                            <img src="{{ asset('dash/' . $car->primary_photo) }}" alt="{{ $car->name }}"
                                 class="w-full h-40 object-cover rounded-lg mb-3">
                        @endif
                        <h3 class="font-bold text-gray-900 dark:text-white">{{ $car->name }}</h3>
                        <p class="text-sm text-gray-500 dark:text-gray-400">{{ $car->year }} &bull; {{ $car->color }} &bull; {{ ucfirst($car->transmission) }}</p>
                    </div>

                    <div class="border-t border-gray-200 dark:border-gray-700 pt-4 space-y-2">
                        <div class="flex justify-between text-sm">
                            <span class="text-gray-500 dark:text-gray-400">Vehicle Price</span>
                            <span class="text-gray-900 dark:text-white">${{ number_format($car->price, 2) }}</span>
                        </div>
                        <div class="flex justify-between text-sm">
                            <span class="text-gray-500 dark:text-gray-400">Quantity</span>
                            <span class="text-gray-900 dark:text-white">1</span>
                        </div>
                        <div class="border-t border-gray-200 dark:border-gray-700 pt-2 flex justify-between">
                            <span class="font-bold text-gray-900 dark:text-white">Total</span>
                            <span class="font-bold text-xl text-primary-500">${{ number_format($car->price, 2) }}</span>
                        </div>
                    </div>

                    <button type="submit"
                            class="w-full mt-6 px-6 py-3 bg-primary-500 hover:bg-primary-600 text-white rounded-lg font-semibold transition-colors">
                        <i data-lucide="arrow-right" class="w-5 h-5 inline mr-2"></i>Continue to Payment
                    </button>

                    <div class="mt-4 space-y-2">
                        <div class="flex items-center gap-2 text-xs text-gray-500 dark:text-gray-400">
                            <i data-lucide="shield-check" class="w-4 h-4 text-green-500"></i>
                            <span>Secure & encrypted checkout</span>
                        </div>
                        <div class="flex items-center gap-2 text-xs text-gray-500 dark:text-gray-400">
                            <i data-lucide="headphones" class="w-4 h-4 text-blue-500"></i>
                            <span>24/7 customer support</span>
                        </div>
                        <div class="flex items-center gap-2 text-xs text-gray-500 dark:text-gray-400">
                            <i data-lucide="refresh-cw" class="w-4 h-4 text-purple-500"></i>
                            <span>Full refund guarantee</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection
