<x-app-layout>

<x-module-hero
    title="Training Sessions"
    description="Manage session schedule and attendance tracking"
    icon="fa-calendar-alt">

    <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-list 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 sessions')
            <a href="{{ route('training.sessions.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 Session
            </a>
        @endcan
    </x-slot>

</x-module-hero>

<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

    {{-- 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 sessions..."
                       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="scheduled" {{ request('status') === 'scheduled' ? 'selected' : '' }}>Scheduled</option>
                <option value="ongoing" {{ request('status') === 'ongoing' ? 'selected' : '' }}>Ongoing</option>
                <option value="completed" {{ request('status') === 'completed' ? 'selected' : '' }}>Completed</option>
                <option value="cancelled" {{ request('status') === 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                <option value="postponed" {{ request('status') === 'postponed' ? 'selected' : '' }}>Postponed</option>
            </select>
            <select name="package" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                <option value="">All Packages</option>
                @foreach($packages as $package)
                    <option value="{{ $package->id }}" {{ request('package') == $package->id ? 'selected' : '' }}>
                        {{ $package->name }}
                    </option>
                @endforeach
            </select>
            <input type="date" name="date_from" value="{{ request('date_from') }}"
                   class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
            <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('package') || request('date_from'))
                <a href="{{ route('training.sessions.index') }}" class="text-sm text-gray-500 hover:text-gray-700">
                    <i class="fas fa-times mr-1"></i> Clear
                </a>
            @endif
        </form>
    </div>

    {{-- Sessions 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">Session</th>
                        <th class="th-col-2 px-4 py-3 text-left">Package</th>
                        <th class="th-col-3 px-4 py-3 text-left">Date & Time</th>
                        <th class="th-col-4 px-4 py-3 text-left">Type</th>
                        <th class="th-col-5 px-4 py-3 text-left">Venue</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($sessions as $session)
                        <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="font-medium text-gray-900 text-sm">{{ $session->session_name }}</div>
                                @if($session->trainers->count() > 0)
                                    <div class="text-xs text-gray-500 mt-0.5">
                                        <i class="fas fa-user-tie mr-1"></i>
                                        {{ $session->trainers->pluck('name')->join(', ') }}
                                    </div>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600">
                                {{ $session->trainingPackage->name ?? 'N/A' }}
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <div class="text-gray-900">{{ $session->session_date->format('M d, Y') }}</div>
                                <div class="text-gray-500 text-xs">
                                    {{ \Carbon\Carbon::parse($session->start_time)->format('g:i A') }}
                                    – {{ \Carbon\Carbon::parse($session->end_time)->format('g:i A') }}
                                </div>
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $typeColors = ['in_person' => 'bg-blue-100 text-blue-800', 'online' => 'bg-purple-100 text-purple-800', 'hybrid' => 'bg-gray-100 text-gray-800'];
                                    $typeIcons = ['in_person' => 'fa-users', 'online' => 'fa-video', 'hybrid' => 'fa-globe'];
                                @endphp
                                <span class="px-2 py-1 rounded-full text-xs font-medium {{ $typeColors[$session->session_type] ?? 'bg-gray-100 text-gray-800' }}">
                                    <i class="fas {{ $typeIcons[$session->session_type] ?? 'fa-question' }} mr-1"></i>
                                    {{ ucfirst(str_replace('_', ' ', $session->session_type)) }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600">
                                @if($session->session_type === 'online')
                                    <span class="text-blue-600"><i class="fas fa-link mr-1"></i>Virtual</span>
                                @elseif($session->venue_name)
                                    <div class="text-gray-900">{{ $session->venue_name }}</div>
                                    @if($session->venue_address)
                                        <div class="text-xs text-gray-500">{{ Str::limit($session->venue_address, 30) }}</div>
                                    @endif
                                @else
                                    <span class="text-gray-400">TBD</span>
                                @endif
                            </td>
                            <td class="px-4 py-3">
                                @php
                                    $statusColors = ['scheduled' => 'bg-blue-100 text-blue-800', 'ongoing' => 'bg-green-100 text-green-800', 'completed' => 'bg-gray-100 text-gray-800', 'cancelled' => 'bg-red-100 text-red-800', 'postponed' => 'bg-yellow-100 text-yellow-800'];
                                @endphp
                                <span class="px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$session->status] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ ucfirst($session->status) }}
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <div class="flex justify-center gap-1">
                                    @can('view training sessions')
                                        <a href="{{ route('training.sessions.show', $session) }}" class="btn-action-view">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    @endcan
                                    @can('edit training sessions')
                                        <a href="{{ route('training.sessions.edit', $session) }}" class="btn-action-edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    @endcan
                                    @can('delete training sessions')
                                        <form action="{{ route('training.sessions.destroy', $session) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this session?');">
                                            @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-calendar-times text-4xl mb-3 text-gray-300 block"></i>
                                No sessions scheduled yet.
                                @can('create training sessions')
                                    <a href="{{ route('training.sessions.create') }}" class="block mt-2 text-green-600 hover:text-green-700 text-sm">
                                        <i class="fas fa-plus mr-1"></i> Schedule your first session
                                    </a>
                                @endcan
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($sessions->hasPages())
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $sessions->links() }}
            </div>
        @endif
    </div>
</div>

</x-app-layout>
