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

<?php $__env->startSection('content'); ?>
<div class="card">
    <div class="card-header bg-white">
        <div class="row align-items-center mb-3">
            <div class="col-md-6">
                <h5 class="mb-0"><i class="fas fa-user-graduate me-2"></i>Data Siswa Magang</h5>
            </div>
            <div class="col-md-6 text-end">
                <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addSiswaModal">
                    <i class="fas fa-plus me-1"></i>Tambah Siswa
                </button>
            </div>
        </div>

        <!-- Form Filter dan Pencarian -->
        <form method="GET" action="<?php echo e(route('admin.siswa')); ?>">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Cari Siswa</label>
                    <input type="text" name="search" class="form-control" 
                           placeholder="Cari nama siswa atau sekolah..." 
                           value="<?php echo e(request('search')); ?>">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Jenis Kelamin</label>
                    <select name="jk" class="form-select">
                        <option value="">Semua JK</option>
                        <option value="L" <?php echo e(request('jk') == 'L' ? 'selected' : ''); ?>>Laki-laki</option>
                        <option value="P" <?php echo e(request('jk') == 'P' ? 'selected' : ''); ?>>Perempuan</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Sekolah</label>
                    <select name="sekolah" class="form-select">
                        <option value="">Semua Sekolah</option>
                        <?php $__currentLoopData = $sekolah; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($sch->id); ?>" <?php echo e(request('sekolah') == $sch->id ? 'selected' : ''); ?>>
                                <?php echo e($sch->nama_sekolah); ?>

                            </option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Status Akun</label>
                    <select name="status_akun" class="form-select">
                        <option value="">Semua Status</option>
                        <option value="ada" <?php echo e(request('status_akun') == 'ada' ? 'selected' : ''); ?>>Sudah Ada Akun</option>
                        <option value="belum" <?php echo e(request('status_akun') == 'belum' ? 'selected' : ''); ?>>Belum Ada Akun</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <div class="btn-group w-100" role="group">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i></button>
                        <?php if(request()->hasAny(['search', 'jk', 'sekolah', 'status_akun'])): ?>
                            <a href="<?php echo e(route('admin.siswa')); ?>" class="btn btn-outline-secondary"><i class="fas fa-times"></i></a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </form>
    </div>
    
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th width="5%">No</th>
                        <th>Nama Siswa</th>
                        <th>Jenis Kelamin</th>
                        <th>Sekolah</th>
                        <th>Periode Magang</th>
                        <th>Status Akun</th>
                        <th width="15%">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $siswa; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($siswa->firstItem() + $i); ?></td>
                        <td>
                            <div class="fw-bold"><?php echo e($s->nama_siswa); ?></div>
                            <small class="text-muted"><?php echo e($s->no_telp_siswa); ?></small>
                        </td>
                        <td class="text-center">
                            <span class="badge bg-<?php echo e($s->jk == 'L' ? 'primary' : 'danger'); ?>"><?php echo e($s->jk == 'L' ? 'Laki-laki' : 'Perempuan'); ?></span>
                        </td>
                        <td><?php echo e($s->sekolah->nama_sekolah); ?></td>
                        <td>
                            <small><?php echo e($s->mulai_magang->format('d/m/Y')); ?> s/d <?php echo e($s->selesai_magang->format('d/m/Y')); ?></small>
                        </td>
                        <td class="text-center">
                            <?php if($s->user): ?>
                                <span class="badge bg-success"><?php echo e($s->user->username); ?></span>
                            <?php else: ?>
                                <span class="badge bg-secondary">Belum ada akun</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="<?php echo e(route('admin.detail.siswa', $s->id)); ?>" class="btn btn-sm btn-info" title="Detail">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <button class="btn btn-sm btn-warning" onclick="editSiswa(<?php echo e($s->id); ?>)" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <a href="<?php echo e(route('admin.export.pdf', $s->id)); ?>" class="btn btn-sm btn-success" title="Export PDF">
                                    <i class="fas fa-file-pdf"></i>
                                </a>
                                <button class="btn btn-sm btn-danger" onclick="confirmDelete(<?php echo e($s->id); ?>, '<?php echo e($s->nama_siswa); ?>')" title="Hapus">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">
                            <?php if(request()->hasAny(['search', 'jk', 'sekolah', 'status_akun'])): ?>
                                <i class="fas fa-search fa-3x mb-3"></i>
                                <p>Tidak ditemukan data siswa dengan kriteria yang dipilih</p>
                                <a href="<?php echo e(route('admin.siswa')); ?>" class="btn btn-primary">Reset Filter</a>
                            <?php else: ?>
                                <i class="fas fa-user-graduate fa-3x mb-3"></i>
                                <p>Belum ada data siswa magang</p>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        <?php if($siswa->hasPages()): ?>
            <?php echo e($siswa->appends(request()->query())->links('pagination::bootstrap-4')); ?>

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

