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

    <x-module-hero title="Contribution Management" description="Track and manage member contributions" icon="fa-hand-holding-usd">
        <x-slot name="tabs">
            @can('view contributions')
                <a href="{{ route('admin.contributions.index') }}" class="module-hero-tab active-tab">
                    <i class="fas fa-list"></i> All Contributions
                </a>
            @endcan
            @can('create contributions')
                <a href="{{ route('admin.contributions.create') }}" class="module-hero-tab">
                    <i class="fas fa-plus"></i> Record Contribution
                </a>
            @endcan
            @can('view members')
                <a href="{{ route('admin.membership.dashboard') }}" class="module-hero-tab">
                    <i class="fas fa-id-card"></i> Membership
                </a>
            @endcan
        </x-slot>
        <x-slot name="actions">
            @can('create contributions')
                <a href="{{ route('admin.contributions.create') }}" class="inline-flex items-center gap-2 px-4 py-2 bg-white/20 hover:bg-white/30 text-white text-sm font-medium rounded transition">
                    <i class="fas fa-plus"></i> Record Contribution
                </a>
            @endcan
            @can('export contributions')
                <a href="{{ route('admin.contributions.export', request()->query()) }}" class="inline-flex items-center gap-2 px-4 py-2 bg-white/20 hover:bg-white/30 text-white text-sm font-medium rounded transition">
                    <i class="fas fa-download"></i> Export
                </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 Collected</p>
                    <p class="text-2xl font-bold text-gray-900 mt-0.5">{{ number_format($statistics['total_collected'] ?? 0, 2) }}</p>
                </div>
                <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-coins 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">Paid</p>
                    <p class="text-2xl font-bold text-green-600 mt-0.5">{{ $statistics['paid_count'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-check-circle 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">Pending</p>
                    <p class="text-2xl font-bold text-yellow-600 mt-0.5">{{ $statistics['pending_count'] ?? 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">Overdue</p>
                    <p class="text-2xl font-bold text-red-600 mt-0.5">{{ $statistics['overdue_count'] ?? 0 }}</p>
                </div>
                <div class="w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-exclamation-circle text-red-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">Penalties</p>
                    <p class="text-2xl font-bold text-gray-900 mt-0.5">{{ number_format($statistics['total_penalties'] ?? 0, 2) }}</p>
                </div>
                <div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center">
                    <i class="fas fa-gavel text-orange-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

        {{-- Bulk Actions --}}
        @can('create contributions')
            <div class="px-6 pb-4 flex flex-wrap gap-3">
                <form action="{{ route('admin.contributions.generate-bulk') }}" method="POST" class="flex items-center gap-2">
                    @csrf
                    <select name="membership_category_id" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="">Generate for Category...</option>
                        @foreach(\App\Models\MembershipCategory::active()->get() as $category)
                            <option value="{{ $category->id }}">{{ $category->name }}</option>
                        @endforeach
                    </select>
                    <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-sync mr-1"></i> Generate
                    </button>
                </form>
                @can('edit contributions')
                    <form action="{{ route('admin.contributions.apply-penalties') }}" method="POST">
                        @csrf
                        <button type="submit" class="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-sm font-medium">
                            <i class="fas fa-gavel mr-1"></i> Apply Penalties
                        </button>
                    </form>
                @endcan
            </div>
        @endcan

        {{-- Search & Filter --}}
        <div class="px-6 pb-4 bg-white border-b border-gray-200 pt-4">
            <form method="GET" action="{{ route('admin.contributions.index') }}">
                <div class="grid grid-cols-1 md:grid-cols-5 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">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="paid" {{ request('status') === 'paid' ? 'selected' : '' }}>Paid</option>
                            <option value="overdue" {{ request('status') === 'overdue' ? 'selected' : '' }}>Overdue</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Cycle</label>
                        <select name="cycle" 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 Cycles</option>
                            <option value="monthly" {{ request('cycle') === 'monthly' ? 'selected' : '' }}>Monthly</option>
                            <option value="quarterly" {{ request('cycle') === 'quarterly' ? 'selected' : '' }}>Quarterly</option>
                            <option value="biannual" {{ request('cycle') === 'biannual' ? 'selected' : '' }}>Biannual</option>
                            <option value="annual" {{ request('cycle') === 'annual' ? 'selected' : '' }}>Annual</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.contributions.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($contributions->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">Member</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">Amount</th>
                            <th class="px-4 py-3 text-left th-col-5">Due Date</th>
                            <th class="px-4 py-3 text-center th-col-6">Status</th>
                            <th class="px-4 py-3 text-center th-col-7">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($contributions as $contribution)
                            <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">{{ $contribution->reference_number }}</td>
                                <td class="px-4 py-3">
                                    <div class="font-medium text-gray-900 text-sm">{{ $contribution->member?->full_name }}</div>
                                    <div class="text-xs text-gray-500">{{ $contribution->member?->membership_code }}</div>
                                </td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $contribution->membershipCategory?->name }}</td>
                                <td class="px-4 py-3">
                                    <div class="font-medium text-gray-900 text-sm">{{ number_format($contribution->amount, 2) }} {{ $contribution->currency }}</div>
                                    @if($contribution->penalty_amount > 0)
                                        <div class="text-xs text-red-600">+{{ number_format($contribution->penalty_amount, 2) }} penalty</div>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $contribution->due_date?->format('M d, Y') }}</td>
                                <td class="px-4 py-3 text-center">
                                    @php
                                        $sc = ['paid'=>'bg-green-100 text-green-700','pending'=>'bg-yellow-100 text-yellow-700','overdue'=>'bg-red-100 text-red-700'];
                                        $sc2 = $sc[$contribution->status] ?? 'bg-gray-100 text-gray-700';
                                    @endphp
                                    <span class="px-2 py-0.5 rounded-full text-xs font-semibold {{ $sc2 }}">{{ ucfirst($contribution->status) }}</span>
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <a href="{{ route('admin.contributions.show', $contribution) }}" 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">
                    {{ $contributions->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 Contributions Found</h3>
                    <p class="text-gray-500">
                        @if(request('search') || request('category_id') || request('status'))
                            No contributions match your search criteria.
                        @else
                            There are no contributions recorded yet.
                        @endif
                    </p>
                </div>
            @endif
        </div>
    </div>

</x-app-layout>
