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

<?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-users me-2"></i>Daftar Pegawai Magang</h5>
            </div>
            <div class="col-md-6 text-end">
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPegawaiModal">
                    <i class="fas fa-plus me-2"></i>Tambah Pegawai
                </button>
            </div>
        </div>
        
        <!-- Form Pencarian -->
        <div class="row mt-3">
            <div class="col-md-8">
                <form method="GET" action="<?php echo e(route('admin.pegawai')); ?>" class="d-flex">
                    <input type="text" name="search" class="form-control me-2" 
                           placeholder="Cari nama, username, sekolah, atau perusahaan..." 
                           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.pegawai')); ?>" class="btn btn-outline-secondary ms-2"><i class="fas fa-times"></i></a>
                    <?php endif; ?>
                </form>
            </div>
            <div class="col-md-4 text-end">
                <small class="text-muted">Menampilkan <?php echo e($pegawai->count()); ?> dari <?php echo e($pegawai->total()); ?> data</small>
            </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</th><th>Username</th><th>Sekolah</th><th>DU/DI</th><th width="8%">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $pegawai; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($pegawai->firstItem() + $index); ?></td>
                        <td><?php echo e($p->name); ?></td>
                        <td><?php echo e($p->username); ?></td>
                        <td><?php echo e($p->school ?? '–'); ?></td>
                        <td><?php echo e($p->company ?? '–'); ?></td>
                        <td>
                            <div class="btn-group" role="group">
                                <button class="btn btn-sm btn-warning" onclick="editPegawai(<?php echo e($p->id_user); ?>)"><i class="fas fa-edit"></i></button>
                                <button class="btn btn-sm btn-danger" onclick="confirmDelete(<?php echo e($p->id_user); ?>, '<?php echo e($p->name); ?>')"><i class="fas fa-trash"></i></button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">
                            <?php if(request('search')): ?>
                                <i class="fas fa-search fa-3x mb-3"></i>
                                <p>Tidak ditemukan pegawai dengan kata kunci "<?php echo e(request('search')); ?>"</p>
                                <a href="<?php echo e(route('admin.pegawai')); ?>" class="btn btn-primary">Reset Pencarian</a>
                            <?php else: ?>
                                <i class="fas fa-users fa-3x mb-3"></i>
                                <p>Belum ada data pegawai</p>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        <?php if($pegawai->hasPages()): ?>
            <?php echo e($pegawai->appends(request()->query())->links('pagination::bootstrap-4')); ?>

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

<!-- Add Pegawai Modal -->
<div class="modal fade" id="addPegawaiModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Tambah Pegawai Baru</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?php echo e(route('admin.create.pegawai')); ?>">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control" value="<?php echo e(old('name')); ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Username <span class="text-danger">*</span></label>
                        <input type="text" name="username" class="form-control" value="<?php echo e(old('username')); ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <input type="password" name="password" id="add_password" class="form-control" required>
                            <button type="button" class="btn btn-outline-secondary" onclick="togglePassword('add_password', 'add_password_icon')">
                                <i id="add_password_icon" class="fas fa-eye"></i>
                            </button>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Sekolah</label>
                        <input type="text" name="school" class="form-control" value="<?php echo e(old('school')); ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">DU/DI</label>
                        <input type="text" name="company" class="form-control" value="<?php echo e(old('company')); ?>">
                    </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 Pegawai Modal -->
<div class="modal fade" id="editPegawaiModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Pegawai</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="editPegawaiForm" method="POST">
                <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                        <input type="text" name="name" id="edit_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Username <span class="text-danger">*</span></label>
                        <input type="text" name="username" id="edit_username" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password <small class="text-muted">(kosongkan jika tidak ingin mengubah)</small></label>
                        <div class="input-group">
                            <input type="password" name="password" id="edit_password" class="form-control">
                            <button type="button" class="btn btn-outline-secondary" onclick="togglePassword('edit_password', 'edit_password_icon')">
                                <i id="edit_password_icon" class="fas fa-eye"></i>
                            </button>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Sekolah</label>
                        <input type="text" name="school" id="edit_school" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">DU/DI</label>
                        <input type="text" name="company" id="edit_company" class="form-control">
                    </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 pegawai <strong id="deleteName"></strong>?</p>
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <strong>Peringatan:</strong> Data pegawai dan seluruh riwayat presensinya 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>

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

<script>
function togglePassword(fieldId, iconId) {
    const field = document.getElementById(fieldId);
    const icon = document.getElementById(iconId);
    
    if (field.type === 'password') {
        field.type = 'text';
        icon.className = 'fas fa-eye-slash';
    } else {
        field.type = 'password';
        icon.className = 'fas fa-eye';
    }
}

function editPegawai(id) {
    fetch(`<?php echo e(url('admin/pegawai')); ?>/${id}/edit`)
        .then(response => response.json())
        .then(data => {
            document.getElementById('edit_name').value = data.name;
            document.getElementById('edit_username').value = data.username;
            document.getElementById('edit_school').value = data.school || '';
            document.getElementById('edit_company').value = data.company || '';
            document.getElementById('edit_password').value = '';
            document.getElementById('editPegawaiForm').action = `<?php echo e(url('admin/pegawai')); ?>/${id}/update`;
            new bootstrap.Modal(document.getElementById('editPegawaiModal')).show();
        });
}

function confirmDelete(id, name) {
    document.getElementById('deleteName').textContent = name;
    document.getElementById('deleteForm').action = `<?php echo e(url('admin/pegawai')); ?>/${id}/delete`;
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}

// Reset password visibility when modals close
document.getElementById('addPegawaiModal').addEventListener('hidden.bs.modal', () => {
    document.getElementById('add_password').type = 'password';
    document.getElementById('add_password_icon').className = 'fas fa-eye';
});

document.getElementById('editPegawaiModal').addEventListener('hidden.bs.modal', () => {
    document.getElementById('edit_password').type = 'password';
    document.getElementById('edit_password_icon').className = 'fas fa-eye';
});
</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/pegawai.blade.php ENDPATH**/ ?>