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

    <x-module-hero title="Loan Applications" description="Review and manage loan applications" icon="fa-file-alt">
        <x-slot name="tabs">
            @can('view loan products')
                <a href="{{ route('admin.loan-products.index') }}" class="module-hero-tab">
                    <i class="fas fa-box"></i> Products
                </a>
            @endcan
            @can('view loan applications')
                <a href="{{ route('admin.loan-applications.index') }}" class="module-hero-tab active-tab">
                    <i class="fas fa-file-alt"></i> Applications
                </a>
            @endcan
            @can('view loan applications')
                <a href="{{ route('admin.loans.active') }}" class="module-hero-tab">
                    <i class="fas fa-check-circle"></i> Active Loans
                </a>
            @endcan
            @can('view loan applications')
                <a href="{{ route('admin.loans.overdue') }}" class="module-hero-tab">
                    <i class="fas fa-exclamation-triangle"></i> Overdue
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    {{-- Statistics --}}
    @can('view statistics')
        <div class="p-6 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center justify-between">
                <div>
                    <p class="text-xs text-gray-500 font-medium">Total</p>
                    <p class="text-2xl font-bold text-gray-900 mt-0.5">{{ $statistics['total'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-gray-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-file-alt text-gray-600"></i>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center justify-between">
                <div>
                    <p class="text-xs text-gray-500 font-medium">Pending</p>
                    <p class="text-2xl font-bold text-yellow-600 mt-0.5">{{ $statistics['pending'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-yellow-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-clock text-yellow-600"></i>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center justify-between">
                <div>
                    <p class="text-xs text-gray-500 font-medium">Approved</p>
                    <p class="text-2xl font-bold text-green-600 mt-0.5">{{ $statistics['approved'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-check text-green-600"></i>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center justify-between">
                <div>
                    <p class="text-xs text-gray-500 font-medium">Disbursed</p>
                    <p class="text-2xl font-bold text-purple-600 mt-0.5">{{ $statistics['disbursed'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-money-bill-wave text-purple-600"></i>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center justify-between">
                <div>
                    <p class="text-xs text-gray-500 font-medium">Rejected</p>
                    <p class="text-2xl font-bold text-red-600 mt-0.5">{{ $statistics['rejected'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-times text-red-600"></i>
                </div>
            </div>
        </div>
    @endcan

    <div class="w-full">
        @if(session('success'))
            <div class="mx-6 mb-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="px-6 pb-4 bg-white border-b border-gray-200 pt-4">
            <form method="GET">
                <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="Reference or member..."
                               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">Product</label>
                        <select name="product_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 Products</option>
                            @foreach($products as $product)
                                <option value="{{ $product->id }}" {{ request('product_id') == $product->id ? 'selected' : '' }}>{{ $product->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="manager_approved" {{ request('status') === 'manager_approved' ? 'selected' : '' }}>Manager Approved</option>
                            <option value="approved" {{ request('status') === 'approved' ? 'selected' : '' }}>Approved</option>
                            <option value="disbursed" {{ request('status') === 'disbursed' ? 'selected' : '' }}>Disbursed</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.loan-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">Product</th>
                            <th class="px-4 py-3 text-right th-col-4">Amount</th>
                            <th class="px-4 py-3 text-center th-col-5">Status</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 text-sm">{{ $application->member?->full_name }}</div>
                                    <div class="text-xs text-gray-500">{{ $application->member?->membership_code }}</div>
                                </td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $application->loanProduct?->name }}</td>
                                <td class="px-4 py-3 text-right">
                                    <div class="font-medium text-gray-900 text-sm">{{ number_format($application->amount_requested, 2) }}</div>
                                    <div class="text-xs text-gray-500">{{ $application->currency }} / {{ $application->term_months }} mo</div>
                                </td>
                                <td class="px-4 py-3 text-center">
                                    @php
                                        $sc = ['pending'=>'bg-yellow-100 text-yellow-700','manager_approved'=>'bg-blue-100 text-blue-700','approved'=>'bg-green-100 text-green-700','disbursed'=>'bg-purple-100 text-purple-700','active'=>'bg-green-100 text-green-700','rejected'=>'bg-red-100 text-red-700'];
                                        $sc2 = $sc[$application->status] ?? 'bg-gray-100 text-gray-700';
                                    @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-center">
                                    <a href="{{ route('admin.loan-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('product_id') || request('status'))
                            No applications match your filter criteria.
                        @else
                            There are no loan applications yet.
                        @endif
                    </p>
                </div>
            @endif
        </div>
    </div>

</x-app-layout>
