@extends('layouts.dashboard')

@section('content')
<!-- Core Assets -->
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
    :root {
        --slate-900: #0f172a;
        --slate-600: #475569;
        --slate-200: #e2e8f0;
        --indigo-600: #4f46e5;
        --glass-bg: rgba(255, 255, 255, 0.95);
        --radius-xl: 24px;
        --font-jakarta: 'Plus Jakarta Sans', sans-serif;
    }

    body {
        font-family: var(--font-jakarta);
        background-color: #f8fafc;
        color: var(--slate-900);
        letter-spacing: -0.01em;
    }

    .master-container {
        max-width: 1000px;
        margin: 3rem auto;
        padding: 0 1.5rem;
    }

    .hero-title {
        font-weight: 800;
        font-size: 2.25rem;
        color: var(--slate-900);
        margin-bottom: 0.5rem;
    }

    .hero-subtitle {
        color: var(--slate-600);
        font-size: 1rem;
    }

    .elite-card {
        background: var(--glass-bg);
        border: 1px solid var(--slate-200);
        border-radius: var(--radius-xl);
        box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.05), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
        backdrop-filter: blur(8px);
    }

    .section-label {
        font-size: 0.75rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.1em;
        color: var(--indigo-600);
        margin-bottom: 1.5rem;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .section-label::after {
        content: "";
        height: 1px;
        flex-grow: 1;
        background: var(--slate-200);
    }

    .field-label {
        font-size: 0.875rem;
        font-weight: 600;
        color: var(--slate-900);
        margin-bottom: 0.5rem;
        display: block;
    }

    .elite-input {
        width: 100%;
        padding: 0.875rem 1.25rem;
        border-radius: 12px;
        border: 1px solid var(--slate-200);
        background: #fff;
        transition: all 0.2s ease;
        font-size: 0.95rem;
    }

    .elite-input:focus {
        outline: none;
        border-color: var(--indigo-600);
        box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
    }

    .elite-input:disabled {
        background-color: #f1f5f9;
        color: var(--slate-600);
        cursor: not-allowed;
        border-style: dashed;
    }

    .search-engine-results {
        position: absolute;
        width: 100%;
        top: 100%;
        left: 0;
        background: white;
        border-radius: 12px;
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        z-index: 50;
        border: 1px solid var(--slate-200);
        margin-top: 8px;
        max-height: 280px;
        overflow-y: auto;
    }

    .result-item {
        padding: 1rem 1.25rem;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 12px;
        border-bottom: 1px solid #f8fafc;
    }

    .result-item:hover { background-color: #f8faff; }
    .result-item i { color: var(--indigo-600); font-size: 1.1rem; }

    .btn-submit-elite {
        background: var(--slate-900);
        color: #fff;
        padding: 1rem 2.5rem;
        border-radius: 12px;
        font-weight: 700;
        border: none;
        transition: all 0.3s;
        display: inline-flex;
        align-items: center;
        gap: 12px;
    }

    .btn-submit-elite:hover {
        background: var(--indigo-600);
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(79, 70, 229, 0.3);
    }

    @media (max-width: 768px) {
        .master-container { margin: 1rem auto; }
        .hero-title { font-size: 1.75rem; }
        .elite-card { border-radius: 0; border: none; box-shadow: none; background: transparent; }
        .btn-submit-elite { width: 100%; justify-content: center; }
    }
</style>

<div class="master-container">
    <header class="mb-5 text-center text-md-start px-2">
        <h1 class="hero-title">Formulir Peminjaman</h1>
        <p class="hero-subtitle">Proses peminjaman koleksi buku perpustakaan secara digital.</p>
    </header>

    @if(!$anggota)
        <div class="elite-card p-5 text-center">
            <div class="mb-4"><i class="fas fa-user-lock fa-4x text-slate-200"></i></div>
            <h4 class="fw-bold">Akses Dibatasi</h4>
            <p class="text-muted">Hanya anggota aktif yang dapat melakukan peminjaman.</p>
            <a href="{{ route('user.member.create') }}" class="btn btn-outline-dark px-4 py-2 rounded-pill mt-2">Daftar Anggota</a>
        </div>
    @elseif(in_array($anggota->status, ['menunggu', 'ditolak']))
        <div class="elite-card p-4 border-start border-4 border-warning bg-white">
            <div class="d-flex align-items-center gap-4">
                <div class="flex-shrink-0">
                    <span class="p-3 bg-warning bg-opacity-10 rounded-circle d-inline-block">
                        <i class="fas fa-user-clock text-warning fs-4"></i>
                    </span>
                </div>
                <div>
                    <h5 class="mb-1 fw-bold">Status Keanggotaan: {{ ucfirst($anggota->status) }}</h5>
                    <p class="text-muted small mb-0">Maaf, Anda belum bisa meminjam buku. Silakan hubungi admin untuk verifikasi akun.</p>
                </div>
            </div>
        </div>
    @else
        <div class="elite-card overflow-hidden">
            <div class="card-body p-4 p-md-5">
                <form id="peminjamanForm" action="{{ route('user.peminjaman.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <input type="hidden" name="anggota_id" value="{{ $anggota->id }}">
                    <input type="hidden" name="koleksi_id" id="hidden_koleksi_id" required>

                    <!-- Row 1: Profil Peminjam -->
                    <div class="section-label">01. Profil Peminjam</div>
                    <div class="row g-4 mb-5">
                        <div class="col-md-4">
                            <label class="field-label">Nama Lengkap</label>
                            <input type="text" class="elite-input" value="{{ $anggota->nama }}" disabled title="Data diambil dari profil anggota">
                        </div>
                        <div class="col-md-4">
                            <label class="field-label">Nomor Identitas (NIP/NIM)</label>
                            <input type="text" class="elite-input" value="{{ $anggota->nip_nim }}" disabled title="Data diambil dari profil anggota">
                        </div>
                        <div class="col-md-4">
                            <label class="field-label">Email Terdaftar</label>
                            <input type="text" class="elite-input" value="{{ Auth::user()->email }}" disabled title="Email akun Anda">
                        </div>
                    </div>

                    <!-- Row 2: Pilih Koleksi -->
                    <div class="section-label">02. Pilih Koleksi</div>
                    <div class="mb-5 position-relative">
                        <label class="field-label">Cari Buku yang Ingin Dipinjam</label>
                        <div class="input-group">
                            <span class="input-group-text bg-white border-end-0 px-3"><i class="fas fa-search text-muted"></i></span>
                            <input type="text" id="title_input" class="elite-input border-start-0" 
                                   placeholder="Ketik judul buku di sini..." autocomplete="off">
                        </div>
                        <div id="autocomplete-list" class="search-engine-results d-none"></div>
                        
                        <!-- Konfirmasi Pilihan -->
                        <div id="selected-book-alert" class="mt-3 d-none">
                            <div class="alert alert-success border-0 py-3 px-4 d-flex align-items-center gap-3" style="border-radius: 16px; background-color: #f0fdf4;">
                                <i class="fas fa-check-circle fs-4 text-success"></i>
                                <div>
                                    <div class="fw-bold text-success" style="font-size: 0.85rem;">Buku Berhasil Dipilih:</div>
                                    <div id="selected-title-text" class="fw-700 text-dark"></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Row 3: Detail Peminjaman -->
                    <div class="section-label">03. Detail Peminjaman</div>
                    <div class="row g-4">
                        <div class="col-md-6">
                            <label class="field-label">Rencana Pengambilan</label>
                            <input type="date" name="tanggal_pinjam" class="elite-input" required value="{{ date('Y-m-d') }}">
                        </div>
                        <div class="col-md-6">
                            <label class="field-label">Unggah Surat Pengantar (Format PDF)</label>
                            <input type="file" name="surat_pengantar" class="form-control elite-input" accept=".pdf" required>
                        </div>
                    </div>

                    <!-- Action Bar -->
                    <div class="mt-5 pt-4 border-top d-flex flex-column flex-md-row justify-content-between align-items-center gap-4">
                        <a href="{{ route('user.peminjaman.index') }}" class="text-slate-600 text-decoration-none fw-600 small">
                            <i class="fas fa-arrow-left me-2"></i> Batal
                        </a>
                        <button type="submit" class="btn-submit-elite">
                            Kirim Permohonan <i class="fas fa-paper-plane"></i>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    @endif

    <footer class="mt-4 text-center">
        <p class="text-muted" style="font-size: 0.75rem;">© {{ date('Y') }} Perpustakaan Digital Terintegrasi.</p>
    </footer>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const input = document.getElementById('title_input');
    const hiddenInput = document.getElementById('hidden_koleksi_id');
    const listContainer = document.getElementById('autocomplete-list');
    const selectedAlert = document.getElementById('selected-book-alert');
    const selectedText = document.getElementById('selected-title-text');

    const books = @json($koleksi->map(fn($k) => ['id' => $k->biblio_id, 'title' => $k->title]));

    input.addEventListener('input', function() {
        const val = this.value.toLowerCase();
        listContainer.innerHTML = '';
        listContainer.classList.add('d-none');
        
        if (val.length < 2) return;

        const matches = books.filter(b => b.title.toLowerCase().includes(val)).slice(0, 5);
        
        if(matches.length > 0) {
            listContainer.classList.remove('d-none');
            matches.forEach(b => {
                const div = document.createElement('div');
                div.className = 'result-item';
                div.innerHTML = `<i class="fas fa-book-bookmark"></i> <div><span class="fw-bold">${b.title}</span><br><small class="text-muted">Tersedia untuk dipinjam</small></div>`;
                div.onclick = () => {
                    input.value = b.title;
                    hiddenInput.value = b.id;
                    listContainer.classList.add('d-none');
                    selectedAlert.classList.remove('d-none');
                    selectedText.innerText = b.title;
                    // Beri efek visual pada input
                    input.style.borderColor = '#10b981';
                };
                listContainer.appendChild(div);
            });
        }
    });

    document.getElementById('peminjamanForm')?.addEventListener('submit', function(e) {
        if (!hiddenInput.value) {
            e.preventDefault();
            Swal.fire({
                icon: 'warning',
                title: 'Buku Belum Dipilih',
                text: 'Silakan cari dan klik judul buku yang muncul di daftar pencarian.',
                confirmButtonColor: '#4f46e5'
            });
        }
    });

    document.addEventListener('click', (e) => {
        if (e.target !== input) listContainer.classList.add('d-none');
    });
});
</script>
@endsection

