<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">
                        <form action="{{ route('admin.buku.update', $buku->id) }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            @method('PUT')
                            <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"
                                            value="{{ old('judul', $buku->judul) }}" required>
                                    </div>
                                    <div class="mb-3">
                                        <label for="penulis" class="form-label">Penulis</label>
                                        <input type="text" name="penulis" class="form-control" id="penulis"
                                            value="{{ old('penulis', $buku->penulis) }}" required>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <label for="category_id" class="form-label">Kategori</label>
                                            <select name="category_id" class="form-select" id="category_id" required>
                                                @foreach($categories as $category)
                                                <option value="{{ $category->id }}" {{ old('category_id', $buku->category_id) == $category->id ? 'selected' : '' }}>
                                                    {{ $category->name }}
                                                </option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="col-md-6 mb-3">
                                            <label for="tahun" class="form-label">Tahun Terbit</label>
                                            <input type="number" name="tahun" class="form-control" id="tahun"
                                                value="{{ old('tahun', $buku->tahun) }}" required>
                                        </div>
                                    </div>

                                    @php
                                    $tipe_akses = is_array($buku->tipe_akses)
                                    ? $buku->tipe_akses
                                    : [$buku->tipe_akses];
                                    $isOffline = in_array('offline', $tipe_akses);
                                    @endphp

                                    {{-- Field Kode Buku - Hanya untuk Buku Offline --}}
                                    <div class="mb-3" id="kodeBukuWrapper">
                                        <label for="kode_buku" class="form-label">Kode Buku</label>
                                        <input type="text" name="kode_buku" class="form-control" id="kode_buku"
                                            value="{{ old('kode_buku', $buku->kode_buku) }}" placeholder="Masukkan kode buku">
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Tipe Akses</label>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="tipe_akses[]" value="offline" id="tipe_offline"
                                                @if (in_array('offline', $tipe_akses)) checked @endif>
                                            <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"
                                                @if (in_array('online', $tipe_akses)) checked @endif>
                                            <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">
                                        <img src="{{ asset($buku->cover) }}" alt="Cover saat ini"
                                            class="img-thumbnail mt-2" width="150">
                                    </div>
                                    <div class="mb-3">
                                        <label for="file_pdf" class="form-label">Update File PDF (Opsional)</label>
                                        <input type="file" name="file_pdf" class="form-control" id="file_pdf">
                                    </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>