

<?php $__env->startSection('title','Management Kelas'); ?>

<?php $__env->startSection('content'); ?>
<div class="mx-auto max-w-7xl space-y-4">
    <div class="rounded-2xl border border-slate-200 bg-gradient-to-r from-slate-50 via-white to-blue-50 p-4 md:p-5">
        <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
            <div>
                <h1 class="text-lg font-semibold text-slate-800">Management Kelas</h1>
                <p class="mt-1 text-xs text-slate-600">
                    Tahun ajaran: <span class="font-semibold text-slate-700"><?php echo e($tahunAktif->nama); ?></span>
                </p>
            </div>
            <div class="flex items-center gap-2">
                <button
                    type="button"
                    onclick="openTambahKelasModal()"
                    class="inline-flex items-center justify-center gap-2 rounded-xl bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm transition hover:bg-blue-700"
                >
                    <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                    <span>Tambah Kelas</span>
                </button>
            </div>
        </div>

        <div class="mt-4 flex flex-wrap gap-2.5">
            <span class="inline-flex items-center rounded-full border border-slate-200 bg-slate-50 px-3 py-1 text-xs font-semibold text-slate-700">
                Total Kelas: <?php echo e($kelasList?->count() ?? 0); ?>

            </span>
            <span class="inline-flex items-center rounded-full border border-blue-200 bg-blue-50 px-3 py-1 text-xs font-semibold text-blue-700">
                Total Peserta Didik Aktif: <?php echo e($totalSiswaAktif ?? 0); ?>

            </span>
            <span class="inline-flex items-center rounded-full border border-amber-200 bg-amber-50 px-3 py-1 text-xs font-semibold text-amber-700">
                Belum Masuk Kelas: <?php echo e($totalBelumMasukKelas ?? 0); ?>

            </span>
        </div>
    </div>

    <div class="card overflow-hidden">
        <div class="p-4 md:p-5">
            <div class="mb-4 rounded-xl border border-blue-200 bg-blue-50 p-3">
                <h4 class="text-xs font-semibold uppercase tracking-wide text-blue-700">Aksi Cepat</h4>
                <p class="mt-1 text-xs text-blue-700/90">Masukkan peserta didik yang belum memiliki kelas langsung dari panel ini.</p>

                <form id="quickAssignForm" method="POST" onsubmit="return prepareQuickAssignForm(event)" data-action-template="<?php echo e(route('siswa.assign-kelas', '__SID__')); ?>" class="mt-3 grid grid-cols-1 gap-2 md:grid-cols-12 md:items-end">
                    <?php echo csrf_field(); ?>
                    <div class="md:col-span-6">
                        <label for="quickAssignSiswa" class="mb-1 block text-[11px] font-semibold uppercase tracking-wide text-blue-700">Peserta Didik Belum Kelas</label>
                        <select id="quickAssignSiswa" class="w-full rounded-lg border border-blue-200 bg-white px-2.5 py-2 text-xs text-slate-700 focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100">
                            <option value="">Pilih peserta didik...</option>
                            <?php $__currentLoopData = $siswaBelumKelas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $siswaItem): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($siswaItem->id); ?>">
                                    <?php echo e($siswaItem->nama); ?> (<?php echo e(optional($siswaItem->registration)->nomor_registrasi ?? '-'); ?>)
                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="md:col-span-4">
                        <label for="quickAssignKelas" class="mb-1 block text-[11px] font-semibold uppercase tracking-wide text-blue-700">Kelas Tujuan</label>
                        <select id="quickAssignKelas" name="kelas_siswa_id" class="w-full rounded-lg border border-blue-200 bg-white px-2.5 py-2 text-xs text-slate-700 focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100" required>
                            <option value="">Pilih kelas...</option>
                            <?php $__currentLoopData = $kelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kelas): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($kelas->id); ?>"><?php echo e($kelas->nama_kelas); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="md:col-span-2">
                        <button type="submit" class="w-full rounded-lg bg-blue-600 px-2.5 py-2 text-xs font-semibold text-white transition hover:bg-blue-700">Assign</button>
                    </div>
                </form>
            </div>

            <div class="overflow-x-auto rounded-xl border border-slate-200">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-50 text-slate-700">
                        <tr>
                            <th class="px-3 py-2 text-left">Nama Kelas</th>
                            <th class="px-3 py-2 text-left">Peserta Didik Aktif</th>
                            <th class="px-3 py-2 text-right">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 bg-white">
                        <?php $__empty_1 = true; $__currentLoopData = $kelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kelas): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td class="px-3 py-2">
                                    <form method="POST" action="<?php echo e(route('siswa.kelas.update', $kelas->id)); ?>" class="flex items-center gap-2">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('PATCH'); ?>
                                        <input
                                            type="text"
                                            name="nama_kelas"
                                            value="<?php echo e($kelas->nama_kelas); ?>"
                                            class="w-full rounded-lg border border-slate-300 px-2.5 py-1.5 text-xs text-slate-700 focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100"
                                            required
                                        >
                                        <button type="submit" class="rounded-lg border border-blue-200 bg-blue-50 px-2.5 py-1.5 text-xs font-semibold text-blue-700 hover:bg-blue-100">
                                            Simpan
                                        </button>
                                    </form>
                                </td>
                                <td class="px-3 py-2">
                                    <span class="inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] font-semibold <?php echo e((int) ($kelas->siswa_aktif_count ?? 0) > 0 ? 'border-emerald-200 bg-emerald-50 text-emerald-700' : 'border-slate-200 bg-slate-50 text-slate-600'); ?>">
                                        <?php echo e((int) ($kelas->siswa_aktif_count ?? 0)); ?> peserta didik
                                    </span>
                                </td>
                                <td class="px-3 py-2 text-right">
                                    <div class="inline-flex items-center gap-1.5">
                                        <a href="<?php echo e(route('siswa.index', ['kelas_id' => $kelas->id, 'from_menu' => 1])); ?>" class="rounded-lg border border-slate-200 bg-white px-2.5 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50 hover:text-blue-600">
                                            Lihat
                                        </a>
                                        <button type="button" onclick="setQuickAssignClass('<?php echo e($kelas->id); ?>')" class="rounded-lg border border-blue-200 bg-blue-50 px-2.5 py-1.5 text-xs font-semibold text-blue-700 hover:bg-blue-100">
                                            Pilih untuk Assign
                                        </button>

                                        <?php if((int) ($kelas->siswa_count ?? 0) === 0): ?>
                                            <form method="POST" action="<?php echo e(route('siswa.kelas.destroy', $kelas->id)); ?>" onsubmit="return window.globalConfirmSubmit(this, 'Hapus kelas <?php echo e($kelas->nama_kelas); ?>?', { title: 'Konfirmasi Hapus Kelas' })" class="inline-block">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('DELETE'); ?>
                                                <button type="submit" class="rounded-lg border border-rose-200 bg-rose-50 px-2.5 py-1.5 text-xs font-semibold text-rose-700 hover:bg-rose-100">
                                                    Hapus
                                                </button>
                                            </form>
                                        <?php else: ?>
                                            <button type="button" disabled class="rounded-lg border border-slate-200 bg-slate-50 px-2.5 py-1.5 text-xs font-semibold text-slate-400 cursor-not-allowed" title="Kelas masih terisi">
                                                Hapus
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr>
                                <td colspan="3" class="px-3 py-6 text-center text-xs text-slate-500">Belum ada kelas dibuat.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div id="modalTambahKelas" class="fixed inset-0 z-[110] hidden items-center justify-center bg-slate-900/55 p-4" onclick="closeTambahKelasModal()">
    <div class="w-full max-w-md overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-2xl" onclick="event.stopPropagation()">
        <div class="border-b border-slate-100 bg-gradient-to-r from-blue-50 via-white to-slate-50 px-5 py-4">
            <h3 class="text-base font-semibold text-slate-800">Tambah Kelas</h3>
            <p class="mt-1 text-xs text-slate-500">Kelas baru akan otomatis muncul pada submenu Peserta Didik.</p>
        </div>
        <form action="<?php echo e(route('siswa.kelas.store')); ?>" method="POST" class="space-y-4 px-5 py-4">
            <?php echo csrf_field(); ?>
            <div>
                <label for="nama_kelas" class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Nama Kelas</label>
                <input
                    id="nama_kelas"
                    type="text"
                    name="nama_kelas"
                    class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm text-slate-700 focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100"
                    placeholder="Contoh: 1A / 1B / Kelas Tahfidz"
                    required
                >
            </div>
            <div class="flex justify-end gap-2">
                <button type="button" onclick="closeTambahKelasModal()" class="rounded-lg border border-slate-300 px-3 py-2 text-sm font-medium text-slate-600 transition hover:bg-slate-100">Batal</button>
                <button type="submit" class="rounded-lg bg-blue-600 px-3 py-2 text-sm font-semibold text-white transition hover:bg-blue-700">Simpan Kelas</button>
            </div>
        </form>
    </div>
