<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row">
    <div class="col-12">
        <div class="card mb-4 shadow-sm border-0 rounded-3">
            <div class="card-header bg-white py-3 d-flex align-items-center gap-2 border-bottom">
                <i class="bi bi-book text-primary fs-5"></i>
                <span class="fw-semibold fs-5 text-dark m-0">Buku Induk Siswa</span>
            </div>
            <div class="card-body py-4 px-4 bg-light-subtle">
                <p class="text-muted small mb-3">
                    Masukkan NIS, NISN, atau Nama Siswa pada kolom pencarian di bawah untuk memanggil Lembar Buku Induk & Nilai Rapor.
                </p>
                <form method="GET" action="/buku-induk" class="row g-2">
                    <div class="col-md-9">
                        <div class="input-group input-group-lg shadow-sm rounded-pill overflow-hidden">
                            <span class="input-group-text bg-white border-end-0 ps-3">
                                <i class="bi bi-search text-muted"></i>
                            </span>
                            <input type="text" name="q" class="form-control border-start-0 py-3" 
                                   value="<?= esc($keyword ?? '') ?>"
                                   placeholder="Ketik NIS, NISN, atau nama lengkap siswa..." required>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <button type="submit" class="btn btn-primary btn-lg w-100 rounded-pill shadow-sm">
                            <i class="bi bi-search me-1"></i> Cari Data
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Hasil Pencarian -->
    <?php if (isset($keyword)): ?>
        <div class="col-12">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-header bg-white py-3 d-flex align-items-center justify-content-between border-bottom">
                    <div class="d-flex align-items-center gap-2">
                        <i class="bi bi-people-fill text-primary"></i>
                        <span class="fw-semibold text-dark">Hasil Pencarian untuk: "<?= esc($keyword) ?>"</span>
                    </div>
                    <span class="badge bg-primary rounded-pill px-3 py-2"><?= count($students) ?> Siswa Ditemukan</span>
                </div>
                <div class="card-body p-0 overflow-auto" style="max-height: 500px;">
                    <?php if (empty($students)): ?>
                        <div class="text-center py-5">
                            <div class="text-muted mb-3" style="font-size: 3rem;">
                                <i class="bi bi-person-x"></i>
                            </div>
                            <h5 class="text-secondary fw-semibold">Tidak ditemukan hasil pencarian</h5>
                            <p class="text-muted small">Coba cari dengan kata kunci lain yang lebih spesifik.</p>
                        </div>
                    <?php else: ?>
                        <table class="table table-hover table-striped align-middle mb-0">
                            <thead class="table-light sticky-top">
                                <tr>
                                    <th width="5%" class="text-center">No</th>
                                    <th width="15%">NIS / NISN</th>
                                    <th width="35%">Nama Lengkap</th>
                                    <th width="15%">Kelas Aktif</th>
                                    <th width="10%" class="text-center">L/P</th>
                                    <th width="10%" class="text-center">Status</th>
                                    <th width="10%" class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no = 1; foreach ($students as $s): ?>
                                    <tr>
                                        <td class="text-center fw-medium"><?= $no++ ?></td>
                                        <td>
                                            <div class="fw-semibold text-dark"><?= esc($s['nis'] ?? '-') ?></div>
                                            <div class="text-muted small">NISN: <?= esc($s['nisn'] ?? '-') ?></div>
                                        </td>
                                        <td class="fw-bold text-primary"><?= esc($s['full_name']) ?></td>
                                        <td>
                                            <?php if (!empty($s['classroom_name'])): ?>
                                                <span class="fw-semibold text-dark"><i class="bi bi-door-open me-1 text-secondary"></i><?= esc($s['classroom_name']) ?></span>
                                            <?php else: ?>
                                                <span class="text-muted small">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center"><?= esc($s['gender'] == 'L' ? 'L' : 'P') ?></td>
                                        <td class="text-center">
                                            <?php
                                            $badgeColor = 'secondary';
                                            if ($s['status'] === 'Aktif') $badgeColor = 'success';
                                            elseif ($s['status'] === 'Alumni') $badgeColor = 'primary';
                                            elseif ($s['status'] === 'Mutasi') $badgeColor = 'warning';
                                            ?>
                                            <span class="badge bg-<?= $badgeColor ?>"><?= esc($s['status']) ?></span>
                                        </td>
                                        <td class="text-center">
                                            <div class="d-flex gap-1 justify-content-center">
                                                <a href="/buku-induk/view/<?= $s['id'] ?>" class="btn btn-sm btn-outline-primary px-3 rounded-pill">
                                                    <i class="bi bi-eye me-1"></i> Tinjau
                                                </a>
                                                <a href="/buku-induk/print/<?= $s['id'] ?>" target="_blank" class="btn btn-sm btn-success px-3 rounded-pill">
                                                    <i class="bi bi-printer me-1"></i> Cetak
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

<?= $this->endSection() ?>
