@extends('layouts.edit')

@section('title', 'Edit Koleksi')

@section('content')
    <div class="form-page-wrapper">

        <div class="mb-4 border-bottom pb-3">
            <h4 class="mb-0 fw-bold text-dark">
                <i class="fas fa-edit text-primary me-2"></i> Edit Koleksi Buku
            </h4>
        </div>

        @if (session('success'))
            <div class="alert alert-success alert-dismissible fade show shadow-sm" role="alert">
                <i class="fas fa-check-circle me-2"></i> {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        @if ($errors->any())
            <div class="alert alert-danger shadow-sm">
                <ul class="mb-0 ps-3">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <div class="card shadow-sm border-0">
            <div class="card-body p-5 bg-white rounded-3">
                <form action="{{ route('admin.koleksi.update', $koleksi->kode_eksemplar) }}" method="POST"
                    enctype="multipart/form-data">
                    @csrf
                    @method('PUT')

                    <div class="row g-4">

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Judul <span class="text-danger">*</span></label>
                            <input type="text" name="title" class="form-control shadow-sm"
                                value="{{ old('title', $koleksi->title) }}" required>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Kode Eksemplar</label>
                            <input type="text" class="form-control shadow-sm" value="{{ $koleksi->kode_eksemplar }}"
                                readonly>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Pengarang (SOR)</label>
                            <input type="text" name="sor" class="form-control shadow-sm"
                                value="{{ old('sor', $koleksi->sor) }}">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Penerbit</label>
                            <input list="publisherList" name="publisher_name" class="form-control shadow-sm"
                                value="{{ old('publisher_name', $koleksi->publisher_name ?? '') }}">
                            <datalist id="publisherList">
                                @foreach ($publishers as $publisher)
                                    <option value="{{ $publisher->name }}">
                                @endforeach
                            </datalist>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tempat Terbit</label>
                            <input list="placeList" name="publish_place_name" class="form-control shadow-sm"
                                value="{{ old('publish_place_name', $koleksi->publish_place_name ?? '') }}">
                            <datalist id="placeList">
                                @foreach ($publishPlaces as $place)
                                    <option value="{{ $place->place_name }}">
                                @endforeach
                            </datalist>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tahun Terbit</label>
                            <input list="yearList" name="publish_year" class="form-control shadow-sm"
                                value="{{ old('publish_year', $koleksi->publish_year) }}">
                            <datalist id="yearList">
                                @for ($y = date('Y'); $y >= 1900; $y--)
                                    <option value="{{ $y }}">
                                @endfor
                            </datalist>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Klasifikasi</label>
                            <input type="text" name="classification" class="form-control shadow-sm"
                                value="{{ old('classification', $koleksi->classification) }}">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">ISBN / ISSN</label>
                            <input type="text" name="isbn_issn" class="form-control shadow-sm"
                                value="{{ old('isbn_issn', $koleksi->isbn_issn) }}">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Edisi</label>
                            <input type="text" name="edition" class="form-control shadow-sm"
                                value="{{ old('edition', $koleksi->edition) }}">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Labels</label>
                            <input type="text" name="labels" class="form-control shadow-sm"
                                value="{{ old('labels', $koleksi->labels) }}">
                        </div>

                        <div class="col-md-12">
                            <label class="form-label fw-semibold">Catatan</label>
                            <textarea name="notes" class="form-control shadow-sm"
                                rows="3">{{ old('notes', $koleksi->notes) }}</textarea>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Sampul Buku</label>
                            @if ($koleksi->image)
                                <div class="mb-2">
                                    <img src="{{ asset('storage/' . $koleksi->image) }}" class="img-thumbnail"
                                        style="max-height: 150px;">
                                </div>
                            @endif
                            <input type="file" name="image" class="form-control shadow-sm" accept="image/*">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tampilkan di OPAC</label>
                            <select name="opac_hide" class="form-control shadow-sm">
                                <option value="0" {{ $koleksi->opac_hide == 0 ? 'selected' : '' }}>Ya</option>
                                <option value="1" {{ $koleksi->opac_hide == 1 ? 'selected' : '' }}>Tidak</option>
                            </select>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Promosi</label>
                            <select name="promoted" class="form-control shadow-sm">
                                <option value="0" {{ $koleksi->promoted == 0 ? 'selected' : '' }}>Tidak</option>
                                <option value="1" {{ $koleksi->promoted == 1 ? 'selected' : '' }}>Ya</option>
                            </select>
                        </div>

                    </div>

                    <div class="d-flex gap-3 mt-5">
                        <button type="submit" class="btn btn-success rounded-pill px-4 shadow-sm">
                            <i class="fas fa-save me-1"></i> Simpan Perubahan
                        </button>
                        <a href="{{ route('admin.koleksi.index') }}" class="btn btn-secondary rounded-pill px-4 shadow-sm">
                            <i class="fas fa-arrow-left me-1"></i> Kembali
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection