@extends('layouts.edit')

@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-book text-primary me-2"></i> Tambah 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

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

                    <input type="hidden" name="kode_eksemplar" id="kode_eksemplar">

                    <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" id="title" class="form-control shadow-sm" required
                                placeholder="Masukkan judul buku">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Jumlah Eksemplar <span
                                    class="text-danger">*</span></label>
                            <input type="number" name="jumlah_eksemplar" class="form-control shadow-sm" min="1" value="1"
                                required>
                            <small class="text-muted">Masukkan jumlah eksemplar buku ini (contoh: 2)</small>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Pengarang</label>
                            <input type="text" name="sor" class="form-control shadow-sm"
                                placeholder="Masukkan nama pengarang">
                            <small class="text-muted">Kolom ini otomatis disimpan sebagai 'sor' di database.</small>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Edisi</label>
                            <input type="text" name="edition" class="form-control shadow-sm"
                                placeholder="Masukkan edisi buku">
                        </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"
                                placeholder="Masukkan ISBN atau ISSN">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Penerbit</label>
                            <input list="publisherList" type="text" name="publisher_name" class="form-control shadow-sm"
                                placeholder="Ketik atau pilih penerbit">
                            <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="publishPlaceList" type="text" name="publish_place_name"
                                class="form-control shadow-sm" placeholder="Ketik atau pilih tempat terbit">
                            <datalist id="publishPlaceList">
                                @foreach($publishPlaces as $place)
                                    <option value="{{ $place->name }}">
                                @endforeach
                            </datalist>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tahun Terbit</label>
                            <input type="number" name="publish_year" class="form-control shadow-sm"
                                placeholder="Masukkan tahun terbit">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Klasifikasi</label>
                            <input type="text" name="classification" class="form-control shadow-sm"
                                placeholder="Masukkan kode klasifikasi">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Sampul Buku</label>
                            <input type="file" name="image" class="form-control shadow-sm">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tampilkan di OPAC</label>
                            <div class="d-flex gap-4 mt-1">
                                <div class="form-check">
                                    <input type="radio" name="opac_hide" value="0" id="opac_show" class="form-check-input"
                                        checked>
                                    <label for="opac_show" class="form-check-label">Ya</label>
                                </div>
                                <div class="form-check">
                                    <input type="radio" name="opac_hide" value="1" id="opac_hide" class="form-check-input">
                                    <label for="opac_hide" class="form-check-label">Tidak</label>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Promosi di Beranda</label>
                            <div class="d-flex gap-4 mt-1">
                                <div class="form-check">
                                    <input type="radio" name="promoted" value="1" id="promote_yes" class="form-check-input">
                                    <label for="promote_yes" class="form-check-label">Ya</label>
                                </div>
                                <div class="form-check">
                                    <input type="radio" name="promoted" value="0" id="promote_no" class="form-check-input"
                                        checked>
                                    <label for="promote_no" class="form-check-label">Tidak</label>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="form-label fw-semibold">Label</label>
                            <input type="text" name="labels" class="form-control shadow-sm"
                                placeholder="Masukkan label, pisahkan dengan koma">
                        </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
                        </button>
                        <button type="reset" class="btn btn-warning text-white rounded-pill px-4 shadow-sm">
                            <i class="fas fa-undo me-1"></i> Reset
                        </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>

    <style>
        .form-page-wrapper {
            width: 100vw;
            margin-left: calc(-50vw + 50%);
            background: #f8fafc;
            min-height: 100vh;
            padding: 40px 60px;
        }

        label {
            color: #374151;
            font-size: 0.95rem;
        }

        .form-control {
            border-radius: 0.4rem;
            border: 1px solid #d1d5db;
            transition: 0.2s;
        }

        .form-control:focus {
            border-color: #2563eb;
            box-shadow: 0 0 0 0.1rem rgba(37, 99, 235, 0.25);
        }

        .btn {
            border-radius: 2rem;
            font-weight: 600;
        }

        .btn-success:hover {
            background-color: #0ca848;
        }

        .card {
            border: 1px solid #e5e7eb;
        }

        body {
            background-color: #f6f8fa;
            overflow-x: hidden;
        }
    </style>

    <script>
        document.getElementById('title').addEventListener('input', function () {
            const title = this.value.trim().toUpperCase().replace(/\s+/g, '-');
            const random = Date.now().toString().slice(-6);
            const kode = "BK-" + title + "-" + random;

            document.getElementById('kode_eksemplar').value = kode;
        });
    </script>

@endsection