<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">
                    <form action="#" method="POST" id="formTambahBuku">
                        <div class="row">
                            <div class="col-md-8">
                                {{-- JUDUL --}}
                                <div class="mb-3">
                                    <label for="judul" class="form-label">Judul Buku</label>
                                    <input type="text" class="form-control" id="judul" placeholder="Masukkan judul buku" required>
                                </div>
                                
                                {{-- PENULIS --}}
                                <div class="mb-3">
                                    <label for="penulis" class="form-label">Penulis</label>
                                    <input type="text" class="form-control" id="penulis" placeholder="Masukkan nama penulis" required>
                                </div>

                                <div class="row">
                                    {{-- KATEGORI --}}
                                    <div class="col-md-3 mb-3">
                                        <label for="kategori" class="form-label">Kategori</label>
                                        <input type="text" class="form-control" id="kategori" placeholder="Contoh: Fiksi" required>
                                    </div>
                                    
                                    {{-- TAHUN TERBIT (SOLUSI DROPDOWN / SELECT) --}}
                                    <div class="col-md-3 mb-3">
                                        <label for="tahun" class="form-label">Tahun Terbit</label>
                                        <div class="input-group">
                                            <span class="input-group-text bg-white border-end-0"><i class="bi bi-calendar-event"></i></span>
                                            <select class="form-select border-start-0 ps-0" id="tahun" required>
                                                <option value="" selected disabled>Pilih Tahun</option>
                                                @php
                                                    $tahunSekarang = date('Y');
                                                    $tahunMulai = 1900;
                                                @endphp
                                                @for ($i = $tahunSekarang; $i >= $tahunMulai; $i--)
                                                    <option value="{{ $i }}">{{ $i }}</option>
                                                @endfor
                                            </select>
                                        </div>
                                    </div>

                                    {{-- KODE BUKU --}}
                                    <div class="col-md-3 mb-3">
                                        <label for="kode_buku" class="form-label">Kode Buku</label>
                                        <input type="text" class="form-control" id="kode_buku" placeholder="Contoh: 330" required>
                                    </div>
                                    
                                    {{-- STOK BUKU --}}
                                    <div class="col-md-3 mb-3">
                                        <label for="stok_buku" class="form-label">Stok Buku</label>
                                        <input type="number" class="form-control" id="stok_buku" min="0" oninput="this.value = Math.abs(this.value)" placeholder="Contoh: 15" required>
                                    </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" id="tipe_offline" checked>
                                            <label class="form-check-label" for="tipe_offline">Peminjaman Offline</label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" id="tipe_online">
                                            <label class="form-check-label" for="tipe_online">Baca Online</label>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            {{-- UPLOAD FILE --}}
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="cover" class="form-label">Cover Buku</label>
                                    <input type="file" class="form-control" id="cover">
                                    <div class="form-text small">Format: JPG, PNG. Maks: 2MB</div>
                                </div>
                                <div class="mb-3">
                                    <label for="file_pdf" class="form-label">File PDF (untuk buku online)</label>
                                    <input type="file" class="form-control" id="file_pdf">
                                    <div class="form-text small">Format: PDF. Maks: 10MB</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>