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

<?php $__env->startSection('content'); ?>
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Data Kelas</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modal-add">
                        <i class="fas fa-plus"></i> Tambah Kelas
                    </button>
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body table-responsive p-0">
                <table class="table table-hover text-nowrap">
                    <thead>
                        <tr>
                            <th style="width: 50px;">No</th>
                            <th>Jenjang</th>
                            <th>Tingkat</th>
                            <th>Paralel</th>
                            <th>Nama Kelas</th>
                            <th>Status</th>
                            <th style="width: 150px;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $kelasList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $kelas): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td><?php echo e($kelasList->firstItem() + $index); ?></td>
                                <td><?php echo e($kelas->jenjang); ?></td>
                                <td><?php echo e($kelas->tingkat); ?></td>
                                <td><?php echo e($kelas->paralel ?? '-'); ?></td>
                                <td><strong><?php echo e($kelas->nama_kelas); ?></strong></td>
                                <td>
                                    <?php if($kelas->status == 'aktif'): ?>
                                        <span class="badge badge-success">Aktif</span>
                                    <?php else: ?>
                                        <span class="badge badge-secondary">Nonaktif</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <form action="<?php echo e(route('kelas.toggle-status', $kelas->id)); ?>" method="POST" class="d-inline">
                                        <?php echo csrf_field(); ?>
                                        <button type="submit" class="btn btn-sm btn-<?php echo e($kelas->status == 'aktif' ? 'danger' : 'success'); ?>" title="Ubah Status">
                                            <i class="fas fa-power-off"></i>
                                        </button>
                                    </form>
                                    <button type="button" class="btn btn-danger btn-sm m-0" onclick="confirmDelete(<?php echo e($kelas->id); ?>)">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <form id="delete-form-<?php echo e($kelas->id); ?>" action="<?php echo e(route('kelas.destroy', $kelas->id)); ?>" method="POST" class="d-none">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('DELETE'); ?>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr>
                                <td colspan="7" class="text-center">Belum ada data kelas.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <!-- /.card-body -->
            <div class="card-footer clearfix">
                <?php echo e($kelasList->links('pagination::bootstrap-4')); ?>

            </div>
        </div>
        <!-- /.card -->
    </div>
</div>

<!-- Add Modal -->
<div class="modal fade" id="modal-add">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="<?php echo e(route('kelas.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-header">
                    <h4 class="modal-title">Tambah Data Master Kelas</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="jenjang">Jenjang Pendidikan</label>
                        <select name="jenjang" class="form-control" id="add-jenjang" required>
                            <option value="">Pilih Jenjang</option>
                            <option value="TK">TK</option>
                            <option value="SD">SD</option>
                            <option value="SMP">SMP</option>
                            <option value="SMA">SMA</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="tingkat">Tingkat Kelas</label>
                        <select name="tingkat" class="form-control" id="add-tingkat" required>
                            <option value="">Pilih Jenjang Terlebih Dahulu</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="paralel">Paralel (Opsional)</label>
                        <input type="text" name="paralel" class="form-control" placeholder="Contoh: A, B, atau 1, 2">
                        <small class="text-muted">Ketik langsung (contoh: A)</small>
                    </div>
                </div>
                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- /.modal -->

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    // Logic untuk cascade pilihan Tingkat berdasarkan Jenjang
    const tingkatOptions = {
        'TK': ['A', 'B'],
        'SD': ['1', '2', '3', '4', '5', '6'],
        'SMP': ['7', '8', '9'],
        'SMA': ['10', '11', '12']
    };

    function populateTingkat(jenjangSelect, tingkatSelect, selectedValue = null) {
        const jenjang = jenjangSelect.val();
        tingkatSelect.empty();
        
        if(jenjang && tingkatOptions[jenjang]) {
            tingkatSelect.append('<option value="">Pilih Tingkat</option>');
            tingkatOptions[jenjang].forEach(function(tingkat) {
                const isSelected = (selectedValue === tingkat) ? 'selected' : '';
                tingkatSelect.append(`<option value="${tingkat}" ${isSelected}>${tingkat}</option>`);
            });
        } else {
            tingkatSelect.append('<option value="">Pilih Jenjang Terlebih Dahulu</option>');
        }
    }

    $(document).ready(function() {
        // Add Modal
        $('#add-jenjang').change(function() {
            populateTingkat($(this), $('#add-tingkat'));
        });
    });

    // Menampilkan pesan sukses atau error
    <?php if(session('success')): ?>
        Swal.fire({
            icon: 'success',
            title: 'Berhasil',
            text: '<?php echo e(session('success')); ?>',
            timer: 3000,
            showConfirmButton: false
        });
    <?php endif; ?>

    <?php if(session('error')): ?>
        Swal.fire({
            icon: 'error',
            title: 'Gagal',
            text: '<?php echo e(session('error')); ?>',
            timer: 3000,
            showConfirmButton: false
        });
    <?php endif; ?>

    <?php if($errors->any()): ?>
        Swal.fire({
            icon: 'error',
            title: 'Terdapat Kesalahan',
            html: '<?php echo implode("<br>", $errors->all()); ?>'
        });
    <?php endif; ?>

    function confirmDelete(id) {
        Swal.fire({
            title: 'Apakah Anda yakin?',
            text: "Data ini akan dihapus secara permanen!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Ya, hapus!',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById('delete-form-' + id).submit();
            }
        });
    }
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Herd\spp_app\resources\views/kelas/index.blade.php ENDPATH**/ ?>