<x-app-layout>
    @section('page-title', $pageTitle)

    <div class="d-flex align-items-center mb-4">
        <a href="{{ route('admin.buku.index') }}" class="btn btn-outline-secondary me-3">
            <i class="bi bi-arrow-left"></i>
        </a>
        <h3 class="my-0 fw-bold">Formulir Tambah Buku</h3>
    </div>

    <div class="row justify-content-center">
        <div class="col-md-11">
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <div class="card-body">
                        <form action="{{ route('admin.buku.store') }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            <div class="row">
                                <div class="col-md-8">
                                    <div class="mb-3">
                                        <label for="judul" class="form-label">Judul Buku</label>
                                        <input type="text" name="judul" class="form-control" id="judul"
                                            placeholder="Masukkan judul buku" required>
                                    </div>
                                    <div class="mb-3">
                                        <label for="penulis" class="form-label">Penulis</label>
                                        <input type="text" name="penulis" class="form-control" id="penulis"
                                            placeholder="Masukkan nama penulis" required>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-4 mb-3">
                                            <label for="category_id" class="form-label">Kategori</label>
                                            <select name="category_id" class="form-select" id="category_id" required>
                                                <option value="" disabled selected>Pilih Kategori</option>
                                                @foreach($categories as $category)
                                                <option value="{{ $category->id }}">{{ $category->name }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="col-md-4 mb-3">
                                            <label for="tahun" class="form-label">Tahun Terbit</label>
                                            <input type="number" name="tahun" class="form-control" id="tahun"
                                                placeholder="Contoh: 2024" min="0" required>
                                        </div>
                                        <div class="col-md-4 mb-3">
                                            <label for="kode_buku" class="form-label">Kode Buku</label>
                                            <input type="text" name="kode_buku" class="form-control" id="kode_buku"
                                                placeholder="Contoh: 330">
                                        </div>
                                    </div>
                                </div>

                                {{-- TIPE AKSES --}}
                                <div class="mb-3">
                                    <label class="form-label">Tipe Akses</label>
                                    <div class="d-flex gap-3">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="tipe_akses[]" value="offline" id="tipe_offline">
                                            <label class="form-check-label" for="tipe_offline">Peminjaman
                                                Offline</label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="tipe_akses[]" value="online" id="tipe_online">
                                            <label class="form-check-label" for="tipe_online">Baca Online</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="mb-3">
                                        <label for="cover" class="form-label">Cover Buku</label>
                                        <input type="file" name="cover" class="form-control" id="cover">
                                    </div>
                                    <div class="mb-3">
                                        <label for="file_pdf" class="form-label">File PDF (untuk buku online)</label>
                                        <input type="file" name="file_pdf" class="form-control" id="file_pdf">
                                    </div>
                                </div>
                            </div>
                    </div>
                    <hr class="my-4">
                    <div class="d-flex justify-content-end">
                        <button type="submit" class="btn btn-primary px-4 fw-bold">
                            <i class="bi bi-save me-2"></i>Simpan Buku
                        </button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // LOGIC SIMPAN
            document.getElementById('formTambahBuku').addEventListener('submit', function(e) {
                e.preventDefault();
                modernSwal.fire({
                    title: 'Menyimpan...',
                    timer: 1500,
                    didOpen: () => Swal.showLoading()
                }).then(() => {
                    Toast.fire({
                        icon: 'success',
                        title: 'Berhasil',
                        text: 'Buku baru berhasil ditambahkan.'
                    });
                    setTimeout(() => {
                        window.location.href = "{{ route('admin.buku.index') }}";
                    }, 1500);
                });
            });
        });
    </script>
    @endpush
</x-app-layout>