@extends('layouts.app')

@section('title', 'Nos Réalisations')

@section('content')
@include('components.page-banner', [
'title' => 'Nos Réalisations',
'subtitle' => 'Découvrez nos projets et réalisations',
'image' => asset('img/test_6.jpeg')
])

<section class="py-20 bg-white">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        @if($realisations->isEmpty())
        <div class="text-center py-16">
            <div class="w-24 h-24 rounded-full bg-slate-100 flex items-center justify-center mx-auto mb-6">
                <svg class="w-12 h-12 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" /></svg>
            </div>
            <h3 class="text-xl font-semibold text-slate-800 mb-2">Nos réalisations arrivent bientôt</h3>
            <p class="text-slate-600 max-w-md mx-auto">Nous mettons à jour notre portfolio. Revenez prochainement pour découvrir nos projets.</p>
        </div>
        @else
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
            @foreach($realisations as $realisation)
            <article class="group rounded-2xl overflow-hidden bg-slate-50 border border-slate-100 hover:shadow-xl transition-all duration-300 flex flex-col">
                <div class="aspect-video overflow-hidden">
                    @if($realisation->image)
                    <img src="{{ asset('storage/' . $realisation->image) }}" alt="{{ $realisation->lieu ?? $realisation->titre }}" class="w-full h-full object-cover group-hover:scale-105 transition duration-500">
                    @else
                    <img src="https://images.unsplash.com/photo-1621905251189-08b45d6a269e?w=600&q=80" alt="{{ $realisation->lieu ?? $realisation->titre }}" class="w-full h-full object-cover group-hover:scale-105 transition duration-500">
                    @endif
                </div>
                <div class="p-6 flex flex-col flex-1">
                    @if($realisation->type_travaux ?? $realisation->categorie)
                    <span class="text-xs font-medium text-amber-600 uppercase tracking-wider">{{ $realisation->type_travaux ?? $realisation->categorie }}</span>
                    @endif
                    <h3 class="font-bold text-slate-800 mt-2 mb-2">{{ $realisation->lieu ?? $realisation->titre }}</h3>
                    @if($realisation->duree_travaux || $realisation->client)
                    <div class="flex flex-wrap gap-3 text-xs text-slate-500 mb-2">
                        @if($realisation->duree_travaux)
                        <span class="flex items-center gap-1">{{ $realisation->duree_travaux }}</span>
                        @endif
                        @if($realisation->client)
                        <span class="flex items-center gap-1">{{ $realisation->client }}</span>
                        @endif
                    </div>
                    @endif
                    @if($realisation->description)
                    <p class="text-slate-600 text-sm line-clamp-2 flex-1">{{ Str::limit($realisation->description, 100) }}</p>
                    @endif
                    @php
                        $modalData = [
                            'titre' => $realisation->titre,
                            'lieu' => $realisation->lieu ?? null,
                            'categorie' => $realisation->categorie ?? null,
                            'type_travaux' => $realisation->type_travaux ?? null,
                            'description' => $realisation->description ?? null,
                            'client' => $realisation->client ?? null,
                            'duree_travaux' => $realisation->duree_travaux ?? null,
                            'date_realisation' => $realisation->date_realisation ? $realisation->date_realisation->format('d/m/Y') : null,
                            'image' => $realisation->image ? asset('storage/' . $realisation->image) : 'https://images.unsplash.com/photo-1621905251189-08b45d6a269e?w=600&q=80',
                        ];
                    @endphp
                    <button type="button"
                        class="voir-plus-btn mt-4 inline-flex items-center gap-2 text-amber-600 hover:text-amber-700 font-medium text-sm transition-colors"
                        data-realisation='@json($modalData)'>
                        Voir plus
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
                        </svg>
                    </button>
                </div>
            </article>
            @endforeach
        </div>

        {{-- Pagination --}}
        @if($realisations->hasPages())
        <div class="mt-12 flex justify-center">
            {{ $realisations->links() }}
        </div>
        @endif
        @endif
    </div>
</section>

