@extends('adminlte.layouts.app')

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

    <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>Daftar Buku
                    </h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item active">Buku</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">

            <div class="card shadow-xl rounded-3 border-0">
                <div class="card-header d-flex justify-content-between align-items-center bg-gradient-primary text-white py-3 px-4">
                    <h3 class="card-title m-0 fw-semibold">Data Buku</h3>
                    <button id="btnTambah" class="btn btn-light btn-sm fw-semibold shadow-sm hover-scale">
                        <i class="fas fa-plus-circle me-1"></i> Tambah Buku
                    </button>
                </div>

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

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

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

                    <div class="table-responsive rounded-3 overflow-auto shadow-sm">
                        <table id="bukuTable" 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</th>
                                    <th>Pengarang</th>
                                    <th>Penerbit</th>
                                    <th>Tahun</th>
                                    <th>Kategori</th>
                                    <th>Jumlah</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($buku as $item)
                                <tr class="hover-shadow">
                                    <td class="text-center">{{ $loop->iteration }}</td>
                                    <td>{{ $item->judul }}</td>
                                    <td>{{ $item->pengarang }}</td>
                                    <td>{{ $item->penerbit }}</td>
                                    <td class="text-center">{{ $item->tahun_terbit }}</td>
                                    <td class="text-center">{{ $item->kategori }}</td>
                                    <td class="text-center">{{ $item->jumlah }}</td>
                                    <td class="text-center">
                                        <div class="d-flex justify-content-center gap-2 flex-wrap">
<a href="{{ route('peminjaman.create', $item->id) }}" class="btn btn-primary">
    Pinjam
</a>

                                            <button class="btn btn-info btn-sm shadow-sm hover-scale btnShow"
                                                data-url="{{ route('buku.show', $item->id) }}">
                                                <i class="fas fa-eye"></i>
                                            </button>

                                            <button class="btn btn-warning btn-sm shadow-sm hover-scale btnEdit"
                                                data-url="{{ route('buku.edit', $item->id) }}">
                                                <i class="fas fa-edit"></i>
                                            </button>

                                            <form action="{{ route('buku.destroy', $item->id) }}"
                                                method="POST" class="d-inline formDelete">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit"
                                                    class="btn btn-danger btn-sm shadow-sm hover-scale">
                                                    <i class="fas fa-trash-alt"></i>
                                                </button>
                                            </form>

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

                    <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("bukuTable");
    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();

    // Tambah
    document.getElementById("btnTambah").onclick = () => {
        Swal.fire({
            title: 'Tambah Buku?',
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Lanjut'
        }).then(r => {
            if (r.isConfirmed) {
                window.location.href = "{{ route('buku.create') }}";
            }
        });
    };

    // Show
    document.querySelectorAll(".btnShow").forEach(btn => {
        btn.onclick = () => {
            Swal.fire({
                title: 'Lihat Detail?',
                icon: 'info',
                showCancelButton: true,
                confirmButtonText: 'Lanjut'
            }).then(r => {
                if (r.isConfirmed) {
                    window.location.href = btn.dataset.url;
                }
            });
        };
    });

    // Edit
    document.querySelectorAll(".btnEdit").forEach(btn => {
        btn.onclick = () => {
            Swal.fire({
                title: 'Edit Buku?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Lanjut'
            }).then(r => {
                if (r.isConfirmed) {
                    window.location.href = btn.dataset.url;
                }
            });
        };
    });

    // Delete
    document.querySelectorAll(".formDelete").forEach(form => {
        form.addEventListener("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Yakin hapus?',
                text: 'Data akan dihapus permanen',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                confirmButtonText: 'Ya, hapus'
            }).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
