@extends('adminlte.layouts.app')

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

    {{-- HEADER --}}
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-3">
                <div class="col-sm-6">
                    <h1 class="m-0 fw-bold text-gradient-primary">
                        <i class="fas fa-book me-2"></i>Form Peminjaman Buku
                    </h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">
                            <a href="{{ route('peminjaman.index') }}">Peminjaman</a>
                        </li>
                        <li class="breadcrumb-item active">Form</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    {{-- CONTENT --}}
    <section class="content">
        <div class="container-fluid">

            <div class="card shadow-xl rounded-3 border-0">
                <div class="card-header bg-gradient-primary text-white py-3 px-4">
                    <h3 class="card-title m-0 fw-semibold">
                        <i class="fas fa-clipboard-list me-1"></i> Input Peminjaman
                    </h3>
                </div>

                <div class="card-body px-4 py-4">

                    {{-- INFO BUKU --}}
                    <div class="mb-4">
                        <h6 class="fw-bold mb-3">📘 Informasi Buku</h6>
                        <table class="table table-bordered table-sm align-middle">
                            <tr>
                                <th width="200">Judul Buku</th>
                                <td>{{ $buku->judul }}</td>
                            </tr>
                            <tr>
                                <th>Pengarang</th>
                                <td>{{ $buku->pengarang }}</td>
                            </tr>
                            <tr>
                                <th>Penerbit</th>
                                <td>{{ $buku->penerbit }}</td>
                            </tr>
                            <tr>
                                <th>Stok Tersedia</th>
                                <td>
                                    <span class="badge bg-info fs-6">
                                        {{ $buku->jumlah }}
                                    </span>
                                </td>
                            </tr>
                        </table>
                    </div>

                    <hr>

                    {{-- FORM --}}
                    <form id="formPeminjaman"
                          action="{{ route('peminjaman.store') }}"
                          method="POST">
                        @csrf

                        <input type="hidden" name="buku_id" value="{{ $buku->id }}">

                        {{-- DROPDOWN SEARCH --}}
                        <div class="mb-3">
                            <label class="form-label fw-semibold">Peminjam (Anggota)</label>
                            <select name="anggota_id"
                                    id="anggotaSelect"
                                    class="form-select shadow-sm"
                                    required>
                                <option value="">-- Cari & Pilih Anggota --</option>
                                @foreach ($anggota as $item)
                                    <option value="{{ $item->id }}">
                                        {{ $item->siswa->nama }} — NIS {{ $item->siswa->nis }}
                                    </option>
                                @endforeach
                            </select>
                            @error('anggota_id')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-semibold">Tanggal Pinjam</label>
                            <input type="date"
                                   name="tanggal_pinjam"
                                   class="form-control shadow-sm"
                                   value="{{ date('Y-m-d') }}"
                                   required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-semibold">Jumlah Buku</label>
                            <input type="number"
                                   name="jumlah"
                                   class="form-control shadow-sm"
                                   min="1"
                                   max="{{ $buku->jumlah }}"
                                   required>
                            <small class="text-muted">
                                Maksimal {{ $buku->jumlah }} buku
                            </small>
                        </div>

                        <div class="d-flex justify-content-between mt-4">
                            <a href="{{ route('peminjaman.index') }}"
                               class="btn btn-secondary shadow-sm hover-scale">
                                <i class="fas fa-arrow-left me-1"></i> Kembali
                            </a>

                            <button type="submit"
                                    class="btn btn-primary shadow-sm hover-scale">
                                <i class="fas fa-save me-1"></i> Simpan Peminjaman
                            </button>
                        </div>

                    </form>

                </div>
            </div>

        </div>
    </section>
</div>

{{-- ================= CDN ================= --}}
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

{{-- ================= SCRIPT ================= --}}
<script>
$(document).ready(function () {

    // SELECT2 SEARCH
    $('#anggotaSelect').select2({
        placeholder: '-- Cari & Pilih Anggota --',
        allowClear: true,
        width: '100'
    });

    // SWEETALERT SUBMIT
    $('#formPeminjaman').on('submit', function(e){
        e.preventDefault();
        Swal.fire({
            title: 'Simpan Peminjaman?',
            text: 'Pastikan data sudah benar',
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Ya, simpan'
        }).then((result) => {
            if (result.isConfirmed) {
                this.submit();
            }
        });
    });

});
</script>

<style>
.text-gradient-primary {
    background: linear-gradient(90deg, #6a11cb, #2575fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.hover-scale {
    transition: 0.2s;
}
.hover-scale:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}
.select2-container .select2-selection--single {
    height: 38px;
    padding: 4px 10px;
}
</style>
@endsection
