@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-reader me-2"></i>Data Peminjaman & Pengembalian Buku
                    </h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item active">Peminjaman & Pengembalian</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">Daftar Peminjaman & Pengembalian</h3>
                </div>

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

                    {{-- ALERT --}}
                    @if(session('success'))
                        <div class="alert alert-success shadow-sm">
                            {{ session('success') }}
                        </div>
                    @endif

                    {{-- SEARCH --}}
                    <div class="mb-4 d-flex justify-content-between align-items-center flex-wrap gap-2">
                        <input type="text" id="search" placeholder="🔍 Cari peminjaman..."
                            class="form-control shadow-sm rounded-pill"
                            style="max-width: 320px; padding: 10px 20px;">
                    </div>

                    {{-- TABLE --}}
                    <div class="table-responsive rounded-3 overflow-auto shadow-sm">
                        <table id="peminjamanTable"
                            class="table table-hover table-borderless align-middle">
                            <thead class="text-center text-white"
                                style="background: linear-gradient(135deg, #6a11cb, #2575fc);">
                                <tr>
                                    <th>No</th>
                                    <th>Judul Buku</th>
                                    <th>Peminjam</th>
                                    <th>Tgl Pinjam</th>
                                    <th>Tgl Kembali</th>
                                    <th>Jumlah</th>
                                    <th>Status</th>
                                    <th width="120">Aksi</th>
                                    <th width="120">Cetak</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($peminjaman as $item)
                                <tr class="hover-shadow">
                                    <td class="text-center">{{ $loop->iteration }}</td>
                                    <td>{{ $item->buku->judul }}</td>
                                    <td>{{ $item->anggota->siswa->nama ?? '-' }}</td>
                                    <td class="text-center">
                                        {{ \Carbon\Carbon::parse($item->tanggal_pinjam)->format('d-m-Y') }}
                                    </td>
                                    <td class="text-center">
                                        {{ $item->tanggal_kembali
                                            ? \Carbon\Carbon::parse($item->tanggal_kembali)->format('d-m-Y')
                                            : '-' }}
                                    </td>
                                    <td class="text-center">{{ $item->jumlah }}</td>
                                    <td class="text-center">
                                        @if ($item->status == 'dipinjam')
                                            <span class="badge bg-warning text-dark">Dipinjam</span>
                                        @elseif ($item->status == 'dikembalikan')
                                            <span class="badge bg-success">Dikembalikan</span>
                                        @else
                                            <span class="badge bg-danger">Terlambat</span>
                                        @endif
                                    </td>
                                    
                                    <td class="text-center">
                                        @if ($item->status == 'dipinjam')
                                            <form action="{{ route('peminjaman.kembali', $item->id) }}"
                                                  method="POST" class="formKembali d-inline">
                                                @csrf
                                                @method('PUT')
                                                <button type="submit"
                                                    class="btn btn-success btn-sm shadow-sm hover-scale">
                                                    Kembalikan
                                                </button>
                                                
                                            </form>
                                            
                                        @else
                                            <span class="text-muted">-</span>
                                        @endif
                                    </td>
                                    <td class="text-center">
    <div class="d-flex justify-content-center gap-1 flex-wrap">

        {{-- KEMBALIKAN --}}
        @if ($item->status == 'dipinjam')
            <form action="{{ route('peminjaman.kembali', $item->id) }}"
                  method="POST" class="formKembali d-inline">
                @csrf
                @method('PUT')
                <button type="submit"
                    class="btn btn-success btn-sm shadow-sm hover-scale">
                    <i class="fas fa-undo me-1"></i>Kembalikan
                </button>
            </form>
        @endif

        {{-- INVOICE PEMINJAMAN --}}
        <a href="{{ route('peminjaman.invoice', $item->id) }}"
           class="btn btn-primary btn-sm shadow-sm hover-scale">
            <i class="fas fa-file-invoice me-1"></i>Invoice
        </a>

        {{-- INVOICE PENGEMBALIAN --}}
        @if($item->status === 'dikembalikan')
            <a href="{{ route('peminjaman.invoicePengembalian', $item->id) }}"
               class="btn btn-success btn-sm shadow-sm hover-scale">
                <i class="fas fa-file-check me-1"></i>Kembali
            </a>
        @endif

        {{-- JIKA STATUS LAIN --}}
        @if(!in_array($item->status, ['dipinjam', 'dikembalikan']))
            <span class="text-muted">-</span>
        @endif

    </div>
</td>

                                    
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="8" class="text-center text-muted py-4">
                                        Data peminjaman belum tersedia
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    {{-- PAGINATION --}}
                    <div id="pagination" class="mt-4 text-center"></div>

                </div>
            </div>

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

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

<script>
document.addEventListener("DOMContentLoaded", function () {

    const table = document.getElementById("peminjamanTable");
    const rows = table.getElementsByTagName("tr");
    const pagination = document.getElementById("pagination");
    const searchInput = document.getElementById("search");

    let currentPage = 1;
    const rowsPerPage = 5;

    function showPage(page) {
        const start = (page - 1) * rowsPerPage + 1;
        const end = start + rowsPerPage;
        for (let i = 1; i < rows.length; i++) {
            rows[i].style.display = (i >= start && i < end) ? "" : "none";
        }
    }

    function setupPagination() {
        pagination.innerHTML = "";
        const pageCount = Math.ceil((rows.length - 1) / rowsPerPage);
        for (let i = 1; i <= pageCount; i++) {
            const btn = document.createElement("button");
            btn.textContent = i;
            btn.className = "page-btn shadow-sm rounded";
            if (i === currentPage) btn.classList.add("active");
            btn.onclick = () => {
                currentPage = i;
                showPage(i);
                setupPagination();
            };
            pagination.appendChild(btn);
        }
    }

    searchInput.addEventListener("keyup", function () {
        const filter = this.value.toLowerCase();
        for (let i = 1; i < rows.length; i++) {
            const text = rows[i].textContent.toLowerCase();
            rows[i].style.display = text.includes(filter) ? "" : "none";
        }
    });

    showPage(currentPage);
    setupPagination();

    // SWEETALERT KEMBALI
    document.querySelectorAll(".formKembali").forEach(form => {
        form.addEventListener("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Yakin buku sudah dikembalikan?',
                icon: 'question',
                showCancelButton: true,
                confirmButtonText: 'Ya, kembalikan'
            }).then(r => {
                if (r.isConfirmed) form.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);
}
.hover-shadow:hover {
    background: #f8f9fa;
    box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}
#pagination button {
    background: #fff;
    border: 1px solid #ddd;
    padding: 6px 14px;
    border-radius: 8px;
    margin: 0 3px;
}
#pagination button.active,
#pagination button:hover {
    background: #007bff;
    color: #fff;
}
</style>
@endsection
