@php $canManageForms = auth()->user()->isSuperAdmin(); @endphp

<x-app-layout>
    <x-ui.page-header title="Gestion des formulaires" description="{{ $canManageForms ? 'Créer, modifier, assigner et consulter les formulaires' : 'Consulter, verrouiller et gérer les échéances' }}">
        @if($canManageForms)
            <x-slot name="actions">
                <a href="{{ route('admin.forms.create') }}" class="btn-primary">
                    <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 4v16m8-8H4"/></svg>
                    Nouveau formulaire
                </a>
            </x-slot>
        @endif
    </x-ui.page-header>

    <x-alert />

    <div class="card overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full table-modern">
                <thead>
                    <tr>
                        <th>Titre</th>
                        <th>Champs</th>
                        <th>Filiales</th>
                        <th>Statut</th>
                        <th class="text-right">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($forms as $form)
                        <tr>
                            <td class="font-medium text-slate-800">{{ $form->title }}</td>
                            <td>{{ $form->fields_count }}</td>
                            <td>{{ $form->filiales_count }}</td>
                            <td>
                                @if($form->is_active)
                                    <span class="badge-success">Modifiable</span>
                                @else
                                    <span class="badge-danger">Verrouillé</span>
                                @endif
                            </td>
                            <td>
                                <div class="flex items-center justify-end gap-1">
                                    <form method="POST" action="{{ route('admin.forms.lock', $form) }}" class="inline">
                                        @csrf @method('PATCH')
                                        @if($form->is_active)
                                            <button type="submit" class="p-2 rounded-lg text-slate-500 hover:bg-red-50 hover:text-red-600 transition" title="Verrouiller les modifications"
                                                    onclick="return confirm('Désactiver les modifications pour ce formulaire ?')">
                                                <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 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/></svg>
                                            </button>
                                        @else
                                            <button type="submit" class="p-2 rounded-lg text-slate-500 hover:bg-emerald-50 hover:text-emerald-600 transition" title="Réautoriser les modifications">
                                                <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 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2 2v6a2 2 0 002 2z"/></svg>
                                            </button>
                                        @endif
                                    </form>
                                    @if($canManageForms)
                                        <x-ui.crud-actions
                                            :view="route('admin.forms.show', $form)"
                                            :edit="route('admin.forms.edit', $form)"
                                            :delete="route('admin.forms.destroy', $form)"
                                        >
                                            <a href="{{ route('admin.forms.assign', $form) }}" class="p-2 rounded-lg text-slate-500 hover:bg-emerald-50 hover:text-emerald-600 transition" title="Assigner">
                                                <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 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"/></svg>
                                            </a>
                                            <a href="{{ route('admin.forms.submissions', $form) }}" class="p-2 rounded-lg text-slate-500 hover:bg-blue-50 hover:text-blue-600 transition" title="Soumissions">
                                                <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 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/></svg>
                                            </a>
                                        </x-ui.crud-actions>
                                    @else
                                        <x-ui.crud-actions :view="route('admin.forms.show', $form)">
                                            <a href="{{ route('admin.forms.submissions', $form) }}" class="p-2 rounded-lg text-slate-500 hover:bg-blue-50 hover:text-blue-600 transition" title="Soumissions">
                                                <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 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/></svg>
                                            </a>
                                        </x-ui.crud-actions>
                                    @endif
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center py-12 text-slate-400">Aucun formulaire.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($forms->hasPages())
            <div class="px-6 py-4 border-t border-slate-100">{{ $forms->links() }}</div>
        @endif
    </div>
</x-app-layout>