@push('modals')
{{-- Modal Voir plus - rendu en fin de body pour éviter les problèmes de positionnement --}}
<div id="realisation-modal" class="fixed inset-0 z-[9999] flex items-center justify-center p-4 pt-24" style="display: none;" aria-hidden="true">
    <div id="modal-backdrop" class="absolute inset-0 bg-slate-900/80 backdrop-blur-sm cursor-pointer"></div>
    <div class="absolute inset-0 flex items-center justify-center p-4 pt-24 pb-8 overflow-y-auto pointer-events-none">
        <div class="relative bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[85vh] overflow-hidden pointer-events-auto" role="dialog" aria-modal="true" aria-labelledby="modal-title">
            <button type="button" id="modal-close" class="absolute top-4 right-4 z-10 w-10 h-10 rounded-full bg-white/90 hover:bg-white shadow-lg flex items-center justify-center text-slate-600 hover:text-slate-800 transition" aria-label="Fermer">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
            </button>
            <div class="overflow-y-auto max-h-[90vh]">
                <div id="modal-image" class="aspect-video bg-slate-200 shrink-0">
                    <img src="" alt="" class="w-full h-full object-cover">
                </div>
                <div class="p-6 sm:p-8">
                    <div id="modal-badge" class="text-xs font-medium text-amber-600 uppercase tracking-wider mb-2"></div>
                    <h2 id="modal-title" class="text-2xl font-bold text-slate-800 mb-4"></h2>
                    <div id="modal-meta" class="flex flex-wrap gap-4 text-sm text-slate-500 mb-4"></div>
                    <div id="modal-description" class="text-slate-600 leading-relaxed prose prose-slate max-w-none"></div>
                </div>
            </div>
        </div>
    </div>
</div>
@endpush

@push('scripts')
<script>
(function() {
    const modal = document.getElementById('realisation-modal');
    const backdrop = document.getElementById('modal-backdrop');
    const closeBtn = document.getElementById('modal-close');
    if (!modal || !backdrop || !closeBtn) return;
    const modalTitle = document.getElementById('modal-title');
    const modalBadge = document.getElementById('modal-badge');
    const modalMeta = document.getElementById('modal-meta');
    const modalDescription = document.getElementById('modal-description');
    const modalImage = document.querySelector('#modal-image img');

    function openModal(data) {
        const titre = data.lieu || data.titre || 'Réalisation';
        const badge = data.type_travaux || data.categorie || '';
        modalTitle.textContent = titre;
        modalBadge.textContent = badge;
        modalBadge.style.display = badge ? 'block' : 'none';
        modalImage.src = data.image || '';
        modalImage.alt = titre;

        let metaHtml = '';
        if (data.client) metaHtml += '<span class="flex items-center gap-1"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/></svg> ' + escapeHtml(data.client) + '</span>';
        if (data.duree_travaux) metaHtml += '<span class="flex items-center gap-1"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg> ' + escapeHtml(data.duree_travaux) + '</span>';
        if (data.date_realisation) metaHtml += '<span class="flex items-center gap-1"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg> ' + escapeHtml(data.date_realisation) + '</span>';
        modalMeta.innerHTML = metaHtml || '<span class="text-slate-400">—</span>';
        modalDescription.innerHTML = data.description ? '<p>' + escapeHtml(data.description).replace(/\n/g, '</p><p>') + '</p>' : '<p class="text-slate-400">Aucune description disponible.</p>';

        modal.style.display = 'flex';
        document.body.style.overflow = 'hidden';
    }

    function closeModal() {
        modal.style.display = 'none';
        document.body.style.overflow = '';
    }

    function escapeHtml(text) {
        const div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }

    document.querySelectorAll('.voir-plus-btn').forEach(function(btn) {
        btn.addEventListener('click', function() {
            try {
                const data = JSON.parse(this.getAttribute('data-realisation'));
                openModal(data);
            } catch (e) { console.error(e); }
        });
    });

    if (closeBtn) closeBtn.addEventListener('click', closeModal);
    if (backdrop) backdrop.addEventListener('click', closeModal);
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && modal.style.display === 'flex') closeModal();
    });
})();
</script>
@endpush
@endsection
