{{-- Admin Membership Application Details --}}
<x-app-layout title="Application Review">

    <x-module-hero title="Application Review" :description="'Reference: ' . $application->reference_number" icon="fa-file-alt">
        <x-slot name="backButton">
            <a href="{{ route('admin.members.applications.index') }}" class="inline-flex items-center gap-2 text-white/80 hover:text-white text-sm transition">
                <i class="fas fa-arrow-left"></i> Back to Applications
            </a>
        </x-slot>
        <x-slot name="tabs">
            @can('view members')
                <a href="{{ route('admin.membership.dashboard') }}" class="module-hero-tab">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a>
            @endcan
            @can('view members')
                <a href="{{ route('admin.members.applications.index') }}" class="module-hero-tab active-tab">
                    <i class="fas fa-file-alt"></i> Applications
                </a>
            @endcan
            @can('view members')
                <a href="{{ route('admin.members.index') }}" class="module-hero-tab">
                    <i class="fas fa-users"></i> Members
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        @if(session('success'))
            <div class="mb-6 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
        @if($errors->any())
            <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <ul class="list-disc list-inside text-sm">
                    @foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach
                </ul>
            </div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-6">

                {{-- Personal Information --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                            <i class="fas fa-user text-green-600"></i> Personal Information
                        </h3>
                    </div>
                    <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">First Name</label>
                            <p class="text-gray-900 font-medium">{{ $application->first_name }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Last Name</label>
                            <p class="text-gray-900 font-medium">{{ $application->last_name }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Email</label>
                            <a href="mailto:{{ $application->email }}" class="text-green-600 hover:underline">{{ $application->email }}</a>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Phone</label>
                            <a href="tel:{{ $application->phone }}" class="text-green-600 hover:underline">{{ $application->phone }}</a>
                        </div>
                    </div>
                </div>

                {{-- Address Information --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                            <i class="fas fa-map-marker-alt text-green-600"></i> Address Information
                        </h3>
                    </div>
                    <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Address</label>
                            <p class="text-gray-900 text-sm">{{ $application->address ?: 'Not provided' }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">City</label>
                            <p class="text-gray-900 text-sm">{{ $application->city ?: 'Not provided' }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Country</label>
                            <p class="text-gray-900 text-sm">{{ $application->country ?: 'Not provided' }}</p>
                        </div>
                    </div>
                </div>

                {{-- Professional Information --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                            <i class="fas fa-briefcase text-green-600"></i> Professional Information
                        </h3>
                    </div>
                    <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Occupation</label>
                            <p class="text-gray-900 text-sm">{{ $application->occupation ?: 'Not provided' }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Organization</label>
                            <p class="text-gray-900 text-sm">{{ $application->organization ?: 'Not provided' }}</p>
                        </div>
                        @if($application->professional_background)
                            <div class="md:col-span-2">
                                <label class="block text-xs font-medium text-gray-500 mb-1">Professional Background</label>
                                <p class="text-gray-900 text-sm whitespace-pre-line">{{ $application->professional_background }}</p>
                            </div>
                        @endif
                    </div>
                </div>

                @if($application->status === 'rejected' && $application->rejection_reason)
                    <div class="bg-red-50 border border-red-200 rounded-lg p-6">
                        <h3 class="text-base font-semibold text-red-800 flex items-center gap-2 mb-2">
                            <i class="fas fa-times-circle"></i> Rejection Reason
                        </h3>
                        <p class="text-red-700 text-sm">{{ $application->rejection_reason }}</p>
                        @if($application->reviewer)
                            <p class="text-xs text-red-600 mt-2">Rejected by {{ $application->reviewer->name }} on {{ $application->reviewed_at->format('M d, Y H:i') }}</p>
                        @endif
                    </div>
                @endif
            </div>

            {{-- Sidebar --}}
            <div class="space-y-6">
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900">Application Info</h3>
                    </div>
                    <div class="p-6 space-y-4">
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Reference Number</label>
                            <p class="text-gray-900 font-mono text-sm">{{ $application->reference_number }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Applied Category</label>
                            <span class="inline-block bg-green-100 text-green-700 px-3 py-1 rounded-full text-sm">
                                {{ $application->membershipCategory?->name ?? 'N/A' }}
                            </span>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Status</label>
                            @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>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Submitted On</label>
                            <p class="text-gray-900 text-sm">{{ $application->created_at->format('M d, Y H:i') }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Last Updated</label>
                            <p class="text-gray-900 text-sm">{{ $application->updated_at->format('M d, Y H:i') }}</p>
                        </div>
                    </div>
                </div>

                @if(in_array($application->status, ['pending', 'under_review']))
                    <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                            <h3 class="text-base font-semibold text-gray-900">Actions</h3>
                        </div>
                        <div class="p-6 space-y-3">
                            @can('approve members')
                                <form action="{{ route('admin.members.applications.approve', $application) }}" method="POST">
                                    @csrf
                                    <button type="submit"
                                            class="w-full px-4 py-2.5 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-medium flex items-center justify-center gap-2"
                                            onclick="return confirm('Approve this application? A member account will be created.')">
                                        <i class="fas fa-check-circle"></i> Approve Application
                                    </button>
                                </form>
                                <button type="button"
                                        onclick="document.getElementById('rejectModal').classList.remove('hidden')"
                                        class="w-full px-4 py-2.5 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-sm font-medium flex items-center justify-center gap-2">
                                    <i class="fas fa-times-circle"></i> Reject Application
                                </button>
                            @endcan
                        </div>
                    </div>
                @endif
            </div>
        </div>
    </div>

</x-app-layout>

{{-- Reject Modal --}}
<div id="rejectModal" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 hidden">
    <div class="bg-white rounded-lg shadow-xl max-w-md w-full mx-4">
        <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-red-600 to-red-700 rounded-t-lg">
            <h3 class="text-base font-semibold text-white">Reject Application</h3>
            <button type="button" onclick="document.getElementById('rejectModal').classList.add('hidden')"
                    class="text-white/80 hover:text-white transition"><i class="fas fa-times"></i></button>
        </div>
        <form action="{{ route('admin.members.applications.reject', $application) }}" method="POST">
            @csrf
            <div class="p-6">
                <label class="block text-sm font-medium text-gray-700 mb-2">Reason for Rejection <span class="text-red-500">*</span></label>
                <textarea name="rejection_reason" rows="4" required
                          class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500 focus:border-red-500 text-sm"
                          placeholder="Please provide a reason for rejecting this application..."></textarea>
                @error('rejection_reason')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200 flex justify-end gap-3 rounded-b-lg">
                <button type="button" onclick="document.getElementById('rejectModal').classList.add('hidden')"
                        class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition text-sm">Cancel</button>
                <button type="submit" class="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-sm font-medium">Confirm Rejection</button>
            </div>
        </form>
    </div>
</div>
