<x-app-layout>

<x-module-hero
    title="My Certificates"
    description="View and download your earned training certificates"
    icon="fa-award">

    <x-slot name="tabs">
        @can('view training packages')
            <a href="{{ route('training.packages.index') }}" class="module-hero-tab">
                <i class="fas fa-box"></i> Packages
            </a>
        @endcan
        @can('view training sessions')
            <a href="{{ route('training.sessions.index') }}" class="module-hero-tab">
                <i class="fas fa-calendar-alt"></i> Sessions
            </a>
        @endcan
        <a href="{{ route('training.registrations.my') }}" class="module-hero-tab">
            <i class="fas fa-user"></i> My Registrations
        </a>
        <span class="module-hero-tab active-tab">
            <i class="fas fa-certificate"></i> My Certificates
        </span>
        @can('view training certificates')
            <a href="{{ route('training.certificates.index') }}" class="module-hero-tab">
                <i class="fas fa-list"></i> All Certificates
            </a>
        @endcan
    </x-slot>

</x-module-hero>

<div class="p-6">

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

    @if($certificates->count() > 0)
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-5">
            @foreach($certificates as $certificate)
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden hover:shadow-md transition">
                    {{-- Certificate Header --}}
                    <div class="p-4" style="background-color: var(--theme-bg-sidebar);">
                        <div class="flex items-center justify-between">
                            <div class="flex items-center gap-3">
                                <i class="fas fa-certificate text-white text-xl"></i>
                                <div>
                                    <p class="text-white/70 text-xs">Certificate Number</p>
                                    <p class="text-white font-mono font-bold text-sm">{{ $certificate->certificate_number }}</p>
                                </div>
                            </div>
                            @if($certificate->is_revoked)
                                <span class="px-2 py-1 bg-red-500 text-white text-xs rounded-full">Revoked</span>
                            @else
                                <span class="px-2 py-1 bg-white text-green-700 text-xs rounded-full font-medium">Valid</span>
                            @endif
                        </div>
                    </div>

                    {{-- Certificate Body --}}
                    <div class="p-4">
                        <h3 class="font-semibold text-gray-800 text-sm mb-3">{{ $certificate->trainingPackage->name }}</h3>
                        <div class="space-y-1.5 text-xs text-gray-600">
                            <div class="flex items-center gap-2">
                                <i class="fas fa-calendar w-4 text-gray-400"></i>
                                <span>Issued: {{ $certificate->issue_date->format('M d, Y') }}</span>
                            </div>
                            <div class="flex items-center gap-2">
                                <i class="fas fa-file w-4 text-gray-400"></i>
                                <span>Format: {{ ucfirst($certificate->format_type) }}</span>
                            </div>
                            @if($certificate->verification_count > 0)
                                <div class="flex items-center gap-2">
                                    <i class="fas fa-eye w-4 text-gray-400"></i>
                                    <span>Verified {{ $certificate->verification_count }} time(s)</span>
                                </div>
                            @endif
                        </div>

                        {{-- Actions --}}
                        <div class="mt-4 flex gap-2">
                            <a href="{{ route('training.certificates.show', $certificate) }}"
                               class="flex-1 text-center px-3 py-2 bg-gray-100 text-gray-700 rounded-lg text-xs hover:bg-gray-200 transition">
                                <i class="fas fa-eye mr-1"></i> View
                            </a>
                            @unless($certificate->is_revoked)
                                <a href="{{ route('training.certificates.download', $certificate) }}"
                                   class="flex-1 text-center px-3 py-2 bg-green-600 text-white rounded-lg text-xs hover:bg-green-700 transition">
                                    <i class="fas fa-download mr-1"></i> Download
                                </a>
                            @endunless
                        </div>

                        {{-- Verification Link --}}
                        @unless($certificate->is_revoked)
                            <div class="mt-3 p-2 bg-gray-50 rounded-lg">
                                <p class="text-xs text-gray-500 mb-1">Verification Link:</p>
                                <div class="flex items-center gap-2">
                                    <input type="text" readonly
                                           value="{{ route('public.training.certificates.verify', $certificate->verification_code) }}"
                                           class="flex-1 text-xs bg-white px-2 py-1 border border-gray-200 rounded text-gray-600"
                                           id="verify-link-{{ $certificate->id }}">
                                    <button onclick="copyVerificationLink({{ $certificate->id }})"
                                            class="px-2 py-1 bg-gray-200 rounded hover:bg-gray-300 transition">
                                        <i class="fas fa-copy text-gray-600 text-xs"></i>
                                    </button>
                                </div>
                            </div>
                        @endunless
                    </div>
                </div>
            @endforeach
        </div>

        @if($certificates->hasPages())
            <div class="mt-6">{{ $certificates->links() }}</div>
        @endif
    @else
        <div class="text-center py-16 bg-white rounded-xl shadow-sm border border-gray-200">
            <i class="fas fa-certificate text-5xl text-gray-300 mb-4 block"></i>
            <h3 class="text-lg font-semibold text-gray-700 mb-2">No Certificates Yet</h3>
            <p class="text-gray-500 text-sm mb-6 max-w-md mx-auto">
                You haven't earned any certificates yet. Complete a training program to receive your certificate.
            </p>
            <a href="{{ route('training.registrations.my') }}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700 transition">
                <i class="fas fa-user-graduate"></i> View My Registrations
            </a>
        </div>
    @endif
</div>

</x-app-layout>

@push('scripts')
<script>
    function copyVerificationLink(certificateId) {
        const input = document.getElementById('verify-link-' + certificateId);
        input.select();
        document.execCommand('copy');
        const button = input.nextElementSibling;
        const originalIcon = button.innerHTML;
        button.innerHTML = '<i class="fas fa-check text-green-600 text-xs"></i>';
        setTimeout(() => { button.innerHTML = originalIcon; }, 2000);
    }
</script>
@endpush
