<?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-person-lines-fill me-2 text-primary"></i>Detail 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"><a href="<?= BASE_URL ?>/index.php?page=siswa">Data Siswa</a></li>
          <li class="breadcrumb-item active">Detail</li>
        </ol>
      </nav>
    </div>
    <div class="d-flex gap-2 flex-wrap no-print">
      <a href="<?= BASE_URL ?>/index.php?page=siswa_edit&id=<?= $siswa['id'] ?>"
         class="btn btn-warning btn-sm">
        <i class="bi bi-pencil me-1"></i> Edit
      </a>
      <?php if (in_array($siswa['status'], ['Pindah', 'Keluar'])): ?>
      <a href="<?= BASE_URL ?>/index.php?page=siswa_surat_pindah&id=<?= $siswa['id'] ?>"
         class="btn btn-outline-secondary btn-sm no-print" target="_blank">
        <i class="bi bi-printer me-1"></i> Cetak Surat Pindah
      </a>
      <?php endif; ?>
      <button class="btn btn-outline-secondary btn-sm" data-print>
        <i class="bi bi-printer me-1"></i> Cetak
      </button>
      <a href="<?= BASE_URL ?>/index.php?page=siswa" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left me-1"></i> Kembali
      </a>
    </div>
  </div>

  <!-- Header Card -->
  <div class="detail-header mb-4">
    <div class="d-flex align-items-center gap-3 flex-wrap">
      <?php if (!empty($siswa['foto'])): ?>
        <img src="<?= BASE_URL ?>/uploads/<?= e($siswa['foto']) ?>"
             class="rounded-circle" style="width:80px;height:80px;object-fit:cover;border:3px solid rgba(255,255,255,.3);">
      <?php else: ?>
        <div style="width:80px;height:80px;border-radius:50%;background:rgba(255,255,255,.2);display:flex;align-items:center;justify-content:center;font-size:2rem;font-weight:700;color:#fff;border:3px solid rgba(255,255,255,.3);">
          <?= strtoupper(substr($siswa['nama'], 0, 1)) ?>
        </div>
      <?php endif; ?>
      <div>
        <h3 class="mb-1 text-white fw-700"><?= e($siswa['nama']) ?></h3>
        <div class="d-flex gap-3 flex-wrap" style="opacity:.85;">
          <span><i class="bi bi-card-text me-1"></i>NIS: <?= e($siswa['nis'] ?? '-') ?></span>
          <span><i class="bi bi-building me-1"></i>Kelas: <?= e($siswa['nama_kelas'] ?? '-') ?></span>
          <span>
            <?php
            $statusClass = [
                'Aktif'      => 'bg-success',
                'Gaib'       => 'bg-warning text-dark',
                'Tidak Aktif'=> 'bg-danger',
                'Lulus'      => 'bg-info',
                'Pindah'     => 'bg-warning',
                'Keluar'     => 'bg-secondary'
            ][$siswa['status']] ?? 'bg-secondary';
            ?>
            <?php if ($siswa['status'] === 'Gaib'): ?>
              <span class="badge bg-warning text-dark">
                <i class="bi bi-person-exclamation me-1"></i>Gaib
              </span>
            <?php else: ?>
              <span class="badge <?= $statusClass ?>"><?= e($siswa['status']) ?></span>
            <?php endif; ?>
          </span>
        </div>
      </div>
    </div>
  </div>

  <!-- Tabs -->
  <ul class="nav nav-tabs mb-3" id="detailTabs">
    <li class="nav-item">
      <a class="nav-link active" data-bs-toggle="tab" href="#tab-pokok">
        <i class="bi bi-person me-1"></i>Data Pokok
      </a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#tab-alamat">
        <i class="bi bi-geo-alt me-1"></i>Alamat
      </a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#tab-ortu">
        <i class="bi bi-people me-1"></i>Orang Tua
      </a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#tab-wali">
        <i class="bi bi-person-check me-1"></i>Wali
      </a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#tab-nilai">
        <i class="bi bi-journal-text me-1"></i>Nilai
      </a>
    </li>
  </ul>

  <div class="tab-content">
    <!-- Tab Data Pokok -->
    <div class="tab-pane fade show active" id="tab-pokok">
      <?php if ($siswa['status'] === 'Gaib'): ?>
      <div class="alert alert-warning d-flex align-items-center gap-2 mb-3">
        <i class="bi bi-exclamation-triangle-fill fs-5"></i>
        <div>
          <strong>Siswa Gaib</strong> — Siswa ini terdaftar aktif di data namun
          tidak hadir / tidak diketahui keberadaannya.
          Hubungi orang tua/wali untuk konfirmasi.
        </div>
      </div>
      <?php endif; ?>
      <div class="card">
        <div class="card-body">
          <div class="row g-3">
            <?php
            $fields = [
              'No. Urut'      => $siswa['no'] ?? '-',
              'NIS'           => $siswa['nis'] ?? '-',
              'NISN'          => $siswa['nisn'] ?? '-',
              'NIK'           => $siswa['nik'] ?? '-',
              'Nama Lengkap'  => $siswa['nama'],
              'Jenis Kelamin' => $siswa['jenis_kelamin'],
              'Kelas'         => $siswa['nama_kelas'] ?? '-',
              'Agama'         => $siswa['agama'] ?? '-',
              'Tempat Lahir'  => $siswa['tempat_lahir'] ?? '-',
              'Tanggal Lahir' => formatTanggal($siswa['tanggal_lahir'] ?? ''),
              'Anak Ke'       => $siswa['anak_ke'] ?? '-',
              'Tanggal Masuk' => formatTanggal($siswa['tgl_masuk'] ?? ''),
              'Asal Sekolah'  => $siswa['asal_sekolah'] ?? '-',
              'Status'        => $siswa['status'],
            ];
            foreach ($fields as $label => $value):
            ?>
            <div class="col-6 col-md-4 col-lg-3">
              <div class="detail-label"><?= $label ?></div>
              <div class="detail-value">
                <?php if ($label === 'Status' && $value === 'Gaib'): ?>
                  <span class="badge bg-warning text-dark">
                    <i class="bi bi-person-exclamation me-1"></i>Gaib
                  </span>
                <?php else: ?>
                  <?= e($value) ?>
                <?php endif; ?>
              </div>
            </div>
            <?php endforeach; ?>
          </div>
        </div>
      </div>
    </div>

    <!-- Tab Alamat -->
    <div class="tab-pane fade" id="tab-alamat">
      <div class="card">
        <div class="card-body">
          <?php if (empty($alamat)): ?>
            <div class="empty-state"><div class="empty-icon">📍</div><p>Data alamat belum diisi</p></div>
          <?php else: ?>
          <div class="row g-3">
            <?php
            $alamatFields = [
              'Alamat' => $alamat['alamat'] ?? '-',
              'RT' => $alamat['rt'] ?? '-',
              'RW' => $alamat['rw'] ?? '-',
              'Dusun' => $alamat['dusun'] ?? '-',
              'Kelurahan/Desa' => $alamat['kelurahan'] ?? '-',
              'Kecamatan' => $alamat['kecamatan'] ?? '-',
              'Kabupaten/Kota' => $alamat['kabupaten'] ?? '-',
              'Provinsi' => $alamat['provinsi'] ?? '-',
              'Kode Pos' => $alamat['kode_pos'] ?? '-',
            ];
            foreach ($alamatFields as $label => $value):
            ?>
            <div class="col-6 col-md-4">
              <div class="detail-label"><?= $label ?></div>
              <div class="detail-value"><?= e($value) ?></div>
            </div>
            <?php endforeach; ?>
          </div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <!-- Tab Orang Tua -->
    <div class="tab-pane fade" id="tab-ortu">
      <div class="card">
        <div class="card-body">
          <?php if (empty($orangTua)): ?>
            <div class="empty-state"><div class="empty-icon">👨‍👩‍👦</div><p>Data orang tua belum diisi</p></div>
          <?php else: ?>
          <div class="row g-3">
            <?php
            $ortuFields = [
              'Nama Ayah'     => $orangTua['nama_ayah']      ?? '-',
              'Nama Ibu'      => $orangTua['nama_ibu']       ?? '-',
              'Pekerjaan Ayah'=> $orangTua['pekerjaan_ayah'] ?? '-',
              'Pekerjaan Ibu' => $orangTua['pekerjaan_ibu']  ?? '-',
              'No. HP Ayah'   => $orangTua['no_hp_ayah']     ?? '-',
              'No. HP Ibu'    => $orangTua['no_hp_ibu']      ?? '-',
              'Penghasilan'   => $orangTua['penghasilan']    ?? '-',
            ];
            foreach ($ortuFields as $label => $value):
            ?>
            <div class="col-6 col-md-4">
              <div class="detail-label"><?= $label ?></div>
              <div class="detail-value"><?= e($value) ?></div>
            </div>
            <?php endforeach; ?>
          </div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <!-- Tab Wali -->
    <div class="tab-pane fade" id="tab-wali">
      <div class="card">
        <div class="card-body">
          <?php if (empty($wali)): ?>
            <div class="empty-state"><div class="empty-icon">🤝</div><p>Data wali belum diisi</p></div>
          <?php else: ?>
          <div class="row g-3">
            <?php
            $waliFields = [
              'Nama Wali' => $wali['nama_wali'] ?? '-',
              'Hubungan' => $wali['hubungan'] ?? '-',
              'Pekerjaan Wali' => $wali['pekerjaan_wali'] ?? '-',
              'No. HP Wali' => $wali['no_hp_wali'] ?? '-',
              'Alamat Wali' => $wali['alamat_wali'] ?? '-',
            ];
            foreach ($waliFields as $label => $value):
            ?>
            <div class="col-6 col-md-4">
              <div class="detail-label"><?= $label ?></div>
              <div class="detail-value"><?= e($value) ?></div>
            </div>
            <?php endforeach; ?>
          </div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <!-- Tab Nilai -->
    <div class="tab-pane fade" id="tab-nilai">
      <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
          <h6 class="mb-0">
            <i class="bi bi-journal-text me-2"></i>Rekap Nilai Rapor (Semua Semester)
          </h6>
          <a href="<?= BASE_URL ?>/index.php?page=nilai&kelas_id=<?= $siswa['kelas_id'] ?>&siswa_id=<?= $siswa['id'] ?>"
             class="btn btn-sm btn-outline-primary">
            <i class="bi bi-eye me-1"></i> Lihat Detail
          </a>
        </div>
        <div class="card-body p-0">
          <?php
          $nilaiAda = array_filter($nilaiSiswa, function($r) {
              for ($s = 1; $s <= 6; $s++) {
                  if ($r['semester'][$s] !== null) return true;
              }
              return false;
          });
          ?>
          <?php if (empty($nilaiAda)): ?>
            <div class="empty-state py-3">
              <div class="empty-icon">📝</div>
              <p>Belum ada data nilai</p>
            </div>
          <?php else: ?>
          <div class="table-responsive">
            <table class="table mb-0" style="font-size:.83rem;">
              <thead>
                <tr style="background:#1e40af;color:#fff;">
                  <th style="background:#1e40af;color:#fff;border-color:#3b82f6;">Mata Pelajaran</th>
                  <?php for ($s = 1; $s <= 6; $s++): ?>
                  <th class="text-center"
                      style="background:#2563eb;color:#fff;border-color:#3b82f6;min-width:55px;">
                    Smt <?= $s ?>
                  </th>
                  <?php endfor; ?>
                  <th class="text-center"
                      style="background:#1e40af;color:#fff;border-color:#3b82f6;min-width:75px;">
                    Rata-rata
                  </th>
                  <th class="text-center"
                      style="background:#1e40af;color:#fff;border-color:#3b82f6;">
                    Predikat
                  </th>
                </tr>
              </thead>
              <tbody>
                <?php
                $totalRata = 0;
                $countRata = 0;
                foreach ($nilaiAda as $r):
                ?>
                <tr>
                  <td class="fw-600"><?= e($r['nama']) ?></td>
                  <?php for ($s = 1; $s <= 6; $s++):
                    $val   = $r['semester'][$s];
                    $lulus = $val !== null && $val >= (float)$r['kkm'];
                  ?>
                  <td class="text-center <?= $val !== null ? ($lulus ? 'text-success fw-600' : 'text-danger fw-600') : 'text-muted' ?>">
                    <?= $val !== null ? number_format((float)$val, 0) : '-' ?>
                  </td>
                  <?php endfor; ?>
                  <td class="text-center fw-700"
                      style="<?= $r['rata'] !== null ? ($r['rata'] >= $r['kkm'] ? 'color:#16a34a' : 'color:#dc2626') : '' ?>">
                    <?= $r['rata'] !== null ? number_format((float)$r['rata'], 1) : '-' ?>
                  </td>
                  <td class="text-center">
                    <?php if ($r['predikat'] !== '-'): ?>
                    <span class="badge <?= ['A'=>'bg-success','B'=>'bg-primary','C'=>'bg-warning text-dark','D'=>'bg-danger'][$r['predikat']] ?? 'bg-secondary' ?>">
                      <?= $r['predikat'] ?>
                    </span>
                    <?php else: ?>
                    <span class="text-muted">-</span>
                    <?php endif; ?>
                  </td>
                </tr>
                <?php
                  if ($r['rata'] !== null) { $totalRata += $r['rata']; $countRata++; }
                endforeach;
                $rataAkhir = $countRata > 0 ? round($totalRata / $countRata, 1) : null;
                ?>
              </tbody>
              <?php if ($rataAkhir !== null): ?>
              <tfoot>
                <tr style="background:#1e3a5f;color:#fff;font-weight:700;">
                  <td colspan="7" class="text-end pe-3" style="color:#fff;">Rata-rata Keseluruhan</td>
                  <td class="text-center" style="color:#fff;"><?= $rataAkhir ?></td>
                  <td class="text-center" style="color:#fff;">
                    <?php
                    if ($rataAkhir >= 90)     echo 'A';
                    elseif ($rataAkhir >= 80) echo 'B';
                    elseif ($rataAkhir >= 70) echo 'C';
                    else                      echo 'D';
                    ?>
                  </td>
                </tr>
              </tfoot>
              <?php endif; ?>
            </table>
          </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div><!-- /tab-content -->
</div>

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