<?php include __DIR__ . '/../../views/layouts/header.php'; ?>
<div class="wrapper">
<?php include __DIR__ . '/../../views/layouts/sidebar.php'; ?>
<div class="main-content">
<?php include __DIR__ . '/../../views/layouts/topbar.php'; ?>
<div class="page-content">
  <div class="page-header">
    <h1><i class="bi bi-search me-2 text-primary"></i>Pencarian</h1>
  </div>

  <!-- Form Pencarian -->
  <div class="card mb-4">
    <div class="card-body">
      <form method="GET" class="row g-3 align-items-end">
        <input type="hidden" name="page" value="pencarian">
        <div class="col-12 col-md-5">
          <label class="form-label">Kata Kunci</label>
          <div class="input-group">
            <span class="input-group-text"><i class="bi bi-search"></i></span>
            <input type="text" class="form-control form-control-lg" name="q"
                   value="<?= e($query) ?>" placeholder="Cari nama, NIS, NISN, NIP...">
          </div>
        </div>
        <div class="col-6 col-md-2">
          <label class="form-label">Cari di</label>
          <select class="form-select" name="type">
            <option value="siswa" <?= $type === 'siswa' ? 'selected' : '' ?>>Siswa</option>
            <option value="guru" <?= $type === 'guru' ? 'selected' : '' ?>>Guru</option>
          </select>
        </div>
        <?php if ($type === 'siswa'): ?>
        <div class="col-6 col-md-3">
          <label class="form-label">Filter Kelas</label>
          <select class="form-select" name="kelas_id">
            <option value="">Semua Kelas</option>
            <?php foreach ($kelasList as $k): ?>
            <option value="<?= $k['id'] ?>" <?= ($kelasId ?? 0) == $k['id'] ? 'selected' : '' ?>>
              <?= e($k['nama_kelas']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <?php endif; ?>
        <div class="col-auto">
          <button type="submit" class="btn btn-primary btn-lg">
            <i class="bi bi-search me-1"></i> Cari
          </button>
        </div>
      </form>
    </div>
  </div>

  <!-- Hasil -->
  <?php if ($query || ($kelasId ?? 0)): ?>
  <div class="card">
    <div class="card-header">
      <h6 class="mb-0">
        Hasil Pencarian
        <span class="badge bg-primary ms-1"><?= $total ?> ditemukan</span>
        <?php if ($query): ?>
        <span class="text-muted fw-400 ms-1">untuk "<?= e($query) ?>"</span>
        <?php endif; ?>
      </h6>
    </div>
    <div class="card-body p-0">
      <?php if (empty($results)): ?>
        <div class="empty-state">
          <div class="empty-icon">🔍</div>
          <h6>Tidak ada hasil</h6>
          <p class="text-muted">Coba kata kunci yang berbeda</p>
        </div>
      <?php elseif ($type === 'siswa'): ?>
      <div class="table-responsive">
        <table class="table mb-0">
          <thead>
            <tr><th>Nama</th><th>NIS</th><th>NISN</th><th>Kelas</th><th>Status</th><th>Aksi</th></tr>
          </thead>
          <tbody>
            <?php foreach ($results as $s): ?>
            <tr>
              <td class="fw-600"><?= e($s['nama']) ?></td>
              <td><?= e($s['nis'] ?? '-') ?></td>
              <td><?= e($s['nisn'] ?? '-') ?></td>
              <td><?= e($s['nama_kelas'] ?? '-') ?></td>
              <td><span class="badge badge-<?= strtolower(str_replace(' ', '-', $s['status'])) ?>"><?= e($s['status']) ?></span></td>
              <td>
                <a href="<?= BASE_URL ?>/index.php?page=siswa_detail&id=<?= $s['id'] ?>"
                   class="btn btn-sm btn-outline-info btn-icon"><i class="bi bi-eye"></i></a>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <?php else: ?>
      <div class="table-responsive">
        <table class="table mb-0">
          <thead>
            <tr><th>Nama</th><th>NIP</th><th>Mata Pelajaran</th><th>Status</th><th>Aksi</th></tr>
          </thead>
          <tbody>
            <?php foreach ($results as $g): ?>
            <tr>
              <td class="fw-600"><?= e($g['nama']) ?></td>
              <td><?= e($g['nip'] ?? '-') ?></td>
              <td><?= e($g['mata_pelajaran'] ?? '-') ?></td>
              <td><span class="badge <?= $g['status'] === 'Aktif' ? 'badge-aktif' : 'badge-tidak-aktif' ?>"><?= e($g['status']) ?></span></td>
              <td>
                <a href="<?= BASE_URL ?>/index.php?page=guru_detail&id=<?= $g['id'] ?>"
                   class="btn btn-sm btn-outline-info btn-icon"><i class="bi bi-eye"></i></a>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <?php endif; ?>
    </div>
  </div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
