<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-3 sm:gap-4">
            <div class="min-w-0">
                <p class="text-xs font-black tracking-widest text-[#15db00] uppercase">Chat Dukungan</p>
                <h2 class="mt-1 font-black text-xl sm:text-2xl text-slate-900 tracking-tight leading-tight">
                    {{ __('Percakapan Pengguna') }}
                </h2>
            </div>
            @if ($openCount > 0)
                <span class="inline-flex items-center gap-2 px-3 py-1.5 sm:px-4 sm:py-2 bg-amber-50 border border-amber-200 text-amber-800 text-xs font-black rounded-xl shrink-0">
                    <span class="relative flex h-2.5 w-2.5">
                        <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-amber-400 opacity-75"></span>
                        <span class="relative inline-flex rounded-full h-2.5 w-2.5 bg-amber-500"></span>
                    </span>
                    {{ $openCount }} chat aktif
                </span>
            @endif
        </div>
    </x-slot>

    <div class="py-6 sm:py-10 bg-slate-100/70 min-h-screen">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-4 sm:space-y-6">

            @if (session('success'))
                <div class="bg-emerald-50 border border-emerald-200 text-emerald-800 px-4 py-3 rounded-xl flex items-center gap-3 shadow-sm" role="alert">
                    <svg class="w-5 h-5 text-emerald-500 shrink-0" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                    <span class="text-sm font-medium">{{ session('success') }}</span>
                </div>
            @endif

            <!-- Filter & Pencarian -->
            <form method="GET" action="{{ route('support.index') }}" class="flex flex-col sm:flex-row gap-2 sm:gap-3">
                <div class="flex-1 flex gap-2 sm:gap-3">
                    <input
                        type="text"
                        name="search"
                        value="{{ request('search') }}"
                        placeholder="Cari nama, WhatsApp, atau usaha..."
                        class="flex-1 px-3 py-2.5 sm:px-4 bg-white rounded-xl border border-slate-200 text-sm outline-none focus:border-[#15db00] focus:ring-4 focus:ring-[#15db00]/10"
                    >
                    <select
                        name="status"
                        class="px-3 py-2.5 sm:px-4 bg-white rounded-xl border border-slate-200 text-sm outline-none focus:border-[#15db00] focus:ring-4 focus:ring-[#15db00]/10"
                    >
                        <option value="" {{ request('status') === '' ? 'selected' : '' }}>Semua</option>
                        <option value="open" {{ request('status') === 'open' ? 'selected' : '' }}>Aktif</option>
                        <option value="replied" {{ request('status') === 'replied' ? 'selected' : '' }}>Dibalas</option>
                        <option value="closed" {{ request('status') === 'closed' ? 'selected' : '' }}>Selesai</option>
                    </select>
                </div>
                <button type="submit" class="inline-flex items-center justify-center gap-2 px-4 py-2.5 bg-slate-900 hover:bg-slate-700 text-white text-xs font-bold rounded-xl transition-all duration-200">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" />
                    </svg>
                    Filter
                </button>
                @if (request('search') || request('status'))
                    <a href="{{ route('support.index') }}" class="inline-flex items-center justify-center px-4 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-600 text-xs font-bold rounded-xl transition-colors">
                        Reset
                    </a>
                @endif
            </form>

            <!-- Daftar Percakapan -->
            <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
                <div class="divide-y divide-slate-100">
                    @forelse ($contacts as $contact)
                        @php
                            $lastMessage = $contact->messages->last();
                            $unreadCount = $contact->messages->where('sender', 'user')->whereNull('read_at')->count();
                        @endphp
                        <a href="{{ route('support.show', $contact->id) }}" class="flex items-center gap-3 sm:gap-4 px-4 py-3 sm:px-6 sm:py-4 hover:bg-slate-50/80 transition-colors group">
                            <!-- Avatar -->
                            <div class="relative shrink-0">
                                <span class="w-10 h-10 sm:w-12 sm:h-12 rounded-2xl bg-cashev-blue/10 text-cashev-blue flex items-center justify-center text-base sm:text-lg font-bold">
                                    {{ strtoupper(substr($contact->nama, 0, 1)) }}
                                </span>
                                @if ($unreadCount > 0)
                                    <span class="absolute -top-1 -right-1 w-4 h-4 sm:w-5 sm:h-5 bg-rose-500 text-white text-[9px] sm:text-[10px] font-black rounded-full flex items-center justify-center">{{ $unreadCount }}</span>
                                @endif
                            </div>
                            <!-- Info -->
                            <div class="flex-1 min-w-0">
                                <div class="flex items-center justify-between gap-2">
                                    <p class="font-bold text-slate-900 text-sm truncate">{{ $contact->nama }}</p>
                                    <p class="text-[10px] text-slate-400 whitespace-nowrap shrink-0">{{ $contact->created_at?->diffForHumans() }}</p>
                                </div>
                                <div class="flex items-center justify-between gap-2 mt-0.5">
                                    <p class="text-xs text-slate-500 truncate">{{ $lastMessage->message ?? 'Belum ada pesan' }}</p>
                                    @if ($contact->status === 'open')
                                        <span class="w-2.5 h-2.5 rounded-full bg-amber-400 shrink-0"></span>
                                    @elseif ($contact->status === 'replied')
                                        <span class="w-2.5 h-2.5 rounded-full bg-cashev-green shrink-0"></span>
                                    @else
                                        <span class="w-2.5 h-2.5 rounded-full bg-slate-300 shrink-0"></span>
                                    @endif
                                </div>
                            </div>
                        </a>
                    @empty
                        <div class="px-6 py-12 sm:py-16 text-center">
                            <span class="w-14 h-14 sm:w-16 sm:h-16 rounded-3xl bg-slate-100 text-slate-400 flex items-center justify-center text-2xl sm:text-3xl mx-auto mb-3">📭</span>
                            <p class="text-sm font-bold text-gray-900">Belum Ada Percakapan</p>
                            <p class="mt-1 text-xs text-gray-500">Percakapan dari pengguna akan muncul di sini.</p>
                        </div>
                    @endforelse
                </div>
            </div>

            <!-- Pagination -->
            <div class="flex flex-col sm:flex-row justify-between items-center gap-3 sm:gap-4">
                <p class="text-xs text-slate-500">
                    Menampilkan {{ $contacts->firstItem() ?? 0 }}–{{ $contacts->lastItem() ?? 0 }} dari {{ $contacts->total() }} percakapan
                </p>
                {{ $contacts->links() }}
            </div>

        </div>
    </div>
</x-app-layout>