<!-- Add Modal -->
<div class="modal fade" id="addSiswaModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Tambah Siswa Magang</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?php echo e(route('admin.create.siswa')); ?>" id="addSiswaForm">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Nama Siswa <span class="text-danger">*</span></label>
                            <input type="text" name="nama_siswa" class="form-control <?php $__errorArgs = ['nama_siswa'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                                   value="<?php echo e(old('nama_siswa')); ?>" required>
                            <?php $__errorArgs = ['nama_siswa'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Jenis Kelamin <span class="text-danger">*</span></label>
                            <select name="jk" class="form-select <?php $__errorArgs = ['jk'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                <option value="">Pilih</option>
                                <option value="L" <?php echo e(old('jk') == 'L' ? 'selected' : ''); ?>>Laki-laki</option>
                                <option value="P" <?php echo e(old('jk') == 'P' ? 'selected' : ''); ?>>Perempuan</option>
                            </select>
                            <?php $__errorArgs = ['jk'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">No. Telp Siswa <span class="text-danger">*</span></label>
                            <input type="text" name="no_telp_siswa" class="form-control <?php $__errorArgs = ['no_telp_siswa'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                                   value="<?php echo e(old('no_telp_siswa')); ?>" required>
                            <?php $__errorArgs = ['no_telp_siswa'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">No. Telp Pembimbing <span class="text-danger">*</span></label>
                            <input type="text" name="no_telp_pembimbing" class="form-control <?php $__errorArgs = ['no_telp_pembimbing'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                                   value="<?php echo e(old('no_telp_pembimbing')); ?>" required>
                            <?php $__errorArgs = ['no_telp_pembimbing'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label class="form-label">Sekolah <span class="text-danger">*</span></label>
                            <select name="id_sekolah" class="form-select <?php $__errorArgs = ['id_sekolah'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                <option value="">Pilih Sekolah</option>
                                <?php $__currentLoopData = $sekolah; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($sch->id); ?>" <?php echo e(old('id_sekolah') == $sch->id ? 'selected' : ''); ?>><?php echo e($sch->nama_sekolah); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['id_sekolah'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Mulai Magang <span class="text-danger">*</span></label>
                            <input type="date" name="mulai_magang" class="form-control <?php $__errorArgs = ['mulai_magang'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                                   value="<?php echo e(old('mulai_magang')); ?>" required>
                            <?php $__errorArgs = ['mulai_magang'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Selesai Magang <span class="text-danger">*</span></label>
                            <input type="date" name="selesai_magang" class="form-control <?php $__errorArgs = ['selesai_magang'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" 
                                   value="<?php echo e(old('selesai_magang')); ?>" required>
                            <?php $__errorArgs = ['selesai_magang'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                    </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 Modal -->
<div class="modal fade" id="editSiswaModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form method="POST" id="editSiswaForm">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?>
                <div class="modal-header">
                    <h5 class="modal-title"><i class="fas fa-edit me-2"></i>Edit Siswa Magang</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <!-- Error Alert -->
                    <div class="alert alert-danger d-none" id="editErrorAlert">
                        <ul id="editErrorList" class="mb-0"></ul>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Nama Siswa <span class="text-danger">*</span></label>
                            <input type="text" name="nama_siswa" id="editNamaSiswa" class="form-control" required>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Jenis Kelamin <span class="text-danger">*</span></label>
                            <select name="jk" id="editJk" class="form-select" required>
                                <option value="">Pilih Jenis Kelamin</option>
                                <option value="L">Laki-laki</option>
                                <option value="P">Perempuan</option>
                            </select>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">No. Telp Siswa <span class="text-danger">*</span></label>
                            <input type="text" name="no_telp_siswa" id="editNoTelpSiswa" class="form-control" required>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">No. Telp Pembimbing <span class="text-danger">*</span></label>
                            <input type="text" name="no_telp_pembimbing" id="editNoTelpPembimbing" class="form-control" required>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-12 mb-3">
                            <label class="form-label">Sekolah <span class="text-danger">*</span></label>
                            <select name="id_sekolah" id="editIdSekolah" class="form-select" required>
                                <option value="">Pilih Sekolah</option>
                                <?php $__currentLoopData = $sekolah; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($s->id); ?>"><?php echo e($s->nama_sekolah); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Mulai Magang <span class="text-danger">*</span></label>
                            <input type="date" name="mulai_magang" id="editMulaiMagang" class="form-control" required>
                            <div class="invalid-feedback"></div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Selesai Magang <span class="text-danger">*</span></label>
                            <input type="date" name="selesai_magang" id="editSelesaiMagang" class="form-control" required>
                            <div class="invalid-feedback"></div>
                        </div>
                    </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 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 siswa:</p>
                <div class="card bg-light">
                    <div class="card-body">
                        <strong>Nama:</strong> <span id="deleteName"></span>
                    </div>
                </div>
                <div class="alert alert-warning mt-3">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <strong>Peringatan:</strong> Data siswa dan riwayat presensi akan terhapus permanen.
                </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">Hapus</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
// Fungsi untuk membersihkan error
function clearErrors(modalId) {
    const modal = document.getElementById(modalId);
    modal.querySelectorAll('.is-invalid').forEach(el => el.classList.remove('is-invalid'));
    modal.querySelectorAll('.invalid-feedback').forEach(el => el.textContent = '');
    
    if (modalId === 'editSiswaModal') {
        const errorAlert = document.getElementById('editErrorAlert');
        if (errorAlert) {
            errorAlert.classList.add('d-none');
            document.getElementById('editErrorList').innerHTML = '';
        }
    }
}

// Auto-open add modal jika ada validation errors
document.addEventListener('DOMContentLoaded', function() {
    <?php if($errors->any()): ?>
        new bootstrap.Modal(document.getElementById('addSiswaModal')).show();
    <?php endif; ?>
});

// Clear form saat modal ditutup
document.getElementById('addSiswaModal').addEventListener('hidden.bs.modal', function() {
    document.getElementById('addSiswaForm').reset();
    clearErrors('addSiswaModal');
});

document.getElementById('editSiswaModal').addEventListener('hidden.bs.modal', function() {
    clearErrors('editSiswaModal');
});

function editSiswa(id) {
    clearErrors('editSiswaModal');
    
    fetch(`/admin/siswa-magang/${id}/edit`)
        .then(response => {
            if (!response.ok) throw new Error('Network response was not ok');
            return response.json();
        })
        .then(data => {
            // Populate form fields
            Object.keys(data).forEach(key => {
                const element = document.getElementById(`edit${key.charAt(0).toUpperCase() + key.slice(1).replace(/_(.)/g, (_, c) => c.toUpperCase())}`);
                if (element) element.value = data[key] || '';
            });
            
            document.getElementById('editSiswaForm').action = `/admin/siswa-magang/${id}/update`;
            new bootstrap.Modal(document.getElementById('editSiswaModal')).show();
        })
        .catch(error => {
            console.error('Error:', error);
            alert('Terjadi kesalahan saat mengambil data siswa: ' + error.message);
        });
}

// Handle edit form submission
document.getElementById('editSiswaForm').addEventListener('submit', function(e) {
    e.preventDefault();
    
    const form = this;
    const formData = new FormData(form);
    const submitBtn = form.querySelector('button[type="submit"]');
    const originalText = submitBtn.innerHTML;
    
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span>Updating...';
    
    clearErrors('editSiswaModal');
    
    fetch(form.action, {
        method: 'POST',
        body: formData,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
            'Accept': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            bootstrap.Modal.getInstance(document.getElementById('editSiswaModal')).hide();
            location.reload();
        } else {
            if (data.errors) {
                displayErrors(data.errors);
            } else {
                showAlert([data.message || 'Terjadi kesalahan']);
            }
        }
    })
    .catch(error => {
        console.error('Error:', error);
        showAlert(['Terjadi kesalahan saat memperbarui data']);
    })
    .finally(() => {
        submitBtn.disabled = false;
        submitBtn.innerHTML = originalText;
    });
});

function displayErrors(errors) {
    const errorList = [];
    
    Object.keys(errors).forEach(field => {
        const input = document.querySelector(`#editSiswaModal [name="${field}"]`);
        const feedback = input?.nextElementSibling;
        
        if (input) {
            input.classList.add('is-invalid');
            if (feedback?.classList.contains('invalid-feedback')) {
                feedback.textContent = errors[field][0];
            }
        }
        
        errors[field].forEach(message => errorList.push(message));
    });
    
    showAlert(errorList);
}

function showAlert(messages) {
    const errorAlert = document.getElementById('editErrorAlert');
    const errorList = document.getElementById('editErrorList');
    
    if (errorAlert && errorList) {
        errorList.innerHTML = messages.map(msg => `<li>${msg}</li>`).join('');
        errorAlert.classList.remove('d-none');
    }
}

function confirmDelete(id, name) {
    document.getElementById('deleteName').textContent = name;
    document.getElementById('deleteForm').action = `/admin/siswa-magang/${id}/delete`;
    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/siswa.blade.php ENDPATH**/ ?>