@extends('admin.layout')

@section('content')
<link rel="stylesheet" href="{{ asset('css/fitur-admin.css') }}">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<div class="dashboard-section">
    <div class="mb-4 d-flex justify-content-between align-items-center">
        <div>
            <h4 class="fw-bold text-dark">Data Guru BK</h4>
            <p class="text-muted small">Kelola daftar administrator dan guru pembimbing SMAN 4 Jember.</p>
        </div>
        <a href="{{ route('admin.guru.create') }}" class="btn text-white px-4 shadow-sm" style="background-color: var(--primary-green); border-radius: 10px;">
            <i class="fas fa-plus me-2"></i>Tambah Guru BK
        </a>
    </div>

    <div class="table-container-modern shadow-sm">
        <div class="table-responsive">
            <table class="table table-modern mb-0">
                <thead>
                    <tr>
                        <th class="text-center" width="100">ID Guru</th>
                        <th>Nama Lengkap & Gelar</th>
                        <th class="text-center" width="150">Opsi</th>
                    </tr>
                </thead>
                <tbody id="guru-table-body">
                    <tr><td colspan="3" class="text-center py-5 text-muted">Memuat data...</td></tr>
                </tbody>
            </table>
        </div>
        
        <div class="pagination-container">
            <div class="text-muted small" id="pagination-info">
                Menampilkan 0 sampai 0 dari 0 data
            </div>
            <div class="d-flex gap-1" id="pagination-controls">
                </div>
        </div>
    </div>
</div>

<script>
    async function fetchGuru(page = 1) {
        const token = localStorage.getItem('access_token');
        try {
            const response = await fetch(`/api/admin/guru?page=${page}`, {
                headers: { 
                    'Authorization': 'Bearer ' + token,
                    'Accept': 'application/json'
                }
            });
            const result = await response.json();
            const tbody = document.getElementById('guru-table-body');
            tbody.innerHTML = '';

            if(!result.data || result.data.length === 0) {
                tbody.innerHTML = '<tr><td colspan="3" class="text-center py-4">Tidak ada data guru.</td></tr>';
                return;
            }

            result.data.forEach((item) => {
                tbody.innerHTML += `
                    <tr>
                        <td class="text-center fw-bold text-muted">${item.id_guru}</td>
                        <td>
                            <div class="d-flex align-items-center gap-3">
                                <span class="badge badge-bk rounded-circle d-flex align-items-center justify-content-center" style="width: 30px; height: 30px;">BK</span>
                                <b class="text-dark">${item.nama_guru}</b>
                            </div>
                        </td>
                        <td class="text-center">
                            <div class="opsi-wrapper">
                                <a href="/admin/guru/${item.id}/edit" class="btn-action btn-edit" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <button onclick="deleteGuru(${item.id})" class="btn-action btn-delete" title="Hapus">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>`;
            });

            document.getElementById('pagination-info').innerText = 
                `Menampilkan ${result.from || 0} sampai ${result.to || 0} dari ${result.total || 0} data`;
            
            renderPagination(result);
        } catch (error) { 
            tbody.innerHTML = '<tr><td colspan="3" class="text-center text-danger">Gagal memuat data.</td></tr>';
        }
    }

    function renderPagination(data) {
        const controls = document.getElementById('pagination-controls');
        controls.innerHTML = '';

        const prevBtn = document.createElement('button');
        prevBtn.className = 'page-link-custom';
        prevBtn.innerHTML = '&laquo; Previous';
        prevBtn.disabled = data.current_page === 1;
        prevBtn.onclick = () => fetchGuru(data.current_page - 1);
        controls.appendChild(prevBtn);

        const pageBtn = document.createElement('button');
        pageBtn.className = 'page-link-custom active';
        pageBtn.innerText = data.current_page || 1;
        controls.appendChild(pageBtn);

        const nextBtn = document.createElement('button');
        nextBtn.className = 'page-link-custom';
        nextBtn.innerHTML = 'Next &raquo;';
        nextBtn.disabled = data.current_page === data.last_page;
        nextBtn.onclick = () => fetchGuru(data.current_page + 1);
        controls.appendChild(nextBtn);
    }

    async function deleteGuru(id) {
        const res = await Swal.fire({
            title: 'Hapus Guru?',
            text: "Data guru akan dihapus permanen!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#ef4444',
            confirmButtonText: 'Ya, Hapus'
        });

        if (res.isConfirmed) {
            const token = localStorage.getItem('access_token');
            try {
                const response = await fetch('/api/admin/guru/' + id, { 
                    method: 'DELETE', 
                    headers: { 'Authorization': 'Bearer ' + token } 
                });
                if(response.ok) {
                    Swal.fire('Terhapus!', 'Data guru berhasil dihapus.', 'success');
                    fetchGuru();
                }
            } catch (error) { Swal.fire('Error!', 'Koneksi gagal.', 'error'); }
        }
    }
    document.addEventListener('DOMContentLoaded', () => fetchGuru(1));
</script>
@endsection