{{-- Admin Membership Applications Index --}}
<x-app-layout title="Membership Applications">

    <x-module-hero title="Membership Applications" description="Review and manage membership applications" icon="fa-file-alt">
        <x-slot name="tabs">
            @can('view members')
                <a href="{{ route('admin.membership.dashboard') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.membership.dashboard') ? '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-tachometer-alt text-sm"></i> Dashboard
                </a>
            @endcan
            @can('view members')
                <a href="{{ route('admin.members.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.members.index') || request()->routeIs('admin.members.show') || request()->routeIs('admin.members.create') ? '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> Members
                </a>
            @endcan
            @can('view members')
                <a href="{{ route('admin.members.applications.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.members.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
            @can('view membership categories')
                <a href="{{ route('admin.membership-categories.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.membership-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-tags text-sm"></i> Categories
                </a>
            @endcan
            @can('manage renewal configs')
                <a href="{{ route('admin.renewal-configs.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.renewal-configs.*') ? '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-rotate text-sm"></i> Renewal Config
                </a>
            @endcan
            @can('view contributions')
                <a href="{{ route('admin.contributions.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.contributions.*') ? '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-hand-holding-usd text-sm"></i> Contributions
                </a>
            @endcan
            @can('view loan products')
                <a href="{{ route('admin.loan-products.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.loan-products.*') ? '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> Loan Products
                </a>
            @endcan
            @can('view loan applications')
                <a href="{{ route('admin.loan-applications.index') }}"
                   class="module-hero-tab {{ request()->routeIs('admin.loan-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-money-bill-wave text-sm"></i> Loan Applications
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="w-full">
        @if(session('success'))
            <div class="mx-6 mt-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center">
                <i class="fas fa-check-circle mr-3"></i> {{ session('success') }}
            </div>
        @endif

        {{-- Search & Filter --}}
        <div class="p-6 bg-white border-b border-gray-200">
            <form method="GET" action="{{ route('admin.members.applications.index') }}">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Search</label>
                        <input type="text" name="search" value="{{ request('search') }}"
                               placeholder="Name, email, or reference..."
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Category</label>
                        <select name="category_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">All Categories</option>
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                        <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">All Status</option>
                            <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</option>
                            <option value="under_review" {{ request('status') === 'under_review' ? 'selected' : '' }}>Under Review</option>
                            <option value="approved" {{ request('status') === 'approved' ? 'selected' : '' }}>Approved</option>
                            <option value="rejected" {{ request('status') === 'rejected' ? 'selected' : '' }}>Rejected</option>
                        </select>
                    </div>
                    <div class="flex items-end gap-2">
                        <button type="submit" class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-medium">
                            <i class="fas fa-search mr-1"></i> Filter
                        </button>
                        <a href="{{ route('admin.members.applications.index') }}" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition text-sm">Clear</a>
                    </div>
                </div>
            </form>
        </div>

        {{-- Table --}}
        <div class="overflow-x-auto">
            @if($applications->count() > 0)
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left th-col-1">Reference</th>
                            <th class="px-4 py-3 text-left th-col-2">Applicant</th>
                            <th class="px-4 py-3 text-left th-col-3">Category</th>
                            <th class="px-4 py-3 text-left th-col-4">Status</th>
                            <th class="px-4 py-3 text-left th-col-5">Date</th>
                            <th class="px-4 py-3 text-center th-col-6">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($applications as $application)
                            <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 font-mono text-sm text-gray-600">{{ $application->reference_number }}</td>
                                <td class="px-4 py-3">
                                    <div class="font-medium text-gray-900">{{ $application->full_name }}</div>
                                    <div class="text-sm text-gray-500">{{ $application->email }}</div>
                                    <div class="text-sm text-gray-500">{{ $application->phone }}</div>
                                </td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $application->membershipCategory?->name ?? 'N/A' }}</td>
                                <td class="px-4 py-3">
                                    @php
                                        $sc = ['pending'=>'bg-yellow-100 text-yellow-800','under_review'=>'bg-blue-100 text-blue-800','approved'=>'bg-green-100 text-green-800','rejected'=>'bg-red-100 text-red-800'];
                                        $sc2 = $sc[$application->status] ?? 'bg-gray-100 text-gray-800';
                                    @endphp
                                    <span class="px-2 py-0.5 rounded-full text-xs font-semibold {{ $sc2 }}">{{ ucfirst(str_replace('_', ' ', $application->status)) }}</span>
                                </td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $application->created_at->format('M d, Y') }}</td>
                                <td class="px-4 py-3 text-center">
                                    <a href="{{ route('admin.members.applications.show', $application) }}" class="btn-action-view">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                <div class="px-4 py-3 border-t border-gray-200 bg-white">
                    {{ $applications->appends(request()->query())->links() }}
                </div>
            @else
                <div class="text-center py-16 bg-white">
                    <i class="fas fa-inbox text-5xl text-gray-300 mb-4"></i>
                    <h3 class="text-lg font-semibold text-gray-700 mb-2">No Applications Found</h3>
                    <p class="text-gray-500">
                        @if(request('search') || request('category_id') || request('status'))
                            No applications match your search criteria.
                        @else
                            There are no membership applications to review at this time.
                        @endif
                    </p>
                </div>
            @endif
        </div>
    </div>

</x-app-layout>
