@extends('layouts.admin')

@section('title', 'Data Buku')

@section('content')
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-2xl font-bold text-white bg-[#7488e1] px-4 py-1 rounded-lg inline-block shadow-sm">Data Buku</h1>
        </div>
    </div>

    @if (session('success'))
        <x-alert type="success" :message="session('success')" />
    @endif

    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">

        <div class="flex flex-col md:flex-row justify-between items-center mb-6 gap-4">
            <div class="flex gap-2">
                <a href="{{ route('admin.buku.create') }}"
                    class="px-4 py-2 bg-[#7488e1] text-white rounded-lg text-sm font-medium hover:bg-[#5f74d1] transition shadow-md shadow-blue-200">
                    + Tambah Buku
                </a>
            </div>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse">
                <thead>
                    <tr class="border-b border-gray-100">
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">No</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">Judul</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">Pengarang</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">Penerbit</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">Tahun</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider">Stok</th>
                        <th class="py-4 px-4 text-xs font-semibold text-gray-400 uppercase tracking-wider text-right">Aksi
                        </th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50 text-sm text-gray-600">
                    @forelse($bukus as $buku)
                        <tr class="hover:bg-gray-50 transition">
                            <td class="py-4 px-4 font-medium">
                                {{ $loop->iteration + ($bukus->currentPage() - 1) * $bukus->perPage() }}</td>
                            <td class="py-4 px-4 font-medium text-gray-800">{{ $buku->judul }}</td>
                            <td class="py-4 px-4">{{ $buku->pengarang }}</td>
                            <td class="py-4 px-4">{{ $buku->penerbit }}</td>
                            <td class="py-4 px-4">{{ $buku->tahun_terbit }}</td>
                            <td class="py-4 px-4">
                                <span
                                    class="px-2 py-1 rounded-full text-xs font-medium {{ $buku->stok > 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ $buku->stok }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <div class="flex justify-end gap-2">
                                    <a href="{{ route('admin.buku.edit', $buku->id_buku) }}"
                                        class="px-3 py-1 bg-yellow-100 text-yellow-700 rounded-lg text-xs font-medium hover:bg-yellow-200 transition">Edit</a>
                                    <form action="{{ route('admin.buku.destroy', $buku->id_buku) }}" method="POST"
                                        onsubmit="return confirm('Yakin hapus buku ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                            class="px-3 py-1 bg-red-100 text-red-700 rounded-lg text-xs font-medium hover:bg-red-200 transition">Hapus</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="py-8 px-4 text-center text-gray-400">Belum ada data buku</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="mt-6">
            {{ $bukus->links() }}
        </div>
    </div>
@endsection
