@extends('layouts.app')

@section('content')
<div class="bg-gray-50 min-h-screen">

    <!-- 🔷 HEADER BG -->
    <div class="h-48 md:h-64 bg-gradient-to-r from-blue-900 via-blue-600 to-blue-400 w-full absolute top-0 left-0 z-0"></div>

    <div class="container max-w-7xl mx-auto px-4 py-8 relative z-10">

        <!-- HEADER -->
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
            <div class="text-white">
                <h1 class="text-2xl md:text-3xl font-bold">💬 Pesan Masuk</h1>
                <p class="text-blue-100 text-sm">Kelola pesan dari pengguna</p>
            </div>

            <div class="flex flex-col sm:flex-row gap-2 w-full md:w-auto">

                <!-- SEARCH -->
                <form action="{{ route('admin.pesan.index') }}" method="GET" class="relative w-full sm:w-64">
                    <input type="text" name="search" value="{{ request('search') }}"
                        placeholder="Cari..."
                        class="w-full px-4 py-2 rounded-xl border border-gray-300 text-sm">
                </form>

                <!-- BACK -->
                <a href="{{ route('admin.dashboard') }}"
                    class="px-4 py-2 rounded-xl bg-white/10 text-white border border-white/20 hover:bg-white/20 text-center">
                    Kembali
                </a>

            </div>
        </div>

        <!-- ALERT -->
        @if(session('success'))
        <div class="mb-6 p-4 rounded-xl bg-green-50 border-l-4 border-green-500 text-green-700">
            {{ session('success') }}
        </div>
        @endif

        <!-- CARD -->
        <div class="bg-white rounded-2xl shadow-sm border overflow-hidden">

            <!-- DESKTOP TABLE -->
            <div class="hidden md:block overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 text-xs uppercase text-gray-500">
                        <tr>
                            <th class="px-5 py-3 text-center">No</th>
                            <th class="px-5 py-3">Pengirim</th>
                            <th class="px-5 py-3">Pesan</th>
                            <th class="px-5 py-3">Tanggal</th>
                            <th class="px-5 py-3 text-center">Aksi</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y">
                        @forelse($pesan as $index => $item)
                        <tr class="hover:bg-gray-50">

                            <td class="px-5 py-4 text-center">
                                {{ $pesan->firstItem() + $index }}
                            </td>

                            <!-- USER -->
                            <td class="px-5 py-4">
                                <p class="font-bold text-gray-800">{{ $item->nama }}</p>
                                <p class="text-xs text-blue-500">{{ $item->email }}</p>
                            </td>

                            <!-- PESAN -->
                            <td class="px-5 py-4 text-sm text-gray-600">
                                {{ Str::limit($item->pesan, 80) }}
                            </td>

                            <!-- TANGGAL -->
                            <td class="px-5 py-4 text-xs">
                                {{ $item->created_at->format('d M Y H:i') }}
                            </td>

                            <!-- AKSI -->
                            <td class="px-5 py-4 text-center">
                                <div class="flex justify-center gap-2">

                                    <button onclick="alert('{{ addslashes($item->pesan) }}')"
                                        style="background:#14b8a6;color:white;padding:5px 10px;border-radius:999px;font-size:11px;">
                                        Baca
                                    </button>

                                    <form action="{{ route('admin.pesan.destroy', $item->id) }}" method="POST"
                                        onsubmit="return confirm('Hapus pesan?');">
                                        @csrf
                                        @method('DELETE')

                                        <button type="submit"
                                            style="background:#f43f5e;color:white;padding:5px 10px;border-radius:999px;font-size:11px;">
                                            Hapus
                                        </button>
                                    </form>

                                </div>
                            </td>

                        </tr>
                        @empty
                        <tr>
                            <td colspan="5" class="text-center py-10 text-gray-500">
                                Tidak ada pesan
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- 📱 MOBILE -->
            <div class="md:hidden p-4 space-y-4">
                @forelse($pesan as $item)
                <div class="border rounded-xl p-4 shadow-sm">

                    <p class="font-bold text-gray-800">{{ $item->nama }}</p>
                    <p class="text-xs text-blue-500">{{ $item->email }}</p>

                    <p class="text-sm text-gray-600 mt-2">
                        {{ Str::limit($item->pesan, 100) }}
                    </p>

                    <div class="mt-3 flex justify-between items-center">

                        <span class="text-xs text-gray-400">
                            {{ $item->created_at->format('d M H:i') }}
                        </span>

                        <div class="flex gap-2">
                            <button onclick="alert('{{ addslashes($item->pesan) }}')"
                                style="background:#14b8a6;color:white;padding:4px 8px;border-radius:999px;font-size:10px;">
                                Baca
                            </button>

                            <form action="{{ route('admin.pesan.destroy', $item->id) }}" method="POST">
                                @csrf
                                @method('DELETE')
                                <button type="submit"
                                    style="background:#f43f5e;color:white;padding:4px 8px;border-radius:999px;font-size:10px;">
                                    Hapus
                                </button>
                            </form>
                        </div>

                    </div>

                </div>
                @empty
                <div class="text-center text-gray-500 py-10">
                    Tidak ada pesan
                </div>
                @endforelse
            </div>

            <!-- PAGINATION -->
            @if($pesan->hasPages())
            <div class="p-4 border-t">
                {{ $pesan->links() }}
            </div>
            @endif

        </div>

    </div>
</div>
@endsection