</div>

<script>
function setQuickAssignClass(kelasId)
{
    const selectKelas = document.getElementById('quickAssignKelas');
    if (!selectKelas) {
        return;
    }

    selectKelas.value = String(kelasId || '');
    if (typeof window.showGlobalToast === 'function') {
        window.showGlobalToast('info', 'Kelas tujuan quick assign dipilih.');
    }
}

function prepareQuickAssignForm(event)
{
    event.preventDefault();

    const form = document.getElementById('quickAssignForm');
    const siswaSelect = document.getElementById('quickAssignSiswa');
    const kelasSelect = document.getElementById('quickAssignKelas');

    if (!form || !siswaSelect || !kelasSelect) {
        return false;
    }

    const siswaId = (siswaSelect.value || '').trim();
    const kelasId = (kelasSelect.value || '').trim();

    if (!siswaId) {
        if (typeof window.showGlobalToast === 'function') {
            window.showGlobalToast('warning', 'Pilih peserta didik yang belum memiliki kelas.');
        }
        return false;
    }

    if (!kelasId) {
        if (typeof window.showGlobalToast === 'function') {
            window.showGlobalToast('warning', 'Pilih kelas tujuan terlebih dahulu.');
        }
        return false;
    }

    const template = form.getAttribute('data-action-template') || '';
    if (!template.includes('__SID__')) {
        if (typeof window.showGlobalToast === 'function') {
            window.showGlobalToast('danger', 'Template quick assign tidak valid.');
        }
        return false;
    }

    form.action = template.replace('__SID__', encodeURIComponent(siswaId));
    form.submit();
    return false;
}

function openTambahKelasModal()
{
    const modal = document.getElementById('modalTambahKelas');
    if (!modal) {
        return;
    }
    modal.classList.remove('hidden');
    modal.classList.add('flex');
}

function closeTambahKelasModal()
{
    const modal = document.getElementById('modalTambahKelas');
    if (!modal) {
        return;
    }
    modal.classList.add('hidden');
    modal.classList.remove('flex');
}
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/sdmk4757/public_html/ppdb.sdmuhwonorejo.com/resources/views/admin/siswa/management-kelas.blade.php ENDPATH**/ ?>