<?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 d-flex justify-content-between align-items-start flex-wrap gap-2">
    <div>
      <h1><i class="bi bi-people-fill me-2 text-primary"></i>Data Siswa</h1>
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/index.php?page=dashboard">Dashboard</a></li>
          <li class="breadcrumb-item active">Data Siswa</li>
        </ol>
      </nav>
    </div>
    <div class="d-flex gap-2 flex-wrap">
      <a href="<?= BASE_URL ?>/index.php?page=siswa_export&kelas_id=<?= $kelasId ?? 0 ?>&status=<?= urlencode($status) ?>"
         class="btn btn-success btn-sm">
        <i class="bi bi-file-earmark-excel me-1"></i> Export
      </a>
      <a href="<?= BASE_URL ?>/index.php?page=siswa_import" class="btn btn-warning btn-sm">
        <i class="bi bi-upload me-1"></i> Import
      </a>
      <a href="<?= BASE_URL ?>/index.php?page=siswa_tambah" class="btn btn-primary btn-sm">
        <i class="bi bi-plus-lg me-1"></i> Tambah Siswa
      </a>
    </div>
  </div>

  <?php if ($flash): ?>
  <div class="alert alert-<?= $flash['type'] ?> alert-dismissible alert-auto-dismiss fade show">
    <i class="bi bi-<?= $flash['type'] === 'success' ? 'check-circle' : 'exclamation-triangle' ?>-fill me-2"></i>
    <?= e($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
  </div>
  <?php endif; ?>

  <!-- Filter -->
  <div class="search-bar">
    <form method="GET" action="" class="row g-2 align-items-end">
      <input type="hidden" name="page" value="siswa">
      <div class="col-12 col-md-4">
        <label class="form-label mb-1">Cari Siswa</label>
        <div class="input-group">
          <span class="input-group-text"><i class="bi bi-search"></i></span>
          <input type="text" class="form-control" name="search" placeholder="Nama, NIS, NISN, NIK..."
                 value="<?= e($search) ?>">
        </div>
      </div>
      <div class="col-6 col-md-3">
        <label class="form-label mb-1">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>
      <div class="col-6 col-md-2">
        <label class="form-label mb-1">Status</label>
        <select class="form-select" name="status">
          <option value="">Semua Status</option>
          <?php foreach (['Aktif','Gaib','Tidak Aktif','Lulus','Pindah','Keluar'] as $s): ?>
          <option value="<?= $s ?>" <?= $status === $s ? 'selected' : '' ?>><?= $s ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-12 col-md-3 d-flex gap-2">
        <button type="submit" class="btn btn-primary flex-fill">
          <i class="bi bi-search me-1"></i> Cari
        </button>
        <a href="<?= BASE_URL ?>/index.php?page=siswa" class="btn btn-outline-secondary">
          <i class="bi bi-x-lg"></i>
        </a>
      </div>
    </form>
  </div>

  <!-- Table -->
  <div class="card">
    <div class="card-header">
      <h6 class="mb-0">
        <i class="bi bi-table me-2"></i>
        Daftar Siswa
        <span class="badge bg-primary ms-1"><?= formatAngka($total) ?></span>
      </h6>
    </div>
    <div class="card-body p-0">
      <?php if (empty($siswa)): ?>
        <div class="empty-state">
          <div class="empty-icon">👨‍🎓</div>
          <h6>Tidak ada data siswa</h6>
          <p class="text-muted">Tambahkan siswa baru atau ubah filter pencarian.</p>
          <a href="<?= BASE_URL ?>/index.php?page=siswa_tambah" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-lg me-1"></i> Tambah Siswa
          </a>
        </div>
      <?php else: ?>
      <div class="table-responsive">
        <table class="table" id="dataTable">
          <thead>
            <tr>
              <th style="width:50px">No</th>
              <th>Nama Siswa</th>
              <th>NIS / NISN</th>
              <th>Kelas</th>
              <th>Jenis Kelamin</th>
              <th>Status</th>
              <th style="width:130px">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($siswa as $i => $s): ?>
            <tr>
              <td class="text-muted"><?= nomorUrut($i, $curPage, 15) ?></td>
              <td>
                <div class="d-flex align-items-center gap-2">
                  <?php if (!empty($s['foto'])): ?>
                    <img src="<?= BASE_URL ?>/uploads/<?= e($s['foto']) ?>"
                         class="avatar avatar-sm" alt="Foto">
                  <?php else: ?>
                    <div class="avatar-placeholder avatar-sm" style="width:32px;height:32px;font-size:.75rem;">
                      <?= strtoupper(substr($s['nama'], 0, 1)) ?>
                    </div>
                  <?php endif; ?>
                  <div>
                    <div class="fw-600"><?= e($s['nama']) ?></div>
                    <div class="text-muted fs-sm"><?= e($s['tempat_lahir'] ?? '') ?></div>
                  </div>
                </div>
              </td>
              <td>
                <div><?= e($s['nis'] ?? '-') ?></div>
                <div class="text-muted fs-sm"><?= e($s['nisn'] ?? '-') ?></div>
              </td>
              <td><?= e($s['nama_kelas'] ?? '-') ?></td>
              <td>
                <?php if ($s['jenis_kelamin'] === 'Laki-laki'): ?>
                  <span class="badge bg-primary-subtle text-primary">
                    <i class="bi bi-gender-male me-1"></i>L
                  </span>
                <?php else: ?>
                  <span class="badge bg-danger-subtle text-danger">
                    <i class="bi bi-gender-female me-1"></i>P
                  </span>
                <?php endif; ?>
              </td>
              <td>
                <?php
                $statusClass = [
                  'Aktif'      => 'badge-aktif',
                  'Tidak Aktif'=> 'badge-tidak-aktif',
                  'Lulus'      => 'badge-lulus',
                  'Pindah'     => 'badge-pindah',
                  'Keluar'     => 'badge-keluar',
                ][$s['status']] ?? 'bg-secondary';
                ?>
                <?php if ($s['status'] === 'Gaib'): ?>
                  <span class="badge" style="background:#fef3c7;color:#d97706;">
                    <i class="bi bi-person-exclamation me-1"></i>Gaib
                  </span>
                <?php else: ?>
                  <span class="badge <?= $statusClass ?>"><?= e($s['status']) ?></span>
                <?php endif; ?>
              </td>
              <td>
                <div class="d-flex gap-1">
                  <a href="<?= BASE_URL ?>/index.php?page=siswa_detail&id=<?= $s['id'] ?>"
                     class="btn btn-sm btn-outline-info btn-icon" data-bs-toggle="tooltip" title="Detail">
                    <i class="bi bi-eye"></i>
                  </a>
                  <a href="<?= BASE_URL ?>/index.php?page=siswa_edit&id=<?= $s['id'] ?>"
                     class="btn btn-sm btn-outline-warning btn-icon" data-bs-toggle="tooltip" title="Edit">
                    <i class="bi bi-pencil"></i>
                  </a>
                  <?php if (in_array($s['status'], ['Pindah', 'Keluar'])): ?>
                  <a href="<?= BASE_URL ?>/index.php?page=siswa_surat_pindah&id=<?= $s['id'] ?>"
                     class="btn btn-sm btn-outline-secondary btn-icon"
                     data-bs-toggle="tooltip" title="Cetak Surat Pindah"
                     target="_blank">
                    <i class="bi bi-printer"></i>
                  </a>
                  <?php endif; ?>
                  <a href="<?= BASE_URL ?>/index.php?page=siswa_hapus&id=<?= $s['id'] ?>&csrf=<?= generateCSRF() ?>"
                     class="btn btn-sm btn-outline-danger btn-icon"
                     data-confirm="Hapus data siswa <?= e($s['nama']) ?>?"
                     data-bs-toggle="tooltip" title="Hapus">
                    <i class="bi bi-trash"></i>
                  </a>
                </div>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>

      <!-- Pagination -->
      <?php if ($pagination): ?>
      <div class="d-flex justify-content-between align-items-center px-3 py-2 border-top flex-wrap gap-2">
        <small class="text-muted">
          Menampilkan <?= count($siswa) ?> dari <?= formatAngka($total) ?> data
        </small>
        <?= $pagination ?>
      </div>
      <?php endif; ?>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
