<x-app-layout>

<x-module-hero
    title="Training Management"
    description="Manage training packages, sessions, registrations, and certificates"
    icon="fa-graduation-cap">

    <x-slot name="tabs">
        @can('view training packages')
            <a href="{{ route('training.packages.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.packages.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-box text-sm"></i> Packages
            </a>
        @endcan
        @can('view training sessions')
            <a href="{{ route('training.sessions.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.sessions.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-calendar-alt text-sm"></i> Sessions
            </a>
            <a href="{{ route('training.sessions.calendar') }}"
               class="module-hero-tab {{ request()->routeIs('training.sessions.calendar') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-calendar-week text-sm"></i> Calendar
            </a>
        @endcan
        @can('view training registrations')
            <a href="{{ route('training.registrations.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.registrations.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-users text-sm"></i> Registrations
            </a>
            <a href="{{ route('training.registrations.pending') }}"
               class="module-hero-tab {{ request()->routeIs('training.registrations.pending') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-clock text-sm"></i> Pending
            </a>
        @endcan
        @can('view training certificates')
            <a href="{{ route('training.certificates.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.certificates.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-certificate text-sm"></i> Certificates
            </a>
        @endcan
        @can('view training reports')
            <a href="{{ route('training.reports.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.reports.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-chart-bar text-sm"></i> Reports
            </a>
        @endcan
    </x-slot>

    <x-slot name="actions">
        @can('create training packages')
            <a href="{{ route('training.packages.create') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-[#1e293b] text-white text-sm font-semibold rounded-lg hover:bg-[#334155] transition">
                <i class="fas fa-plus"></i> New Package
            </a>
        @endcan
    </x-slot>

</x-module-hero>

{{-- Main Content --}}
<div class="p-6">

    {{-- Flash Messages --}}
    @if(session('success'))
        <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-check-circle"></i> {{ session('success') }}
        </div>
    @endif
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
        </div>
    @endif

    {{-- Search & Filters --}}
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 mb-4">
        <form method="GET" class="flex flex-wrap gap-3 items-center">
            <div class="flex-1 min-w-56 relative">
                <input type="text" name="search" value="{{ request('search') }}"
                       placeholder="Search packages..."
                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                <i class="fas fa-search absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 text-sm"></i>
            </div>
            <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                <option value="">All Status</option>
                <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Active</option>
                <option value="draft" {{ request('status') === 'draft' ? 'selected' : '' }}>Draft</option>
                <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                <option value="archived" {{ request('status') === 'archived' ? 'selected' : '' }}>Archived</option>
            </select>
            <select name="category" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                <option value="">All Categories</option>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}" {{ request('category') == $category->id ? 'selected' : '' }}>
                        {{ $category->name }}
                    </option>
                @endforeach
            </select>
            <button type="submit" class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg text-sm hover:bg-gray-200 transition">
                <i class="fas fa-filter mr-1"></i> Filter
            </button>
            @if(request('search') || request('status') || request('category'))
                <a href="{{ route('training.packages.index') }}" class="text-sm text-gray-500 hover:text-gray-700">
                    <i class="fas fa-times mr-1"></i> Clear
                </a>
            @endif
        </form>
    </div>

    {{-- Packages Table --}}
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="th-col-1 px-4 py-3 text-left">Package</th>
                        <th class="th-col-2 px-4 py-3 text-left">Category</th>
                        <th class="th-col-3 px-4 py-3 text-left">Duration</th>
                        <th class="th-col-4 px-4 py-3 text-left">Fee</th>
                        <th class="th-col-5 px-4 py-3 text-left">Enrolled</th>
                        <th class="th-col-6 px-4 py-3 text-left">Status</th>
                        <th class="th-col-7 px-4 py-3 text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($packages as $package)
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td class="px-4 py-3">
                                <div class="flex items-center gap-3">
                                    @if($package->featured_image)
                                        <img src="{{ $package->featured_image }}" alt="{{ $package->name }}"
                                             class="w-10 h-10 object-cover rounded">
                                    @else
                                        <div class="w-10 h-10 bg-green-100 rounded flex items-center justify-center">
                                            <i class="fas fa-book text-green-600"></i>
                                        </div>
                                    @endif
                                    <div>
                                        <div class="font-medium text-gray-900 text-sm">{{ $package->name }}</div>
                                        @if($package->generates_certificate)
                                            <span class="text-xs text-green-600">
                                                <i class="fas fa-certificate mr-1"></i>Certificate
                                            </span>
                                        @endif
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600">
                                {{ $package->category->name ?? 'Uncategorized' }}
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600">
                                {{ $package->getFormattedDuration() }}
                            </td>
                            <td class="px-4 py-3 text-sm">
                                @if($package->is_free)
                                    <span class="text-green-600 font-medium">Free</span>
                                @else
                                    <span class="text-gray-900">{{ $package->getFormattedFee() }}</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <div class="flex items-center gap-2">
                                    <span class="font-medium text-gray-900">{{ $package->registrations_count }}</span>
                                    <span class="text-gray-400">/ {{ $package->capacity }}</span>
                                </div>
                                <div class="w-20 h-1.5 bg-gray-200 rounded-full mt-1">
                                    <div class="h-1.5 rounded-full {{ $package->registrations_count >= $package->capacity ? 'bg-red-500' : 'bg-green-500' }}"
                                         style="width: {{ min(100, $package->capacity > 0 ? ($package->registrations_count / $package->capacity) * 100 : 0) }}%"></div>
                                </div>
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $statusColors = [
                                        'active' => 'bg-green-100 text-green-800',
                                        'draft' => 'bg-yellow-100 text-yellow-800',
                                        'inactive' => 'bg-red-100 text-red-800',
                                        'archived' => 'bg-gray-100 text-gray-800',
                                    ];
                                @endphp
                                <span class="px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$package->status] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ ucfirst($package->status) }}
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <div class="flex justify-center gap-1">
                                    @can('view training packages')
                                        <a href="{{ route('training.packages.show', $package) }}" class="btn-action-view">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    @endcan
                                    @can('edit training packages')
                                        <a href="{{ route('training.packages.edit', $package) }}" class="btn-action-edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    @endcan
                                    @can('delete training packages')
                                        <form action="{{ route('training.packages.destroy', $package) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this package?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td colspan="7" class="px-4 py-12 text-center text-gray-500">
                                <i class="fas fa-inbox text-4xl mb-3 text-gray-300 block"></i>
                                No training packages found.
                                @can('create training packages')
                                    <a href="{{ route('training.packages.create') }}" class="block mt-2 text-green-600 hover:text-green-700 text-sm">
                                        <i class="fas fa-plus mr-1"></i> Create your first package
                                    </a>
                                @endcan
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($packages->hasPages())
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $packages->links() }}
            </div>
        @endif
    </div>
</div>

</x-app-layout>
