@extends('layouts.admin')

@section('title', 'Messages de contact')
@section('page-title', 'Messages de contact')

@section('content')

<div class="flex flex-wrap gap-2 mb-6">
    @foreach(['all' => 'Tous', 'unread' => 'Non lus', 'read' => 'Lus'] as $val => $lbl)
    <a href="{{ route('admin.contacts.index', ['filter' => $val]) }}"
       class="px-4 py-2 rounded-xl text-xs font-medium transition-all {{ $filter === $val ? 'bg-primary text-white shadow-sm' : 'bg-white text-gray-600 hover:bg-gray-50 border border-gray-200' }}">
        {{ $lbl }}
    </a>
    @endforeach
</div>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="divide-y divide-gray-50">
        @forelse($contacts as $contact)
        <div class="flex items-start gap-4 px-5 py-4 hover:bg-gray-50 transition-colors {{ !$contact->is_read ? 'bg-blue-50/30' : '' }}">
            <div class="w-10 h-10 {{ !$contact->is_read ? 'bg-pink-500' : 'bg-gray-200' }} rounded-full flex items-center justify-center flex-shrink-0 mt-0.5">
                <span class="text-sm font-bold {{ !$contact->is_read ? 'text-white' : 'text-gray-600' }}">
                    {{ strtoupper(substr($contact->name, 0, 1)) }}
                </span>
            </div>
            <div class="flex-1 min-w-0">
                <div class="flex items-start justify-between gap-2">
                    <div>
                        <a href="{{ route('admin.contacts.show', $contact) }}"
                           class="font-semibold text-gray-800 hover:text-primary transition-colors text-sm">
                            {{ $contact->name }}
                            @if(!$contact->is_read)
                            <span class="ml-2 bg-blue-500 text-white text-xs px-2 py-0.5 rounded-full">Nouveau</span>
                            @endif
                        </a>
                        <p class="text-xs text-gray-500 mt-0.5">{{ $contact->email }} · {{ $contact->subject }}</p>
                    </div>
                    <div class="flex items-center gap-2 flex-shrink-0">
                        <span class="text-xs text-gray-400">{{ $contact->created_at->diffForHumans() }}</span>
                        <form action="{{ route('admin.contacts.destroy', $contact) }}" method="POST"
                              onsubmit="return confirm('Supprimer ce message ?')">
                            @csrf @method('DELETE')
                            <button type="submit" class="p-1.5 text-red-400 hover:bg-red-50 rounded-lg transition-colors">
                                <i class="fas fa-trash text-xs"></i>
                            </button>
                        </form>
                    </div>
                </div>
                <p class="text-sm text-gray-400 mt-1 truncate">{{ Str::limit($contact->message, 100) }}</p>
            </div>
        </div>
        @empty
        <div class="px-5 py-12 text-center text-gray-400">
            <i class="fas fa-envelope-open text-3xl mb-2 block"></i>
            Aucun message trouvé.
        </div>
        @endforelse
    </div>

    @if($contacts->hasPages())
    <div class="px-5 py-4 border-t border-gray-100">
        {{ $contacts->links() }}
    </div>
    @endif
</div>

@endsection
