@extends('layouts.user')

@section('title', 'Booking Details - ' . $booking->booking_reference)

@section('content')
<div class="flex h-screen bg-gray-100">
    <!-- Sidebar -->
    @include('user.sidebar')

    <!-- Main Content -->
    <main class="flex-1 overflow-auto">
        <!-- Top Header -->
        <header class="bg-white shadow-sm sticky top-0 z-10">
            <div class="px-8 py-4 flex items-center justify-between">
                <div class="flex items-center gap-4">
                    <a href="{{ route('user.bookings') }}" 
                       class="w-10 h-10 rounded-full bg-white shadow-sm border border-gray-200 flex items-center justify-center text-gray-500 hover:text-gray-700 hover:bg-gray-50 transition-colors">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                        </svg>
                    </a>
                    <div>
                        <h1 class="text-2xl font-bold text-gray-900">Booking Details</h1>
                        <p class="text-sm text-gray-500">Reference: <span class="font-mono font-semibold">{{ $booking->booking_reference }}</span></p>
                    </div>
                </div>
                
                <div class="flex items-center gap-3">
                    <a href="{{ route('home') }}" target="_blank" rel="noopener noreferrer"
                       class="inline-flex items-center gap-1.5 text-sm px-3 py-1.5 rounded-lg bg-white border border-gray-200 hover:border-emerald-300 hover:bg-emerald-50 text-gray-600 hover:text-emerald-700 transition-all">
                        <span>View Website</span>
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
                        </svg>
                    </a>
                    
                    @php
                        $statusClass = match($booking->status) {
                            'confirmed' => 'bg-green-100 text-green-800 border-green-200',
                            'pending' => 'bg-yellow-100 text-yellow-800 border-yellow-200',
                            'cancelled' => 'bg-red-100 text-red-800 border-red-200',
                            'completed' => 'bg-blue-100 text-blue-800 border-blue-200',
                            default => 'bg-gray-100 text-gray-800 border-gray-200'
                        };
                    @endphp
                    <span class="inline-flex items-center px-4 py-1.5 rounded-full text-sm font-semibold border {{ $statusClass }}">
                        {{ ucfirst($booking->status) }}
                    </span>
                </div>
            </div>
        </header>

        <div class="p-8 max-w-5xl">
            <!-- Success / Error Messages -->
            @if (session('success'))
                <div class="mb-6 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg">
                    {{ session('success') }}
                </div>
            @endif
            @if (session('error'))
                <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                    {{ session('error') }}
                </div>
            @endif

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                
                <!-- Main Booking Info -->
                <div class="lg:col-span-2 bg-white rounded-xl shadow border border-gray-200 p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4 flex items-center gap-2">
                        <span>📋</span> Booking Information
                    </h3>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Travel Date</p>
                            <p class="font-semibold text-gray-900 text-lg">{{ $booking->travel_date->format('F j, Y') }}</p>
                            <p class="text-sm text-gray-500">{{ $booking->travel_date->diffForHumans() }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Number of Travelers</p>
                            <p class="font-semibold text-gray-900 text-lg">{{ $booking->number_of_people }} people</p>
                        </div>
                        
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Payment Status</p>
                            <span class="inline-flex px-3 py-1 rounded-full text-sm font-medium
                                {{ $booking->payment_status === 'paid' ? 'bg-green-100 text-green-800' : 'bg-yellow-100 text-yellow-800' }}">
                                {{ ucfirst($booking->payment_status) }}
                            </span>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Payment Method</p>
                            <p class="font-medium text-gray-900">{{ ucfirst($booking->payment_method ?? 'Not specified') }}</p>
                        </div>
                    </div>

                    @if($booking->special_requests)
                        <div class="mt-6 pt-6 border-t">
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Special Requests</p>
                            <p class="text-gray-700 bg-gray-50 p-3 rounded-lg border">{{ $booking->special_requests }}</p>
                        </div>
                    @endif
                </div>

                <!-- Price Card -->
                <div class="bg-white rounded-xl shadow border border-gray-200 p-6 flex flex-col">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">💰 Price Summary</h3>
                    
                    <div class="flex-1">
                        <div class="flex justify-between py-2 border-b">
                            <span class="text-gray-600">Total Amount</span>
                            <span class="font-bold text-xl text-gray-900">{{ $booking->formatted_total_price }}</span>
                        </div>
                    </div>

                    <div class="mt-6 space-y-2">
                        @if($booking->status !== 'cancelled' && $booking->isPending)
                            <a href="{{ route('user.booking.cancel', $booking->booking_reference) }}" 
                               onclick="return confirm('Are you sure you want to cancel this booking?')"
                               class="block w-full text-center px-4 py-2.5 bg-red-50 hover:bg-red-100 text-red-600 rounded-lg font-medium transition-colors">
                                Cancel Booking
                            </a>
                        @endif

                        @if($booking->payment_status === 'paid')
                            <a href="{{ route('user.booking.invoice', $booking->booking_reference) }}" 
                               class="block w-full text-center px-4 py-2.5 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors">
                                📄 Download Invoice
                            </a>
                            <a href="{{ route('user.booking.ticket', $booking->booking_reference) }}" 
                               class="block w-full text-center px-4 py-2.5 bg-green-600 hover:bg-green-700 text-white rounded-lg font-medium transition-colors">
                                🎫 Download Ticket
                            </a>
                        @endif
                    </div>
                </div>

                <!-- Traveler Details -->
                <div class="lg:col-span-3 bg-white rounded-xl shadow border border-gray-200 p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">👤 Primary Traveler</h3>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Full Name</p>
                            <p class="font-medium text-gray-900">{{ $booking->full_name }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Email</p>
                            <p class="font-medium text-gray-900">{{ $booking->email }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Phone</p>
                            <p class="font-medium text-gray-900">{{ $booking->phone ?? '—' }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-1">Country</p>
                            <p class="font-medium text-gray-900">{{ $booking->country ?? '—' }}</p>
                        </div>
                    </div>

                    @if(!empty($booking->traveler_details))
                        <div class="mt-6 pt-6 border-t">
                            <p class="text-xs text-gray-500 uppercase tracking-wider mb-3">Additional Travelers</p>
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-3">
                                @foreach($booking->traveler_details as $index => $traveler)
                                    <div class="bg-gray-50 border rounded-lg p-3 text-sm">
                                        <span class="font-medium">#{{ $index + 1 }}</span> 
                                        {{ $traveler['name'] ?? 'Traveler' }} 
                                        @if(!empty($traveler['age'])) — {{ $traveler['age'] }} yrs @endif
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    @endif
                </div>

                <!-- Package Info -->
                <div class="lg:col-span-3 bg-white rounded-xl shadow border border-gray-200 p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">🗺️ Package Details</h3>
                    
                    <div class="flex flex-col md:flex-row md:items-center gap-6">
                        @if($booking->package->main_image)
                            <img src="{{ $booking->package->main_image }}" 
                                 class="w-full md:w-48 h-32 object-cover rounded-lg border" 
                                 alt="{{ $booking->package->title }}">
                        @endif
                        
                        <div class="flex-1">
                            <a href="{{ route('packages.show', $booking->package->slug) }}" 
                               class="text-xl font-semibold text-blue-600 hover:underline">
                                {{ $booking->package->title }}
                            </a>
                            
                            <p class="text-gray-600 mt-1">
                                {{ $booking->package->destination?->name ?? 'Multiple Destinations' }} 
                                • {{ $booking->package->duration_days }} days
                            </p>
                            
                            <div class="mt-3">
                                <a href="{{ route('packages.show', $booking->package->slug) }}" 
                                   class="text-sm text-blue-600 hover:text-blue-800 font-medium inline-flex items-center gap-1">
                                    View Package Details →
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Cancellation Section -->
            @if($booking->status !== 'cancelled')
                <div class="mt-6 bg-white rounded-xl shadow border border-gray-200 p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-2">Cancel Booking</h3>
                    <p class="text-sm text-gray-600 mb-4">
                        You can cancel this booking up to 48 hours before the travel date. 
                        A cancellation reason is required.
                    </p>

                    @if(now()->greaterThan($booking->travel_date->subHours(48)))
                        <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg text-sm">
                            ❌ Cancellation window has closed (less than 48 hours before travel).
                        </div>
                    @else
                        <form action="{{ route('user.booking.cancel', $booking->booking_reference) }}" method="POST">
                            @csrf
                            <div class="mb-4">
                                <label class="block text-sm font-medium text-gray-700 mb-1">Cancellation Reason</label>
                                <textarea name="cancellation_reason" rows="3" required
                                          class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-blue-500"
                                          placeholder="Please tell us why you're cancelling..."></textarea>
                            </div>
                            <button type="submit"
                                    onclick="return confirm('Are you sure you want to cancel this booking?')"
                                    class="bg-red-600 hover:bg-red-700 text-white px-6 py-2.5 rounded-lg font-medium transition-colors">
                                Confirm Cancellation
                            </button>
                        </form>
                    @endif
                </div>
            @endif

        </div>
    </main>
</div>
@endsection