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

    <x-module-hero title="Renewal History" description="View all your past membership renewals" icon="fa-clock-rotate-left">
        <x-slot name="tabs">
            <a href="{{ route('member.dashboard') }}" class="module-hero-tab">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            @can('view membership certificate')
                <a href="{{ route('member.certificate.index') }}" class="module-hero-tab">
                    <i class="fas fa-certificate"></i> My Certificate
                </a>
            @endcan
            @can('renew membership')
                <a href="{{ route('member.renewal.settings') }}" class="module-hero-tab">
                    <i class="fas fa-rotate"></i> Renew Membership
                </a>
            @endcan
            @can('view renewal history')
                <a href="{{ route('member.renewal.index') }}" class="module-hero-tab active-tab">
                    <i class="fas fa-clock-rotate-left"></i> Renewal History
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">

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

        @if($renewals->count() > 0)
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
                    <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                        <i class="fas fa-history text-blue-600"></i> All Renewals
                    </h3>
                    <span class="text-sm text-gray-500">{{ $renewals->total() }} record{{ $renewals->total() !== 1 ? 's' : '' }}</span>
                </div>

                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Renewal Date</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Type</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Previous Expiry</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">New Expiry</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Certificate</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @foreach($renewals as $renewal)
                                <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 whitespace-nowrap text-sm text-gray-900">
                                        {{ $renewal->renewed_at->format('M j, Y') }}
                                        <div class="text-xs text-gray-400">{{ $renewal->renewed_at->format('g:i A') }}</div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        @if($renewal->renewal_type === 'automatic')
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                                <i class="fas fa-robot mr-1"></i> Automatic
                                            </span>
                                        @else
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                                <i class="fas fa-hand-pointer mr-1"></i> Manual
                                            </span>
                                        @endif
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">
                                        {{ $renewal->previous_end_date?->format('M j, Y') ?? '—' }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                        {{ $renewal->new_end_date->format('M j, Y') }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        @if($renewal->certificate && $renewal->certificate->pdf_path)
                                            <a href="{{ route('member.certificate.download', $renewal->certificate) }}"
                                               class="inline-flex items-center gap-1.5 text-blue-600 hover:text-blue-800 font-medium transition">
                                                <i class="fas fa-download text-xs"></i>
                                                {{ $renewal->certificate->certificate_number }}
                                            </a>
                                        @elseif($renewal->certificate)
                                            <span class="text-gray-400 text-xs">{{ $renewal->certificate->certificate_number }}</span>
                                        @else
                                            <span class="text-gray-400 text-xs">—</span>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                @if($renewals->hasPages())
                    <div class="px-6 py-4 border-t border-gray-200">
                        {{ $renewals->links() }}
                    </div>
                @endif
            </div>

        @else
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-12 text-center">
                <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-clock-rotate-left text-gray-400 text-2xl"></i>
                </div>
                <h3 class="text-lg font-semibold text-gray-700 mb-2">No Renewal History</h3>
                <p class="text-gray-500 text-sm mb-6">Your renewal history will appear here once you renew your membership.</p>
                @can('renew membership')
                    <a href="{{ route('member.renewal.settings') }}"
                       class="inline-flex items-center gap-2 px-5 py-2.5 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition">
                        <i class="fas fa-rotate"></i> Renew Membership
                    </a>
                @endcan
            </div>
        @endif

    </div>

</x-app-layout>
