@extends('layouts.user')

@section('title', 'User Dashboard')

@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 justify-between items-center">
                <h1 class="text-2xl font-bold text-gray-900">Dashboard</h1>
                <div class="flex items-center gap-4">
                    <div class="text-sm text-gray-500">
                        {{ now()->format('l, F j, Y') }}
                    </div>
                    <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>
                </div>
            </div>
        </header>

        <!-- Dashboard Content -->
        <div class="p-8">
            <!-- Stats Grid -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
                <div class="bg-white rounded-lg shadow p-6 border-l-4 border-blue-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm font-medium">Total Bookings</p>
                            <p class="text-3xl font-bold text-gray-900 mt-2">{{ $stats['total_bookings'] }}</p>
                        </div>
                        <div class="bg-blue-100 p-3 rounded-lg">
                            <svg class="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path>
                            </svg>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-lg shadow p-6 border-l-4 border-green-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm font-medium">Confirmed</p>
                            <p class="text-3xl font-bold text-gray-900 mt-2">{{ $stats['confirmed_bookings'] }}</p>
                        </div>
                        <div class="bg-green-100 p-3 rounded-lg">
                            <svg class="w-6 h-6 text-green-600" 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"></path>
                            </svg>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-lg shadow p-6 border-l-4 border-yellow-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm font-medium">Pending</p>
                            <p class="text-3xl font-bold text-gray-900 mt-2">{{ $stats['pending_bookings'] }}</p>
                        </div>
                        <div class="bg-yellow-100 p-3 rounded-lg">
                            <svg class="w-6 h-6 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                            </svg>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-lg shadow p-6 border-l-4 border-purple-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm font-medium">Total Spent</p>
                            <p class="text-3xl font-bold text-gray-900 mt-2">${{ number_format($stats['total_spent'], 0) }}</p>
                        </div>
                        <div class="bg-purple-100 p-3 rounded-lg">
                            <svg class="w-6 h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1a6.002 6.002 0 00-6 6v3a1 1 0 01-1 1H7a1 1 0 01-1-1v-3a6.002 6.002 0 016-6h1a1 1 0 011-1h2a1 1 0 011 1h1a6.002 6.002 0 016 6v3a1 1 0 01-1 1h-1a1 1 0 01-1-1v-3a6.002 6.002 0 00-6-6z"></path>
                            </svg>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Upcoming Trips & Recent Bookings -->
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
                <!-- Upcoming Trips -->
                <div class="lg:col-span-2 bg-white rounded-lg shadow">
                    <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
                        <h3 class="text-lg font-semibold text-gray-900">📅 Upcoming Trips</h3>
                        <a href="{{ route('user.bookings') }}" class="text-blue-600 hover:text-blue-900 text-sm font-medium">View All</a>
                    </div>
                    <div class="divide-y divide-gray-200">
                        @forelse ($upcomingBookings as $booking)
                            <div class="px-6 py-4 hover:bg-gray-50 transition-colors">
                                <div class="flex items-center justify-between">
                                    <div class="flex-1">
                                        <h4 class="font-semibold text-gray-900">{{ $booking->package->title }}</h4>
                                        <p class="text-sm text-gray-500 mt-1">📍 {{ $booking->package->destination?->name ?? 'N/A' }}</p>
                                        <p class="text-sm text-gray-500">👥 {{ $booking->number_of_people }} people</p>
                                    </div>
                                    <div class="text-right">
                                        <p class="font-semibold text-gray-900">{{ $booking->travel_date->format('M d, Y') }}</p>
                                        <p class="text-sm text-gray-500 mt-1">{{ $booking->travel_date->diffForHumans() }}</p>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <div class="px-6 py-8 text-center text-gray-500">
                                <p>No upcoming trips scheduled</p>
                                <a href="{{ route('packages.index') }}" class="text-blue-600 hover:text-blue-900 text-sm font-medium mt-2 inline-block">Browse Packages</a>
                            </div>
                        @endforelse
                    </div>
                </div>

                <!-- Quick Stats -->
                <div class="bg-white rounded-lg shadow p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">📊 Summary</h3>
                    <div class="space-y-4">
                        <div>
                            <div class="flex justify-between items-center mb-2">
                                <span class="text-sm text-gray-600">Booking Progress</span>
                                <span class="text-sm font-semibold text-gray-900">{{ $stats['confirmed_bookings'] }}/{{ $stats['total_bookings'] }}</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="bg-green-500 h-2 rounded-full" style="width: {{ $stats['total_bookings'] > 0 ? ($stats['confirmed_bookings'] / $stats['total_bookings'] * 100) : 0 }}%"></div>
                            </div>
                        </div>
                        <div class="border-t border-gray-200 pt-4">
                            <p class="text-sm text-gray-600 mb-2">Active Bookings</p>
                            <p class="text-2xl font-bold text-blue-600">{{ $stats['upcoming_bookings'] }}</p>
                        </div>
                        <div class="border-t border-gray-200 pt-4">
                            <p class="text-sm text-gray-600 mb-2">Cancelled</p>
                            <p class="text-2xl font-bold text-red-600">{{ $stats['cancelled_bookings'] }}</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Activity -->
            <div class="bg-white rounded-lg shadow mb-8">
                <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
                    <h3 class="text-lg font-semibold text-gray-900">📋 Recent Bookings</h3>
                    <a href="{{ route('user.bookings') }}" class="text-blue-600 hover:text-blue-900 text-sm font-medium">View All</a>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50 border-b border-gray-200">
                            <tr>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">Package</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">Reference</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">Travel Date</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">Status</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">Action</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @forelse ($recentBookings as $booking)
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4">
                                        <p class="font-medium text-gray-900">{{ $booking->package->title }}</p>
                                    </td>
                                    <td class="px-6 py-4">
                                        <code class="bg-gray-100 px-2 py-1 rounded text-sm text-gray-600">{{ $booking->booking_reference }}</code>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-500">
                                        {{ $booking->travel_date->format('M d, Y') }}
                                    </td>
                                    <td class="px-6 py-4">
                                        <span class="inline-flex px-3 py-1 rounded-full text-sm font-medium
                                            {{ $booking->status === 'confirmed' ? 'bg-green-100 text-green-800' : ($booking->status === 'pending' ? 'bg-yellow-100 text-yellow-800' : 'bg-red-100 text-red-800') }}">
                                            {{ ucfirst($booking->status) }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4">
                                        <a href="{{ route('user.booking.detail', $booking->booking_reference) }}" class="text-blue-600 hover:text-blue-900 text-sm font-medium">View Details</a>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="5" class="px-6 py-8 text-center text-gray-500">
                                        No bookings yet. <a href="{{ route('packages.index') }}" class="text-blue-600 hover:text-blue-900">Start booking now</a>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
</div>
@endsection
