<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('My Subscriptions') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            @if ($message = Session::get('success'))
                <div class="mb-4 p-4 bg-green-100 text-green-700 rounded">
                    {{ $message }}
                </div>
            @endif

            @if ($message = Session::get('info'))
                <div class="mb-4 p-4 bg-blue-100 text-blue-700 rounded">
                    {{ $message }}
                </div>
            @endif

            <!-- Current Active Subscription Banner -->
            @if ($activeSubscription)
                <div class="mb-6 bg-gradient-to-r from-blue-600 to-blue-500 rounded-lg shadow-lg p-6 text-white">
                    <h3 class="text-2xl font-bold mb-2">{{ $activeSubscription->package->name }}</h3>
                    <div class="grid grid-cols-3 gap-4 mb-4">
                        <div>
                            <p class="text-blue-100 text-sm">{{ __('Driver Limit') }}</p>
                            <p class="text-2xl font-bold">{{ $activeSubscription->package->driver_limit }}</p>
                        </div>
                        <div>
                            <p class="text-blue-100 text-sm">{{ __('Truck Limit') }}</p>
                            <p class="text-2xl font-bold">{{ $activeSubscription->package->truck_limit }}</p>
                        </div>
                        <div>
                            <p class="text-blue-100 text-sm">{{ __('Billing') }}</p>
                            <p class="text-2xl font-bold">${{ number_format($activeSubscription->package->price, 2) }}</p>
                        </div>
                    </div>
                    <p class="text-blue-100 mb-4">
                        {{ __('Expires') }}: <strong>{{ $activeSubscription->ends_at ? $activeSubscription->ends_at->format('M d, Y') : __('Never') }}</strong>
                    </p>
                    <div class="flex gap-2">
                        <button onclick="document.getElementById('upgrade-modal').classList.remove('hidden')"
                            class="px-4 py-2 bg-white text-blue-600 font-semibold rounded hover:bg-gray-100 transition">
                            {{ __('Change Package') }}
                        </button>
                        <form action="{{ route('subscriptions.cancel', $activeSubscription) }}" method="POST" class="inline"
                            onsubmit="return confirm('{{ __('Are you sure you want to cancel?') }}');">
                            @csrf
                            @method('PATCH')
                            <button type="submit"
                                class="px-4 py-2 bg-red-600 text-white font-semibold rounded hover:bg-red-700 transition">
                                {{ __('Cancel Subscription') }}
                            </button>
                        </form>
                    </div>
                </div>
            @else
                <div class="mb-6 bg-yellow-50 border border-yellow-200 rounded-lg p-6">
                    <p class="text-yellow-800">{{ __('No active subscription') }}</p>
                    <a href="{{ route('subscriptions.select-package') }}"
                        class="mt-2 inline-block px-4 py-2 bg-blue-600 text-white font-semibold rounded hover:bg-blue-700 transition">
                        {{ __('Select a Package') }}
                    </a>
                </div>
            @endif

            <!-- Subscription History -->
            <div class="bg-white overflow-hidden shadow-sm rounded-lg">
                <div class="p-6 border-b">
                    <h3 class="text-lg font-semibold text-gray-900">{{ __('Subscription History') }}</h3>
                </div>

                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-100 border-b">
                            <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">{{ __('Status') }}</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">{{ __('Payment Status') }}</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">{{ __('Amount') }}</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">{{ __('Started') }}</th>
                                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-900">{{ __('Actions') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($subscriptions as $subscription)
                                <tr class="border-b hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap font-semibold">
                                        {{ $subscription->package->name }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        <span class="px-3 py-1 rounded-full text-xs font-semibold
                                            {{ $subscription->status === 'active' ? 'bg-green-100 text-green-800' : '' }}
                                            {{ $subscription->status === 'pending' ? 'bg-yellow-100 text-yellow-800' : '' }}
                                            {{ $subscription->status === 'expired' ? 'bg-gray-100 text-gray-800' : '' }}
                                            {{ $subscription->status === 'cancelled' ? 'bg-red-100 text-red-800' : '' }}
                                        ">
                                            {{ ucfirst($subscription->status) }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        <span class="px-3 py-1 rounded-full text-xs font-semibold
                                            {{ $subscription->payment_status === 'completed' ? 'bg-green-100 text-green-800' : '' }}
                                            {{ $subscription->payment_status === 'pending' ? 'bg-yellow-100 text-yellow-800' : '' }}
                                            {{ $subscription->payment_status === 'failed' ? 'bg-red-100 text-red-800' : '' }}
                                        ">
                                            {{ ucfirst($subscription->payment_status) }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold">
                                        ${{ number_format($subscription->package->price, 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        {{ $subscription->started_at->format('M d, Y') }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        <a href="{{ route('subscriptions.show', $subscription) }}"
                                            class="inline-block px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 text-xs">
                                            {{ __('View') }}
                                        </a>
                                        @if ($subscription->status === 'expired' && $subscription->payment_status !== 'failed')
                                            <form action="{{ route('subscriptions.renew', $subscription) }}" method="POST" class="inline">
                                                @csrf
                                                <button type="submit"
                                                    class="px-3 py-1 bg-green-600 text-white rounded hover:bg-green-700 text-xs">
                                                    {{ __('Renew') }}
                                                </button>
                                            </form>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="px-6 py-4 text-center text-gray-500">
                                        {{ __('No subscription history') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                @if ($subscriptions->hasPages())
                    <div class="px-6 py-4 border-t">
                        {{ $subscriptions->links() }}
                    </div>
                @endif
            </div>
        </div>
    </div>

    <!-- Upgrade Modal -->
    <div id="upgrade-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
        <div class="bg-white rounded-lg shadow-lg max-w-md w-full mx-4">
            <div class="p-6">
                <h3 class="text-lg font-semibold mb-4">{{ __('Change Package') }}</h3>
                <p class="text-gray-600 mb-4">
                    {{ __('Would you like to select a different package? Your current subscription will be updated.') }}
                </p>
                <div class="flex gap-4">
                    <a href="{{ route('subscriptions.select-package') }}"
                        class="flex-1 px-4 py-2 bg-blue-600 text-white font-semibold rounded text-center hover:bg-blue-700 transition">
                        {{ __('Select Package') }}
                    </a>
                    <button onclick="document.getElementById('upgrade-modal').classList.add('hidden')"
                        class="flex-1 px-4 py-2 bg-gray-300 text-gray-800 font-semibold rounded hover:bg-gray-400 transition">
                        {{ __('Cancel') }}
                    </button>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
