<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 <span class="text-danger">*</span></label>
                                        <input type="text" name="judul" class="form-control @error('judul') is-invalid @enderror" id="judul"
                                            value="{{ old('judul', $buku->judul) }}" maxlength="50">
                                        @error('judul')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="mb-3">
                                        <label for="penulis" class="form-label">Penulis <span class="text-danger">*</span></label>
                                        <input type="text" name="penulis" class="form-control @error('penulis') is-invalid @enderror" id="penulis"
                                            value="{{ old('penulis', $buku->penulis) }}" maxlength="50" pattern="[a-zA-Z\s.,'\']+">
                                        @error('penulis')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="row">
                                        <div class="col-md-4 mb-3">
                                            <label for="category_id" class="form-label">Kategori <span class="text-danger">*</span></label>
                                            <select name="category_id" class="form-select @error('category_id') is-invalid @enderror" id="category_id">
                                                @foreach($categories as $category)
                                                <option value="{{ $category->id }}" {{ old('category_id', $buku->category_id) == $category->id ? 'selected' : '' }}>
                                                    {{ $category->name }}
                                                </option>
                                                @endforeach
                                            </select>
                                            @error('category_id')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        <div class="col-md-4 mb-3">
                                            <label for="tahun" class="form-label">Tahun Terbit <span class="text-danger">*</span></label>
                                            <select name="tahun" class="form-select @error('tahun') is-invalid @enderror" id="tahun">
                                                @for ($year = date('Y') + 1; $year >= 1500; $year--)
                                                    <option value="{{ $year }}" {{ old('tahun', $buku->tahun) == $year ? 'selected' : '' }}>
                                                        {{ $year }}
                                                    </option>
                                                @endfor
                                            </select>
                                            @error('tahun')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        <div class="col-md-4 mb-3">
                                            <label for="stok" class="form-label">Stok <span class="text-danger">*</span></label>
                                            <input type="number" name="stok" class="form-control @error('stok') is-invalid @enderror" id="stok"
                                                value="{{ old('stok', $buku->stok ?? 1) }}" min="0" max="99" oninput="if(this.value.length > 2) this.value = this.value.slice(0, 2); this.value = this.value.replace(/[^0-9]/g, '')">
                                            @error('stok')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </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 @error('kode_buku') is-invalid @enderror" id="kode_buku"
                                            value="{{ old('kode_buku', $buku->kode_buku) }}" placeholder="Masukkan kode buku" maxlength="10"
                                            inputmode="numeric" oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                        @error('kode_buku')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Tipe Akses</label>
                                        <div class="form-check">
                                            <input class="form-check-input @error('tipe_akses') is-invalid @enderror" type="checkbox" name="tipe_akses[]" value="offline" id="tipe_offline"
                                                @if (in_array('offline', old('tipe_akses', $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 @error('tipe_akses') is-invalid @enderror" type="checkbox" name="tipe_akses[]" value="online" id="tipe_online"
                                                @if (in_array('online', old('tipe_akses', $tipe_akses))) checked @endif>
                                            <label class="form-check-label" for="tipe_online">Baca Online</label>
                                        </div>
                                        @error('tipe_akses')
                                            <div class="text-danger small mt-1">{{ $message }}</div>
                                        @enderror
                                    </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 @error('cover') is-invalid @enderror" id="cover" accept="image/*">
                                        @error('cover')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                        <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 @error('file_pdf') is-invalid @enderror" id="file_pdf" accept=".pdf">
                                        @error('file_pdf')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </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.addEventListener('DOMContentLoaded', function() {
            // Script removed to allow standard server-side validation
        });
    </script>
    @endpush
</x-app-layout>