<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-4">
                <div class="flex items-center">
                    <i class="fas fa-receipt text-white text-3xl mr-4"></i>
                    <div>
                        <h1 class="text-3xl font-bold text-white">Contribution Detail</h1>
                        <p class="text-green-100 mt-1">{{ $contribution->period ?? 'Contribution Record' }}</p>
                    </div>
                </div>
                <a href="{{ route('member.contributions') }}"
                   class="px-4 py-2 bg-white text-green-700 rounded-lg hover:bg-gray-50 transition font-medium">
                    <i class="fas fa-arrow-left mr-2"></i>Back to List
                </a>
            </div>
        </div>
    </div>

    {{-- Main Content --}}
    <div class="py-12">
        <div class="max-w-4xl 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

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

                {{-- Contribution Details --}}
                <div class="lg:col-span-2 space-y-6">
                    <div class="theme-card rounded-lg shadow-md overflow-hidden">
                        <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900 flex items-center">
                                <i class="fas fa-info-circle text-green-600 mr-3"></i>
                                Contribution Information
                            </h3>
                        </div>
                        <div class="p-6">
                            <dl class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Period</dt>
                                    <dd class="mt-1 text-sm text-gray-900 font-semibold">{{ $contribution->period ?? 'N/A' }}</dd>
                                </div>
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Membership Category</dt>
                                    <dd class="mt-1 text-sm text-gray-900">{{ $contribution->membershipCategory?->name ?? 'N/A' }}</dd>
                                </div>
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Amount</dt>
                                    <dd class="mt-1 text-sm text-gray-900 font-bold text-green-700">
                                        {{ number_format($contribution->amount, 2) }} {{ $contribution->currency ?? 'SSP' }}
                                    </dd>
                                </div>
                                @if($contribution->penalty_amount > 0)
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Penalty</dt>
                                    <dd class="mt-1 text-sm text-red-600 font-semibold">
                                        {{ number_format($contribution->penalty_amount, 2) }} {{ $contribution->currency ?? 'SSP' }}
                                    </dd>
                                </div>
                                @endif
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Due Date</dt>
                                    <dd class="mt-1 text-sm {{ $contribution->status === 'overdue' ? 'text-red-600 font-semibold' : 'text-gray-900' }}">
                                        {{ $contribution->due_date?->format('F d, Y') ?? 'N/A' }}
                                    </dd>
                                </div>
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Status</dt>
                                    <dd class="mt-1">
                                        @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>
                                    </dd>
                                </div>
                                @if($contribution->paid_date)
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Paid Date</dt>
                                    <dd class="mt-1 text-sm text-gray-900">{{ $contribution->paid_date->format('F d, Y') }}</dd>
                                </div>
                                @endif
                                @if($contribution->payment_reference)
                                <div>
                                    <dt class="text-sm font-medium text-gray-500">Payment Reference</dt>
                                    <dd class="mt-1 text-sm font-mono text-gray-900">{{ $contribution->payment_reference }}</dd>
                                </div>
                                @endif
                                @if($contribution->notes)
                                <div class="sm:col-span-2">
                                    <dt class="text-sm font-medium text-gray-500">Notes</dt>
                                    <dd class="mt-1 text-sm text-gray-900">{{ $contribution->notes }}</dd>
                                </div>
                                @endif
                            </dl>
                        </div>
                    </div>

                    {{-- Payment History --}}
                    @if(isset($payments) && $payments->count() > 0)
                    <div class="theme-card rounded-lg shadow-md overflow-hidden">
                        <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900 flex items-center">
                                <i class="fas fa-history text-green-600 mr-3"></i>
                                Payment History
                            </h3>
                        </div>
                        <div class="p-6">
                            <div class="space-y-3">
                                @foreach($payments as $payment)
                                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                                        <div>
                                            <div class="text-sm font-medium text-gray-900">{{ $payment->reference ?? 'Payment' }}</div>
                                            <div class="text-xs text-gray-500">{{ $payment->created_at?->format('M d, Y H:i') }}</div>
                                        </div>
                                        <div class="text-right">
                                            <div class="font-bold text-green-600">{{ number_format($payment->amount, 2) }}</div>
                                            <span class="text-xs px-2 py-0.5 rounded-full bg-green-100 text-green-700">{{ ucfirst($payment->status ?? 'completed') }}</span>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                    @endif
                </div>

                {{-- Sidebar Actions --}}
                <div class="space-y-6">
                    @if(in_array($contribution->status, ['pending', 'overdue']))
                    <div class="theme-card rounded-lg shadow-md overflow-hidden">
                        <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Actions</h3>
                        </div>
                        <div class="p-6">
                            <a href="{{ route('member.contributions.pay', $contribution) }}"
                               class="w-full block text-center px-4 py-3 bg-green-600 text-white rounded-lg hover:bg-green-700 transition font-medium">
                                <i class="fas fa-credit-card mr-2"></i>Pay Now
                            </a>
                        </div>
                    </div>
                    @endif

                    <div class="theme-card rounded-lg shadow-md overflow-hidden">
                        <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Summary</h3>
                        </div>
                        <div class="p-6 space-y-3 text-sm">
                            <div class="flex justify-between">
                                <span class="text-gray-500">Base Amount</span>
                                <span class="font-medium">{{ number_format($contribution->amount, 2) }}</span>
                            </div>
                            @if($contribution->penalty_amount > 0)
                            <div class="flex justify-between">
                                <span class="text-gray-500">Penalty</span>
                                <span class="font-medium text-red-600">{{ number_format($contribution->penalty_amount, 2) }}</span>
                            </div>
                            @endif
                            <div class="flex justify-between border-t pt-3 font-bold">
                                <span>Total Due</span>
                                <span class="text-green-700">{{ number_format($contribution->amount + ($contribution->penalty_amount ?? 0), 2) }} {{ $contribution->currency ?? 'SSP' }}</span>
                            </div>
                        </div>
                    </div>

                    <a href="{{ route('member.contributions') }}"
                       class="w-full block text-center px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition font-medium">
                        <i class="fas fa-arrow-left mr-2"></i>Back to Contributions
                    </a>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
