<x-app-layout>
    {{-- Hero Section --}}
    <div class="bg-gradient-to-r from-green-600 to-green-700 shadow-lg">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
            <div class="flex items-center justify-between mb-6">
                <div class="flex items-center">
                    <i class="fas fa-hand-holding-usd text-white text-3xl mr-4"></i>
                    <div>
                        <h1 class="text-3xl font-bold text-white">My Contributions</h1>
                        <p class="text-green-100 mt-1">View and manage your membership contributions</p>
                    </div>
                </div>
            </div>

            {{-- Navigation Tabs --}}
            <div class="flex flex-wrap gap-2 mb-6">
                <a href="{{ route('member.contributions') }}"
                   class="px-4 py-2 rounded-lg bg-white text-green-700 transition font-medium">
                    <i class="fas fa-list mr-2"></i>All Contributions
                </a>
                <a href="{{ route('member.dashboard') }}"
                   class="px-4 py-2 rounded-lg bg-green-500 text-white hover:bg-green-400 transition">
                    <i class="fas fa-tachometer-alt mr-2"></i>Dashboard
                </a>
            </div>

            {{-- Stats --}}
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg p-4">
                    <p class="text-green-100 text-sm font-semibold">Total Paid</p>
                    <p class="text-white text-2xl font-bold">{{ $stats['total_paid'] ?? 0 }}</p>
                </div>
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg p-4">
                    <p class="text-green-100 text-sm font-semibold">Pending</p>
                    <p class="text-white text-2xl font-bold">{{ $stats['pending'] ?? 0 }}</p>
                </div>
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg p-4">
                    <p class="text-green-100 text-sm font-semibold">Overdue</p>
                    <p class="text-white text-2xl font-bold">{{ $stats['overdue'] ?? 0 }}</p>
                </div>
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-lg p-4">
                    <p class="text-green-100 text-sm font-semibold">Total Amount</p>
                    <p class="text-white text-2xl font-bold">{{ number_format($stats['total_amount'] ?? 0, 2) }}</p>
                </div>
            </div>
        </div>
    </div>

    {{-- Main Content --}}
    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">

            @if(session('success'))
                <div class="mb-6 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg">
                    <i class="fas fa-check-circle mr-2"></i>{{ session('success') }}
                </div>
            @endif

            @if(session('error'))
                <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                    <i class="fas fa-exclamation-circle mr-2"></i>{{ session('error') }}
                </div>
            @endif

            <div class="theme-card overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    @if(isset($contributions) && $contributions->count() > 0)
                        <div class="overflow-x-auto">
                            <table class="min-w-full divide-y theme-divider">
                                <thead class="theme-card">
                                    <tr>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Period</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Category</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Amount</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Due Date</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                                    </tr>
                                </thead>
                                <tbody class="theme-body divide-y theme-divider">
                                    @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-6 py-4 text-sm text-gray-900">{{ $contribution->period ?? 'N/A' }}</td>
                                            <td class="px-6 py-4 text-sm text-gray-600">{{ $contribution->membershipCategory?->name ?? 'N/A' }}</td>
                                            <td class="px-6 py-4 text-sm font-medium text-gray-900">
                                                {{ number_format($contribution->amount, 2) }} {{ $contribution->currency ?? 'SSP' }}
                                                @if($contribution->penalty_amount > 0)
                                                    <span class="text-xs text-red-500 block">+{{ number_format($contribution->penalty_amount, 2) }} penalty</span>
                                                @endif
                                            </td>
                                            <td class="px-6 py-4 text-sm {{ $contribution->status === 'overdue' ? 'text-red-600 font-medium' : 'text-gray-600' }}">
                                                {{ $contribution->due_date?->format('M d, Y') ?? 'N/A' }}
                                            </td>
                                            <td class="px-6 py-4">
                                                @php
                                                    $statusColors = [
                                                        'paid'    => 'bg-green-100 text-green-800',
                                                        'pending' => 'bg-yellow-100 text-yellow-800',
                                                        'overdue' => 'bg-red-100 text-red-800',
                                                    ];
                                                    $color = $statusColors[$contribution->status] ?? 'bg-gray-100 text-gray-800';
                                                @endphp
                                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $color }}">
                                                    {{ ucfirst($contribution->status) }}
                                                </span>
                                            </td>
                                            <td class="px-6 py-4 text-right text-sm font-medium">
                                                <div class="flex justify-end gap-2">
                                                    @if(in_array($contribution->status, ['pending', 'overdue']))
                                                        <a href="{{ route('member.contributions.pay', $contribution) }}"
                                                           class="inline-flex items-center px-3 py-1 bg-green-600 text-white text-xs rounded hover:bg-green-700 transition">
                                                            <i class="fas fa-credit-card mr-1"></i>Pay Now
                                                        </a>
                                                    @endif
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <div class="mt-6">
                            {{ $contributions->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            <i class="fas fa-hand-holding-usd text-6xl text-gray-300 mb-4"></i>
                            <h3 class="text-xl font-semibold text-theme-heading mb-2">No Contributions Found</h3>
                            <p class="text-theme-body">Your contribution history will appear here once contributions are recorded.</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
