<?php $__env->startSection('title', 'Data Sekolah - MagangTime'); ?>
<?php $__env->startSection('page-title', 'Data Sekolah'); ?>

<?php $__env->startSection('content'); ?>
<div class="card">
    <div class="card-header bg-white">
        <div class="row align-items-center">
            <div class="col-md-6">
                <h5 class="mb-0"><i class="fas fa-school me-2"></i>Kelola Data Sekolah</h5>
            </div>
            <div class="col-md-6 text-end">
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addSekolahModal">
                    <i class="fas fa-plus me-2"></i>Tambah Sekolah
                </button>
            </div>
        </div>
        
        <div class="row mt-3">
            <div class="col-md-8">
                <form method="GET" action="<?php echo e(route('admin.sekolah')); ?>" class="d-flex">
                    <input type="text" name="search" class="form-control me-2" 
                           placeholder="Cari nama sekolah..." 
                           value="<?php echo e(request('search')); ?>">
                    <button type="submit" class="btn btn-outline-primary"><i class="fas fa-search"></i></button>
                    <?php if(request('search')): ?>
                        <a href="<?php echo e(route('admin.sekolah')); ?>" class="btn btn-outline-secondary ms-2"><i class="fas fa-times"></i></a>
                    <?php endif; ?>
                </form>
            </div>
        </div>
    </div>
    
    <div class="card-body">
        <?php if($errors->any()): ?>
            <div class="alert alert-danger">
                <ul class="mb-0"><?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><li><?php echo e($error); ?></li><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></ul>
            </div>
        <?php endif; ?>
        
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th width="5%">No</th>
                        <th>Nama Sekolah</th>
                        <th width="15%">Jumlah Siswa</th>
                        <th width="15%">Tanggal Dibuat</th>
                        <th width="8%">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $sekolah; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($sekolah->firstItem() + $index); ?></td>
                        <td><strong><?php echo e($s->nama_sekolah); ?></strong></td>
                        <td class="text-center">
                            <span class="badge bg-primary"><?php echo e($s->siswa_count); ?> siswa</span>
                        </td>
                        <td class="text-center"><?php echo e($s->created_at->format('d/m/Y')); ?></td>
                        <td>
                            <div class="btn-group" role="group">
                                <button class="btn btn-sm btn-warning" onclick="editSekolah(<?php echo e($s->id); ?>, '<?php echo e($s->nama_sekolah); ?>')"><i class="fas fa-edit"></i></button>
                                <button class="btn btn-sm btn-danger" onclick="confirmDelete(<?php echo e($s->id); ?>, '<?php echo e($s->nama_sekolah); ?>', <?php echo e($s->siswa_count); ?>)"><i class="fas fa-trash"></i></button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">
                            <i class="fas fa-school fa-3x mb-3"></i>
                            <p><?php echo e(request('search') ? 'Tidak ditemukan data sekolah' : 'Belum ada data sekolah yang terdaftar'); ?></p>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <?php if($sekolah->hasPages()): ?>
            <?php echo e($sekolah->appends(request()->query())->links('pagination::bootstrap-4')); ?>

        <?php endif; ?>
    </div>
</div>

<!-- Add School Modal -->
<div class="modal fade" id="addSekolahModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Tambah Sekolah</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?php echo e(route('admin.sekolah.store')); ?>">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Sekolah <span class="text-danger">*</span></label>
                        <input type="text" name="nama_sekolah" class="form-control" 
                               placeholder="Contoh: SMK Negeri 1 Banjarmasin" 
                               value="<?php echo e(old('nama_sekolah')); ?>" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit School Modal -->
<div class="modal fade" id="editSekolahModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Sekolah</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="editSekolahForm" method="POST">
                <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Sekolah <span class="text-danger">*</span></label>
                        <input type="text" name="nama_sekolah" id="edit_nama_sekolah" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-warning">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Konfirmasi Hapus</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Apakah Anda yakin ingin menghapus sekolah <strong id="deleteName"></strong>?</p>
                <div class="alert alert-warning" id="deleteWarning" style="display: none;">
                    <i class="fas fa-info-circle me-2"></i>
                    <strong>Peringatan:</strong> Sekolah ini masih memiliki <span id="siswaCount"></span> siswa dan tidak dapat dihapus.
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <form id="deleteForm" method="POST" class="d-inline">
                    <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                    <button type="submit" class="btn btn-danger" id="deleteButton">Hapus</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php if($errors->any()): ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    new bootstrap.Modal(document.getElementById('addSekolahModal')).show();
});
</script>
<?php endif; ?>

<script>
function editSekolah(id, nama_sekolah) {
    document.getElementById('edit_nama_sekolah').value = nama_sekolah;
    document.getElementById('editSekolahForm').action = `<?php echo e(url('admin/sekolah')); ?>/${id}/update`;
    new bootstrap.Modal(document.getElementById('editSekolahModal')).show();
}

function confirmDelete(id, nama_sekolah, siswaCount) {
    document.getElementById('deleteName').textContent = nama_sekolah;
    document.getElementById('deleteForm').action = `<?php echo e(url('admin/sekolah')); ?>/${id}/delete`;
    
    const warning = document.getElementById('deleteWarning');
    const deleteButton = document.getElementById('deleteButton');
    
    if (siswaCount > 0) {
        document.getElementById('siswaCount').textContent = siswaCount;
        warning.style.display = 'block';
        deleteButton.style.display = 'none';
    } else {
        warning.style.display = 'none';
        deleteButton.style.display = 'inline-block';
    }
    
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\presensi-magang\resources\views/admin/sekolah.blade.php ENDPATH**/ ?>