{{-- Member Certificate Page --}}
<x-app-layout title="My Certificate">

    <x-module-hero title="My Membership Certificate" description="Download and manage your membership certificate" icon="fa-certificate">
        <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 active-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">
                    <i class="fas fa-clock-rotate-left"></i> Renewal History
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6 max-w-3xl mx-auto space-y-6">

        {{-- Flash messages --}}
        @if(session('success'))
            <div class="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($errors->any())
            <div class="p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-3">
                <i class="fas fa-exclamation-circle flex-shrink-0"></i> {{ $errors->first() }}
            </div>
        @endif

        {{-- Renewal reminder banner --}}
        @if($showRenewalReminder)
            <div class="p-4 bg-yellow-50 border border-yellow-300 text-yellow-800 rounded-lg flex items-start gap-3">
                <i class="fas fa-exclamation-triangle mt-0.5 flex-shrink-0 text-yellow-500"></i>
                <div>
                    <p class="font-semibold text-sm">Your membership is expiring soon</p>
                    <p class="text-sm mt-0.5">
                        Your membership expires on
                        <strong>{{ $member->membership_end_date?->format('F j, Y') }}</strong>.
                        @can('renew membership')
                            <a href="{{ route('member.renewal.settings') }}" class="underline font-medium">Renew now</a> to keep your membership active.
                        @endcan
                    </p>
                </div>
            </div>
        @endif

        {{-- Certificate card --}}
        @if($certificate)
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                {{-- Header --}}
                <div class="px-6 py-5 border-b border-gray-100 flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center">
                            <i class="fas fa-certificate text-blue-600"></i>
                        </div>
                        <div>
                            <h2 class="text-base font-semibold text-gray-900">Current Certificate</h2>
                            <p class="text-xs text-gray-500">Active membership certificate</p>
                        </div>
                    </div>
                    <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-circle text-green-500 mr-1" style="font-size:6px"></i> Active
                    </span>
                </div>

                {{-- Details --}}
                <div class="p-6 grid grid-cols-1 sm:grid-cols-2 gap-5">
                    <div>
                        <p class="text-xs text-gray-500 mb-1">Certificate Number</p>
                        <p class="font-mono text-sm font-semibold text-gray-900">{{ $certificate->certificate_number }}</p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 mb-1">Member Name</p>
                        <p class="text-sm font-medium text-gray-900">{{ $member->full_name }}</p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 mb-1">Issue Date</p>
                        <p class="text-sm text-gray-900">{{ $certificate->issue_date->format('F j, Y') }}</p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 mb-1">Valid Till</p>
                        <p class="text-sm font-semibold {{ $certificate->valid_till_date->isPast() ? 'text-red-600' : 'text-gray-900' }}">
                            {{ $certificate->valid_till_date->format('F j, Y') }}
                            @if($certificate->valid_till_date->isPast())
                                <span class="text-xs font-normal text-red-500 ml-1">(Expired)</span>
                            @endif
                        </p>
                    </div>
                    @if($member->primary_category)
                        <div>
                            <p class="text-xs text-gray-500 mb-1">Membership Category</p>
                            <p class="text-sm text-gray-900">{{ $member->primary_category->name }}</p>
                        </div>
                    @endif
                    <div>
                        <p class="text-xs text-gray-500 mb-1">Membership Code</p>
                        <p class="font-mono text-sm text-gray-900">{{ $member->membership_code }}</p>
                    </div>
                </div>

                {{-- Actions --}}
                <div class="px-6 py-4 bg-gray-50 border-t border-gray-100 flex flex-wrap gap-3">
                    @if($certificate->pdf_path)
                        <a href="{{ route('member.certificate.download', $certificate) }}"
                           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-download"></i> Download PDF
                        </a>
                    @endif
                    <form method="POST" action="{{ route('member.certificate.regenerate') }}"
                          onsubmit="return confirm('Regenerate your certificate? A new PDF will be emailed to you.')">
                        @csrf
                        <button type="submit"
                                class="inline-flex items-center gap-2 px-5 py-2.5 bg-gray-100 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-200 transition border border-gray-300">
                            <i class="fas fa-rotate"></i> Regenerate Certificate
                        </button>
                    </form>
                </div>
            </div>

        @else
            {{-- No certificate state --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-10 text-center">
                <div class="w-16 h-16 bg-yellow-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-hourglass-half text-yellow-500 text-2xl"></i>
                </div>
                <h3 class="text-lg font-semibold text-gray-800 mb-2">Certificate Being Processed</h3>
                <p class="text-gray-500 text-sm mb-6 max-w-sm mx-auto">
                    Your membership certificate is being prepared. If it doesn't arrive within a few minutes,
                    you can request a new one below.
                </p>
                <form method="POST" action="{{ route('member.certificate.regenerate') }}">
                    @csrf
                    <button type="submit"
                            class="inline-flex items-center gap-2 px-6 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> Request Certificate
                    </button>
                </form>
            </div>
        @endif

    </div>

</x-app-layout>
