@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">Manajemen Data Alumni</h4>
            <p class="text-muted small">Kelola data historis lulusan untuk kebutuhan dataset sistem prediksi.</p>
        </div>
        <a href="{{ route('admin.alumni.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 Data Alumni
        </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 Alumni</th>
                        <th>Nama Alumni</th>
                        <th>Perguruan Tinggi</th>
                        <th class="text-center">Jurusan Diambil</th>
                        <th class="text-center" width="150">Opsi</th>
                    </tr>
                </thead>
                <tbody id="alumni-table-body">
                    <tr><td colspan="5" class="text-center py-5 text-muted">Memuat data alumni...</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 fetchAlumni(page = 1) {
        const token = localStorage.getItem('access_token');
        try {
            const response = await fetch(`/api/admin/alumni?page=${page}`, {
                headers: { 
                    'Authorization': 'Bearer ' + token,
                    'Accept': 'application/json'
                }
            });
            const result = await response.json();
            const tbody = document.getElementById('alumni-table-body');
            tbody.innerHTML = '';

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

            result.data.forEach((item) => {
                tbody.innerHTML += `
                    <tr>
                        <td class="text-center fw-bold text-muted">${item.id_alumni}</td>
                        <td class="fw-bold text-dark">${item.nama_alumni}</td>
                        <td class="text-muted small">${item.perguruan_tinggi}</td>
                        <td class="text-center"><span class="badge bg-light text-dark border px-3 py-2" style="border-radius:8px">${item.jurusan_diambil}</span></td>
                        <td class="text-center">
                            <div class="opsi-wrapper">
                                <a href="/admin/alumni/${item.id}/edit" class="btn-action btn-edit" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <button onclick="deleteAlumni(${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="5" 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 = () => fetchAlumni(data.current_page - 1);
        controls.appendChild(prevBtn);

        const pageBtn = document.createElement('button');
        pageBtn.className = 'page-link-custom active';
        pageBtn.innerText = data.current_page;
        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 = () => fetchAlumni(data.current_page + 1);
        controls.appendChild(nextBtn);
    }

    async function deleteAlumni(id) {
        const res = await Swal.fire({
            title: 'Hapus Data?',
            text: "Data alumni akan dihapus secara 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/alumni/' + id, { 
                    method: 'DELETE', 
                    headers: { 'Authorization': 'Bearer ' + token } 
                });
                if(response.ok) {
                    Swal.fire('Terhapus!', 'Data berhasil dihapus.', 'success');
                    fetchAlumni();
                }
            } catch (error) { Swal.fire('Error!', 'Koneksi gagal.', 'error'); }
        }
    }
    document.addEventListener('DOMContentLoaded', () => fetchAlumni(1));
</script>
@endsection