{{-- Member Contributions History --}}
<x-app-layout title="My Contributions">

    <x-module-hero title="My Contributions" description="View and pay your contributions" icon="fa-hand-holding-usd">
        <x-slot name="tabs">
            <a href="{{ route('member.dashboard') }}" class="module-hero-tab">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="{{ route('member.profile') }}" class="module-hero-tab">
                <i class="fas fa-user"></i> Profile
            </a>
            <a href="{{ route('member.contributions') }}" class="module-hero-tab active-tab">
                <i class="fas fa-hand-holding-usd"></i> Contributions
            </a>
            <a href="{{ route('member.loans.index') }}" class="module-hero-tab">
                <i class="fas fa-money-bill-wave"></i> Loans
            </a>
        </x-slot>
    </x-module-hero>

    {{-- Summary Cards --}}
    <div class="p-6 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 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 Paid</p>
                <p class="text-2xl font-bold text-green-600 mt-0.5">{{ number_format($history['total_paid'] ?? 0, 2) }}</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">{{ $history['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">{{ $history['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">Total Outstanding</p>
                <p class="text-2xl font-bold text-gray-900 mt-0.5">{{ number_format($history['total_outstanding'] ?? 0, 2) }}</p>
            </div>
            <div class="w-10 h-10 bg-gray-100 rounded-full flex items-center justify-center">
                <i class="fas fa-dollar-sign text-gray-600"></i>
            </div>
        </div>
    </div>

    <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

        {{-- Filters --}}
        <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">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="paid" {{ request('status') === 'paid' ? 'selected' : '' }}>Paid</option>
                            <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</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">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($member->membershipCategories 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">Per Page</label>
                        <select name="per_page" 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="15" {{ request('per_page') == 15 ? 'selected' : '' }}>15</option>
                            <option value="30" {{ request('per_page') == 30 ? 'selected' : '' }}>30</option>
                            <option value="50" {{ request('per_page') == 50 ? 'selected' : '' }}>50</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('member.contributions') }}" 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($history['contributions']->count() > 0)
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left th-col-1">Period</th>
                            <th class="px-4 py-3 text-left th-col-2">Category</th>
                            <th class="px-4 py-3 text-right th-col-3">Amount</th>
                            <th class="px-4 py-3 text-left th-col-4">Due Date</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">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($history['contributions'] as $contribution)
                            <tr>
                                <td class="px-4 py-3 text-gray-900 text-sm">{{ $contribution->period ?? 'N/A' }}</td>
                                <td class="px-4 py-3 text-gray-600 text-sm">{{ $contribution->membershipCategory?->name }}</td>
                                <td class="px-4 py-3 text-right">
                                    <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-sm text-gray-600">{{ $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">
                                    @if(!$contribution->isPaid())
                                        <a href="{{ route('member.contributions.pay', $contribution) }}" class="btn-action-approve">
                                            <i class="fas fa-credit-card"></i> Pay Now
                                        </a>
                                    @else
                                        <span class="text-gray-400 text-xs">Paid</span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                @if(method_exists($history['contributions'], 'links'))
                    <div class="px-4 py-3 border-t border-gray-200 bg-white">
                        {{ $history['contributions']->appends(request()->query())->links() }}
                    </div>
                @endif
            @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('status') || request('category_id'))
                            No contributions match your filter criteria.
                        @else
                            You don't have any contributions yet.
                        @endif
                    </p>
                </div>
            @endif
        </div>
    </div>

</x-app-layout>
