<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Daftar Santri') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <a href="{{ route('santris.create') }}" class="btn-primary mb-4 inline-block">Tambah Santri</a>
                    @if(session('success'))
                        <div class="alert-success mb-4">{{ session('success') }}</div>
                    @endif

                    <form action="{{ route('santris.index') }}" method="GET" class="mb-4">
                        <input type="text" name="search" placeholder="Cari nama santri..."
                            value="{{ request('search') }}" class="border px-3 py-2 rounded-lg">
                        <button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-lg">Cari</button>
                    </form>

                    <div class="overflow-x-auto">
                        <table class="table-auto w-full">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-4 py-2 text-left">No</th>
                                    <th class="px-4 py-2 text-left">Foto</th>
                                    <th class="px-4 py-2 text-left">Nama</th>
                                    <th class="px-4 py-2 text-left">NIS</th>
                                    <th class="px-4 py-2 text-left">Kelas</th>
                                    <th class="px-4 py-2 text-left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($santris as $i => $santri)
                                    <tr class="border-b">
                                        <td class="px-4 py-2">{{ $i + 1 }}</td>
                                        <td class="px-4 py-2">
                                            @if($santri->foto)
                                                <img src="{{ asset('storage/' . $santri->foto) }}" alt="Foto" width="40"
                                                    height="40" class="rounded-full object-cover">
                                            @else
                                                -
                                            @endif
                                        </td>
                                        <td class="px-4 py-2">{{ $santri->nama }}</td>
                                        <td class="px-4 py-2">{{ $santri->nis }}</td>
                                        <td class="px-4 py-2">
                                            {{ $santri->kelas ? $santri->kelas->nama_kelas : 'Belum Ditetapkan' }}
                                        </td>
                                        <td class="px-4 py-2 space-x-1">
                                            <a href="{{ route('santris.show', $santri) }}"
                                                class="btn-info btn-sm p-2 rounded-md">Detail</a>
                                            <a href="{{ route('santris.edit', $santri) }}"
                                                class="btn-accent btn-sm p-2 rounded-md">Edit</a>
                                            <form action="{{ route('santris.destroy', $santri) }}" method="POST"
                                                class="inline-block" onsubmit="return confirm('Yakin hapus?')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit"
                                                    class="btn-danger btn-sm p-2 rounded-md">Delete</button>
                                            </form>
                                        </td>

                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>