{{-- Member Renewal Settings --}}
<x-app-layout title="Renew Membership">

    <x-module-hero title="Renew Membership" description="Manage your renewal preference and renew your membership" icon="fa-rotate">
        <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 active-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-5xl 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

        {{-- Membership status card --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-base font-semibold text-gray-900 mb-4 flex items-center gap-2">
                <i class="fas fa-id-card text-blue-600"></i> Membership Status
            </h3>
            <div class="grid grid-cols-2 gap-4 text-sm">
                <div>
                    <p class="text-xs text-gray-500 mb-1">Member Since</p>
                    <p class="font-medium text-gray-900">{{ $member->membership_start_date?->format('F j, Y') ?? '—' }}</p>
                </div>
                <div>
                    <p class="text-xs text-gray-500 mb-1">Expires On</p>
                    <p class="font-medium {{ $member->membership_end_date?->isPast() ? 'text-red-600' : 'text-gray-900' }}">
                        {{ $member->membership_end_date?->format('F j, Y') ?? '—' }}
                        @if($member->membership_end_date?->isPast())
                            <span class="text-xs font-normal text-red-500 ml-1">(Expired)</span>
                        @endif
                    </p>
                </div>
                <div>
                    <p class="text-xs text-gray-500 mb-1">Status</p>
                    @php
                        $statusColors = ['active' => 'bg-green-100 text-green-800', 'suspended' => 'bg-red-100 text-red-800'];
                        $statusClass  = $statusColors[$member->status] ?? 'bg-gray-100 text-gray-700';
                    @endphp
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $statusClass }}">
                        {{ ucfirst($member->status) }}
                    </span>
                </div>
                <div>
                    <p class="text-xs text-gray-500 mb-1">Current Preference</p>
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
                        {{ $member->renewal_type === 'automatic' ? 'bg-blue-100 text-blue-800' : 'bg-gray-100 text-gray-700' }}">
                        {{ $member->renewal_type === 'automatic' ? 'Automatic' : 'Manual' }}
                    </span>
                </div>
            </div>
        </div>

        {{-- Renewal preference form --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-100">
                <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                    <i class="fas fa-sliders text-blue-600"></i> Renewal Preference
                </h3>
                <p class="text-sm text-gray-500 mt-1">Choose how your membership renews when it expires.</p>
            </div>
            <form method="POST" action="{{ route('member.renewal.settings.update') }}" class="p-6 space-y-4">
                @csrf @method('PUT')

                {{-- Manual option --}}
                <label class="flex items-start gap-4 p-4 border-2 rounded-xl cursor-pointer transition
                    {{ $member->renewal_type === 'manual' ? 'border-blue-500 bg-blue-50' : 'border-gray-200 hover:border-gray-300' }}"
                    id="label-manual">
                    <input type="radio" name="renewal_type" value="manual"
                           {{ $member->renewal_type === 'manual' ? 'checked' : '' }}
                           class="mt-1 text-blue-600 focus:ring-blue-500"
                           onchange="highlightSelected(this)">
                    <div>
                        <p class="font-semibold text-gray-900 text-sm">Manual Renewal</p>
                        <p class="text-xs text-gray-500 mt-0.5">
                            You will be notified when your membership is about to expire and must manually renew it
                            through the portal.
                        </p>
                    </div>
                </label>

                {{-- Automatic option --}}
                <label class="flex items-start gap-4 p-4 border-2 rounded-xl cursor-pointer transition
                    {{ $member->renewal_type === 'automatic' ? 'border-blue-500 bg-blue-50' : 'border-gray-200 hover:border-gray-300' }}"
                    id="label-automatic">
                    <input type="radio" name="renewal_type" value="automatic"
                           {{ $member->renewal_type === 'automatic' ? 'checked' : '' }}
                           class="mt-1 text-blue-600 focus:ring-blue-500"
                           onchange="highlightSelected(this)">
                    <div>
                        <p class="font-semibold text-gray-900 text-sm">Automatic Renewal</p>
                        <p class="text-xs text-gray-500 mt-0.5">
                            Your membership will be renewed automatically on the expiry date. A new certificate will
                            be generated and emailed to you.
                        </p>
                    </div>
                </label>

                <div class="pt-2">
                    <button type="submit"
                            class="w-full sm:w-auto inline-flex items-center justify-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-save"></i> Save Preference
                    </button>
                </div>
            </form>
        </div>

        {{-- Manual renew now card --}}
        @if($member->isActive())
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-100">
                    <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                        <i class="fas fa-rotate text-green-600"></i> Renew Now
                    </h3>
                    <p class="text-sm text-gray-500 mt-1">
                        Manually renew your membership immediately. A new certificate will be generated and emailed to you.
                    </p>
                </div>
                <div class="px-6 py-5">
                    <form method="POST" action="{{ route('member.renewal.store') }}"
                          onsubmit="return confirm('Renew your membership now? A new certificate will be generated and sent to your email.')">
                        @csrf
                        <button type="submit"
                                class="inline-flex items-center gap-2 px-6 py-2.5 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition">
                            <i class="fas fa-rotate"></i> Renew Membership Now
                        </button>
                    </form>
                </div>
            </div>
        @endif

    </div>

    @push('scripts')
    <script>
        function highlightSelected(radio) {
            document.getElementById('label-manual').classList.remove('border-blue-500', 'bg-blue-50');
            document.getElementById('label-manual').classList.add('border-gray-200');
            document.getElementById('label-automatic').classList.remove('border-blue-500', 'bg-blue-50');
            document.getElementById('label-automatic').classList.add('border-gray-200');

            const label = document.getElementById('label-' + radio.value);
            label.classList.remove('border-gray-200');
            label.classList.add('border-blue-500', 'bg-blue-50');
        }
    </script>
    @endpush

</x-app-layout>
