<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 Edit 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">
                        {{-- TAMBAHKAN ID PADA FORM --}}
                        <form action="#" method="POST" id="formEditBuku">
                            <div class="row">
                                <div class="col-md-8">
                                    <div class="mb-3">
                                        <label for="judul" class="form-label">Judul Buku</label>
                                        <input type="text" class="form-control" id="judul" value="{{ $buku['judul'] }}" required>
                                    </div>
                                    <div class="mb-3">
                                        <label for="penulis" class="form-label">Penulis</label>
                                        <input type="text" class="form-control" id="penulis" value="{{ $buku['penulis'] }}" required>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <label for="kategori" class="form-label">Kategori</label>
                                            <input type="text" class="form-control" id="kategori" value="{{ $buku['kategori'] }}" required>
                                        </div>
                                        <div class="col-md-6 mb-3">
                                            <label for="tahun" class="form-label">Tahun Terbit</label>
                                            <input type="number" class="form-control" id="tahun" value="{{ $buku['tahun'] }}" min="1000" max="2099" oninput="this.value = Math.abs(this.value)" required>
                                        </div>
                                    </div>
                                    
                                    @php
                                        $tipe_akses = is_array($buku['tipe_akses']) ? $buku['tipe_akses'] : [$buku['tipe_akses']];
                                        $isOffline = in_array('offline', $tipe_akses);
                                    @endphp

                                    @if($isOffline)
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <label for="kode_buku" class="form-label">Kode Buku</label>
                                            <input type="text" class="form-control" id="kode_buku" value="{{ $buku['kode_buku'] ?? '' }}" placeholder="Masukkan kode buku" required>
                                        </div>
                                        <div class="col-md-6 mb-3">
                                            <label for="stok_buku" class="form-label">Stok Buku</label>
                                            <input type="number" class="form-control" id="stok_buku" value="{{ $buku['stok'] ?? 0 }}" min="0" oninput="this.value = Math.abs(this.value)" required>
                                        </div>
                                    </div>
                                    @endif

                                    <div class="mb-3">
                                        <label class="form-label">Tipe Akses (Tidak dapat diubah)</label>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" id="tipe_offline" @if (in_array('offline', $tipe_akses)) checked @endif disabled>
                                            <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" @if (in_array('online', $tipe_akses)) checked @endif disabled>
                                            <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" class="form-control" id="cover">
                                        <img src="{{ asset($buku['cover']) }}" alt="Cover saat ini" class="img-thumbnail mt-2" width="150">
                                    </div>
                                </div>
                            </div>
                            <hr>
                            <div class="d-flex justify-content-end">
                                <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        document.getElementById('formEditBuku').addEventListener('submit', function(e) {
            e.preventDefault(); 
            
            modernSwal.fire({
                title: 'Menyimpan Perubahan...',
                timer: 800,
                didOpen: () => Swal.showLoading()
            }).then(() => {
                Toast.fire({
                    icon: 'success',
                    title: 'Berhasil',
                    text: 'Data buku berhasil diperbarui.'
                });

                setTimeout(() => {
                    window.location.href = "{{ route('admin.buku.index') }}";
                }, 1500);
            });
        });
    </script>
    @endpush
</x-app-layout>