<x-app-layout>
    <x-module-hero
        title="Mentorship Programmes"
        description="Manage mentorship programmes and their configurations"
        icon="fa-chalkboard-teacher">

        <x-slot name="tabs">
            @can('view mentorship programmes')
                <a href="{{ route('mentorship.programmes.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.programmes.*') ? '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> Programmes
                </a>
            @endcan
            @can('view mentorship applications')
                <a href="{{ route('mentorship.applications.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.applications.*') ? '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-file-alt text-sm"></i> Applications
                </a>
            @endcan
            @cannot('view mentorship applications')
                @can('view own mentorship applications')
                    <a href="{{ route('mentorship.applications.index') }}"
                       class="module-hero-tab {{ request()->routeIs('mentorship.applications.*') ? '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-file-alt text-sm"></i> My Applications
                    </a>
                @endcan
            @endcannot
            @can('view mentorship assignments')
                <a href="{{ route('mentorship.assignments.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.assignments.*') ? '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-link text-sm"></i> Assignments
                </a>
            @endcan
            @can('view mentorship sessions')
                <a href="{{ route('mentorship.sessions.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.sessions.index') || request()->routeIs('mentorship.sessions.show') || request()->routeIs('mentorship.sessions.create') || request()->routeIs('mentorship.sessions.edit') ? '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('mentorship.sessions.calendar') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.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 text-sm"></i> Sessions Calendar
                </a>
            @endcan
            @can('view mentorship certificates')
                <a href="{{ route('mentorship.certificates.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.certificates.*') ? '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 mentorship payments')
                <a href="{{ route('mentorship.payments.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.payments.*') ? '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-credit-card text-sm"></i> Payments
                </a>
            @endcan
            @can('view mentorship categories')
                <a href="{{ route('mentorship.categories.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.categories.*') ? '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-folder text-sm"></i> Categories
                </a>
            @endcan
        </x-slot>

        <x-slot name="actions">
            @can('create mentorship programmes')
                <a href="{{ route('mentorship.programmes.create') }}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-white text-green-800 text-sm font-medium rounded hover:bg-green-50 transition-colors">
                    <i class="fas fa-plus"></i> Create Programme
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    {{-- Content --}}
    <div class="p-6">
        @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 border border-gray-200 p-4 mb-4">
            <form method="GET" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4">
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Category</label>
                    <select name="category_id" class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                        <option value="">All Categories</option>
                        @foreach($categories as $category)
                            <option value="{{ $category->id }}" {{ $filters['category_id'] == $category->id ? 'selected' : '' }}>
                                {{ $category->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Status</label>
                    <select name="is_active" class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                        <option value="">All Status</option>
                        <option value="1" {{ $filters['is_active'] === '1' ? 'selected' : '' }}>Active</option>
                        <option value="0" {{ $filters['is_active'] === '0' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Fee Type</label>
                    <select name="is_free" class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                        <option value="">All Types</option>
                        <option value="1" {{ $filters['is_free'] === '1' ? 'selected' : '' }}>Free</option>
                        <option value="0" {{ $filters['is_free'] === '0' ? 'selected' : '' }}>Paid</option>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Search</label>
                    <input type="text" name="search" value="{{ $filters['search'] }}"
                           placeholder="Search programmes..."
                           class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="px-4 py-2 bg-green-600 text-white text-sm rounded-lg hover:bg-green-700 transition">
                        <i class="fas fa-search mr-1"></i> Filter
                    </button>
                    <a href="{{ route('mentorship.programmes.index') }}"
                       class="px-4 py-2 bg-gray-200 text-gray-700 text-sm rounded-lg hover:bg-gray-300 transition">
                        <i class="fas fa-times mr-1"></i> Clear
                    </a>
                </div>
            </form>
        </div>

        {{-- Table --}}
        <div class="w-full overflow-x-auto">
            @if($programmes->count() > 0)
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="th-col-1 px-4 py-3 text-left">Programme</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">Fee</th>
                            <th class="th-col-4 px-4 py-3 text-left">Duration</th>
                            <th class="th-col-5 px-4 py-3 text-left">Applications</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>
                        @foreach($programmes as $programme)
                            <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-2">
                                        @if($programme->is_featured)
                                            <i class="fas fa-star text-yellow-500" title="Featured"></i>
                                        @endif
                                        <div>
                                            <div class="font-medium text-gray-900 text-sm">{{ $programme->title }}</div>
                                            <div class="text-xs text-gray-500">{{ Str::limit($programme->description, 50) }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600">
                                    {{ $programme->category->name ?? 'Uncategorized' }}
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    @if($programme->is_free)
                                        <span class="px-2 py-1 bg-green-100 text-green-800 rounded-full text-xs font-semibold">Free</span>
                                    @else
                                        <span class="font-medium text-gray-900">{{ number_format($programme->fee_amount, 2) }} {{ $programme->currency }}</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600">{{ $programme->duration_text }}</td>
                                <td class="px-4 py-3 text-sm text-gray-600">
                                    <span class="inline-flex items-center gap-1">
                                        <i class="fas fa-users text-green-600"></i>
                                        {{ $programme->applications_count ?? 0 }}
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    @if($programme->is_active)
                                        <span class="px-2 py-1 bg-green-100 text-green-800 rounded-full text-xs font-semibold">Active</span>
                                    @else
                                        <span class="px-2 py-1 bg-red-100 text-red-800 rounded-full text-xs font-semibold">Inactive</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex justify-center gap-1">
                                        @can('view mentorship programmes')
                                            <a href="{{ route('mentorship.programmes.show', $programme) }}" class="btn-action-view">
                                                <i class="fas fa-eye"></i> View
                                            </a>
                                        @endcan
                                        @can('edit mentorship programmes')
                                            <a href="{{ route('mentorship.programmes.edit', $programme) }}" class="btn-action-edit">
                                                <i class="fas fa-edit"></i> Edit
                                            </a>
                                        @endcan
                                        @can('edit mentorship programmes')
                                            <button onclick="toggleStatus({{ $programme->id }}, {{ $programme->is_active ? 0 : 1 }})"
                                                    class="{{ $programme->is_active ? 'btn-action-reject' : 'btn-action-approve' }}">
                                                <i class="fas fa-{{ $programme->is_active ? 'pause' : 'play' }}"></i>
                                                {{ $programme->is_active ? 'Deactivate' : 'Activate' }}
                                            </button>
                                        @endcan
                                        @can('delete mentorship programmes')
                                            <form action="{{ route('mentorship.programmes.destroy', $programme) }}" method="POST" class="inline"
                                                  onsubmit="return confirm('Delete this programme?')">
                                                @csrf @method('DELETE')
                                                <button type="submit" class="btn-action-delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                <div class="px-4 py-3 border-t border-gray-200">
                    {{ $programmes->appends($filters)->links() }}
                </div>
            @else
                <div class="text-center py-16">
                    <i class="fas fa-chalkboard-teacher text-gray-300 text-5xl mb-4"></i>
                    <h3 class="text-lg font-medium text-gray-900 mb-2">No Programmes Found</h3>
                    <p class="text-gray-500 mb-4">Get started by creating your first mentorship programme.</p>
                    @can('create mentorship programmes')
                        <a href="{{ route('mentorship.programmes.create') }}"
                           class="inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
                            <i class="fas fa-plus"></i> Create Programme
                        </a>
                    @endcan
                </div>
            @endif
        </div>
    </div>

    @push('scripts')
    <script>
        function toggleStatus(programmeId, newStatus) {
            if (!confirm('Are you sure you want to ' + (newStatus ? 'activate' : 'deactivate') + ' this programme?')) return;
            const url = newStatus
                ? `{{ route('mentorship.programmes.activate', ['programme' => 'ID']) }}`.replace('ID', programmeId)
                : `{{ route('mentorship.programmes.deactivate', ['programme' => 'ID']) }}`.replace('ID', programmeId);
            fetch(url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
            }).then(r => r.json()).then(d => { if (d.success) location.reload(); else alert(d.message || 'Error'); });
        }
    </script>
    @endpush
</x-app-layout>
