<?php $pageTitle = 'Global Search'; ?>
<div class="mb-4"><p class="text-secondary">Search across students, certificates, and courses</p></div>
<div class="card-cix mb-4">
    <form method="GET" action="<?= base_url('admin/search') ?>" class="row g-3 align-items-end">
        <div class="col-md-8"><label class="form-label-cix">Search Query</label><input type="text" name="q" class="form-control-cix" placeholder="Student #, name, email, certificate #, course..." value="<?= e($query ?? '') ?>" autofocus></div>
        <div class="col-md-2"><button type="submit" class="btn-cix btn-cix-primary"><i class="fas fa-search"></i> Search</button></div>
    </form>
</div>
<?php if (!empty($query)): ?>
    <?php if (!empty($results['students'])): ?>
    <div class="card-cix mb-4">
        <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-user-graduate text-orange me-2"></i> Students (<?= count($results['students']) ?>)</h5></div>
        <div class="table-responsive"><table class="table-cix"><thead><tr><th>#</th><th>Name</th><th>Email</th><th>Enrollment</th></tr></thead>
        <tbody><?php foreach ($results['students'] as $s): ?>
        <tr><td style="color:var(--secondary);font-weight:600"><?= e($s['student_number']) ?></td><td><?= e($s['first_name'] . ' ' . $s['last_name']) ?></td><td class="text-secondary"><?= e($s['email']) ?></td><td><span class="badge-cix badge-<?= $s['enrollment_status'] ?>"><?= ucfirst($s['enrollment_status']) ?></span></td></tr>
        <?php endforeach; ?></tbody></table></div>
    </div>
    <?php endif; ?>
    <?php if (!empty($results['certificates'])): ?>
    <div class="card-cix mb-4">
        <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-award text-orange me-2"></i> Certificates (<?= count($results['certificates']) ?>)</h5></div>
        <div class="table-responsive"><table class="table-cix"><thead><tr><th>Certificate #</th><th>Student</th><th>Course</th><th>Date</th></tr></thead>
        <tbody><?php foreach ($results['certificates'] as $c): ?>
        <tr><td style="color:var(--secondary);font-weight:600"><?= e($c['certificate_number']) ?></td><td><?= e($c['student_name']) ?></td><td class="text-secondary"><?= e($c['course_name']) ?></td><td class="text-secondary small"><?= e($c['issue_date']) ?></td></tr>
        <?php endforeach; ?></tbody></table></div>
    </div>
    <?php endif; ?>
    <?php if (!empty($results['courses'])): ?>
    <div class="card-cix mb-4">
        <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-book text-orange me-2"></i> Courses (<?= count($results['courses']) ?>)</h5></div>
        <div class="table-responsive"><table class="table-cix"><thead><tr><th>Title</th><th>Status</th></tr></thead>
        <tbody><?php foreach ($results['courses'] as $c): ?>
        <tr><td><strong><?= e($c['title']) ?></strong></td><td><span class="badge-cix <?= $c['is_published'] ? 'badge-active' : 'badge-pending' ?>"><?= $c['is_published'] ? 'Published' : 'Draft' ?></span></td></tr>
        <?php endforeach; ?></tbody></table></div>
    </div>
    <?php endif; ?>
    <?php if (empty($results['students']) && empty($results['certificates']) && empty($results['courses'])): ?>
    <div class="empty-state"><i class="fas fa-search"></i><h5>No Results</h5><p>No matching records found for "<?= e($query) ?>"</p></div>
    <?php endif; ?>
<?php endif; ?>
