<x-app-layout>

<x-module-hero
    title="Certificate Details"
    description="View and verify certificate information"
    icon="fa-certificate">

    <x-slot name="backButton">
        <a href="{{ route('training.certificates.index') }}"
           class="inline-flex items-center gap-2 text-white/80 hover:text-white text-sm transition">
            <i class="fas fa-arrow-left"></i> Back to Certificates
        </a>
    </x-slot>

    <x-slot name="tabs">
        @can('view training certificates')
            <a href="{{ route('training.certificates.index') }}" class="module-hero-tab">
                <i class="fas fa-list"></i> All Certificates
            </a>
        @endcan
        <a href="{{ route('training.certificates.my') }}" class="module-hero-tab">
            <i class="fas fa-user"></i> My Certificates
        </a>
        <span class="module-hero-tab active-tab">
            <i class="fas fa-eye"></i> Details
        </span>
    </x-slot>

    <x-slot name="actions">
        @if($certificate->is_revoked)
            <span class="inline-flex items-center gap-2 px-4 py-2 bg-red-500 text-white text-sm font-medium rounded-lg">
                <i class="fas fa-exclamation-triangle"></i> Revoked
            </span>
        @else
            <span class="inline-flex items-center gap-2 px-4 py-2 bg-white text-green-700 text-sm font-medium rounded-lg">
                <i class="fas fa-check-circle"></i> Valid Certificate
            </span>
        @endif
    </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

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {{-- Certificate Preview --}}
        <div class="lg:col-span-2">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                {{-- Certificate Preview Area --}}
                <div class="bg-gradient-to-br from-green-50 to-green-100 p-6 border-b border-gray-200">
                    <div class="aspect-[1.414/1] bg-white rounded-lg shadow-lg p-8 flex flex-col items-center justify-center text-center border-4 border-green-600">
                        <div class="mb-5">
                            <i class="fas fa-award text-green-600 text-4xl mb-2 block"></i>
                            <h2 class="text-xl font-bold text-gray-800">Certificate of Completion</h2>
                        </div>
                        <div class="mb-5">
                            <p class="text-xs text-gray-500 uppercase tracking-wide">This is to certify that</p>
                            <h3 class="text-2xl font-bold text-green-700 my-2">{{ $certificate->user->name }}</h3>
                            <p class="text-xs text-gray-500">has successfully completed</p>
                        </div>
                        <div class="mb-5">
                            <h4 class="text-lg font-semibold text-gray-800">{{ $certificate->trainingPackage->name }}</h4>
                            @if($certificate->trainingPackage->duration_hours)
                                <p class="text-xs text-gray-500 mt-1">{{ $certificate->trainingPackage->duration_hours }} hours of training</p>
                            @endif
                        </div>
                        <div class="flex items-center justify-between w-full mt-auto pt-4 border-t border-gray-200">
                            <div class="text-left">
                                <p class="text-xs text-gray-500">Certificate Number</p>
                                <p class="font-mono text-xs text-gray-700">{{ $certificate->certificate_number }}</p>
                            </div>
                            <div class="text-right">
                                <p class="text-xs text-gray-500">Issue Date</p>
                                <p class="text-xs text-gray-700">{{ $certificate->issue_date->format('M d, Y') }}</p>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Actions --}}
                <div class="p-5 flex flex-wrap gap-3">
                    @unless($certificate->is_revoked)
                        @can('download training certificates')
                            <a href="{{ route('training.certificates.download', $certificate) }}"
                               class="flex-1 text-center px-4 py-2.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700 transition">
                                <i class="fas fa-download mr-1"></i> Download PDF
                            </a>
                        @endcan
                        <a href="{{ route('public.training.certificates.verify', $certificate->verification_code) }}" target="_blank"
                           class="flex-1 text-center px-4 py-2.5 bg-blue-600 text-white rounded-lg text-sm hover:bg-blue-700 transition">
                            <i class="fas fa-external-link-alt mr-1"></i> Public View
                        </a>
                    @endunless
                    @can('generate training certificates')
                        <form action="{{ route('training.certificates.regenerate', $certificate) }}" method="POST" class="inline">
                            @csrf
                            <button type="submit" class="px-4 py-2.5 bg-yellow-500 text-white rounded-lg text-sm hover:bg-yellow-600 transition">
                                <i class="fas fa-sync-alt mr-1"></i> Regenerate
                            </button>
                        </form>
                    @endcan
                </div>
            </div>
        </div>

        {{-- Sidebar --}}
        <div class="space-y-5">
            {{-- Certificate Details --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
                <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                    <i class="fas fa-info-circle text-green-600"></i> Certificate Information
                </h3>
                <dl class="space-y-3 text-sm">
                    <div>
                        <dt class="text-xs text-gray-500 uppercase tracking-wide">Certificate Number</dt>
                        <dd class="font-mono text-gray-900 mt-0.5">{{ $certificate->certificate_number }}</dd>
                    </div>
                    <div>
                        <dt class="text-xs text-gray-500 uppercase tracking-wide">Verification Code</dt>
                        <dd class="font-mono text-gray-900 mt-0.5">{{ $certificate->verification_code }}</dd>
                    </div>
                    <div>
                        <dt class="text-xs text-gray-500 uppercase tracking-wide">Issue Date</dt>
                        <dd class="text-gray-900 mt-0.5">{{ $certificate->issue_date->format('F d, Y') }}</dd>
                    </div>
                    <div>
                        <dt class="text-xs text-gray-500 uppercase tracking-wide">Format</dt>
                        <dd class="text-gray-900 mt-0.5">{{ ucfirst($certificate->format_type) }}</dd>
                    </div>
                    <div>
                        <dt class="text-xs text-gray-500 uppercase tracking-wide">Status</dt>
                        <dd class="mt-0.5">
                            @if($certificate->is_revoked)
                                <span class="px-2 py-1 bg-red-100 text-red-800 rounded-full text-xs">Revoked</span>
                            @else
                                <span class="px-2 py-1 bg-green-100 text-green-800 rounded-full text-xs">Valid</span>
                            @endif
                        </dd>
                    </div>
                    @if($certificate->verification_count > 0)
                        <div>
                            <dt class="text-xs text-gray-500 uppercase tracking-wide">Verification Count</dt>
                            <dd class="text-gray-900 mt-0.5">{{ $certificate->verification_count }}</dd>
                        </div>
                    @endif
                </dl>
            </div>

            {{-- Recipient --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
                <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                    <i class="fas fa-user text-green-600"></i> Recipient
                </h3>
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                        <i class="fas fa-user text-green-600"></i>
                    </div>
                    <div>
                        <p class="font-medium text-gray-900 text-sm">{{ $certificate->user->name }}</p>
                        <p class="text-xs text-gray-500">{{ $certificate->user->email }}</p>
                    </div>
                </div>
            </div>

            {{-- Training Package --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
                <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                    <i class="fas fa-book text-green-600"></i> Training Program
                </h3>
                <p class="font-medium text-gray-900 text-sm">{{ $certificate->trainingPackage->name }}</p>
                @if($certificate->trainingPackage->description)
                    <p class="text-xs text-gray-500 mt-1">{{ Str::limit($certificate->trainingPackage->description, 100) }}</p>
                @endif
                <a href="{{ route('training.packages.show', $certificate->trainingPackage) }}"
                   class="mt-2 inline-flex items-center gap-1 text-green-600 hover:text-green-700 text-xs">
                    <i class="fas fa-external-link-alt"></i> View Package
                </a>
            </div>

            {{-- Admin Actions --}}
            @can('revoke training certificates')
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
                    <h3 class="font-semibold text-gray-800 text-sm mb-4 flex items-center gap-2">
                        <i class="fas fa-cog text-green-600"></i> Admin Actions
                    </h3>
                    @if($certificate->is_revoked)
                        <form action="{{ route('training.certificates.restore', $certificate) }}" method="POST">
                            @csrf
                            <button type="submit" class="w-full px-4 py-2 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700 transition">
                                <i class="fas fa-undo mr-1"></i> Restore Certificate
                            </button>
                        </form>
                    @else
                        <button type="button" onclick="document.getElementById('revokeModal').classList.remove('hidden')"
                                class="w-full px-4 py-2 bg-red-600 text-white rounded-lg text-sm hover:bg-red-700 transition">
                            <i class="fas fa-ban mr-1"></i> Revoke Certificate
                        </button>
                    @endif
                </div>
            @endcan
        </div>
    </div>
</div>

</x-app-layout>

{{-- Revoke Modal --}}
@can('revoke training certificates')
    <div id="revokeModal" class="fixed inset-0 z-50 hidden overflow-y-auto">
        <div class="fixed inset-0 bg-black bg-opacity-50" onclick="document.getElementById('revokeModal').classList.add('hidden')"></div>
        <div class="relative min-h-screen flex items-center justify-center p-4">
            <div class="relative bg-white rounded-xl shadow-xl max-w-md w-full">
                <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-red-600 to-red-700 rounded-t-xl">
                    <h3 class="text-base font-semibold text-white flex items-center gap-2">
                        <i class="fas fa-ban"></i> Revoke Certificate
                    </h3>
                    <button type="button" onclick="document.getElementById('revokeModal').classList.add('hidden')"
                            class="text-white/80 hover:text-white transition"><i class="fas fa-times"></i></button>
                </div>
                <div class="p-6">
                    <p class="text-sm text-gray-600 mb-4">
                        Are you sure you want to revoke this certificate? This action will invalidate the certificate.
                    </p>
                    <form action="{{ route('training.certificates.revoke', $certificate) }}" method="POST">
                        @csrf
                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                Reason for Revocation <span class="text-red-500">*</span>
                            </label>
                            <textarea name="revoke_reason" rows="3" required
                                      class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500"
                                      placeholder="Please provide a reason..."></textarea>
                        </div>
                        <div class="flex justify-end gap-3">
                            <button type="button" onclick="document.getElementById('revokeModal').classList.add('hidden')"
                                    class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg text-sm hover:bg-gray-50 transition">
                                Cancel
                            </button>
                            <button type="submit" class="px-4 py-2 bg-red-600 text-white rounded-lg text-sm hover:bg-red-700 transition">
                                Revoke Certificate
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endcan
