<?php
// admin_pelajar.php — Admin Dashboard Pelajar (Bootstrap + Dark/Light Theme, Tab T1-T6)
require_once __DIR__.'/config.php';
session_start();
if(!isset($_SESSION['is_admin'])) $_SESSION['is_admin']=true;

function safe($v){return htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');}
$msg='';

/* ===================== PROSES BORANG ===================== */
if($_SERVER['REQUEST_METHOD']==='POST'){
  if(isset($_POST['tambah'])){
    $stmt=$mysqli->prepare("INSERT INTO pelajar (nama,tingkatan,kelas,gpa,markah_koku,jenis_sukan,pencapaian,peringkat) VALUES (?,?,?,?,?,?,?,?)");
    $stmt->bind_param('sssdiiss',$_POST['nama'],$_POST['tingkatan'],$_POST['kelas'],$_POST['gpa'],$_POST['markah_koku'],$_POST['jenis_sukan'],$_POST['pencapaian'],$_POST['peringkat']);
    $msg=$stmt->execute()?"✅ Pelajar ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini'])){
    $stmt=$mysqli->prepare("UPDATE pelajar SET nama=?,tingkatan=?,kelas=?,gpa=?,markah_koku=?,jenis_sukan=?,pencapaian=?,peringkat=? WHERE id=?");
    $stmt->bind_param('sssdiissi',$_POST['nama'],$_POST['tingkatan'],$_POST['kelas'],$_POST['gpa'],$_POST['markah_koku'],$_POST['jenis_sukan'],$_POST['pencapaian'],$_POST['peringkat'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Pelajar dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam'])){
    $stmt=$mysqli->prepare("DELETE FROM pelajar WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Pelajar dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
}

/* ===================== AMBIL DATA ===================== */
$pelajar=$mysqli->query("SELECT * FROM pelajar ORDER BY tingkatan,kelas,nama LIMIT 100");

// Top Akademik & Kokum ikut Tingkatan
$topAkademik=[]; $topKokum=[];
for($i=1;$i<=6;$i++){
  $topAkademik["T$i"]=$mysqli->query("SELECT * FROM pelajar WHERE tingkatan='T$i' ORDER BY gpa DESC LIMIT 3");
  $topKokum["T$i"]=$mysqli->query("SELECT * FROM pelajar WHERE tingkatan='T$i' ORDER BY markah_koku DESC LIMIT 3");
}
?>
<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Kurikulum</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
  body{transition:0.3s;}
  .msg{margin:10px;font-weight:bold;}
  body.dark{background:#121212;color:#eee;}
  body.dark .card{background:#1e1e1e;color:#eee;}
  body.dark .table{color:#eee;}
  body.dark .nav-tabs .nav-link.active{background:#444;color:#fff;}
  th,td{vertical-align:middle;}
  
 
/* Remove default bootstrap tab styling */
.nav-tabs {
    border-bottom: none !important;
}

.nav-tabs .nav-link {
    background: linear-gradient(45deg, #FFE259, #FFA751) !important;
    color: #000 !important;
    border: none !important;
    border-radius: 8px !important;
    margin-right: 6px;
    padding: 10px 18px;
    font-weight: 600;
}

/* Active tab */
.nav-tabs .nav-link.active {
    background: linear-gradient(45deg, #FFD200, #FFB300) !important;
    color: #000 !important;
    box-shadow: 0 4px 10px rgba(255,193,7,0.4);
}

/* Hover */
.nav-tabs .nav-link:hover {
    filter: brightness(1.12);
}
</style>
</head>
<body>
<div class="container-fluid p-3 bg-primary text-white d-flex justify-content-between align-items-center">
  <h2>📚 Admin Pelajar</h2>
  <button onclick="toggleTheme()" class="btn btn-sm btn-outline-secondary">🌙/☀️</button>
</div>
<?php if($msg): ?><div class="alert alert-info"><?=safe($msg)?></div><?php endif; ?>

<!-- Tab Menu -->
<ul class="nav nav-tabs">
  <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#senarai"><i class="bi bi-list-ul"></i> Senarai</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#topakademik"><i class="bi bi-mortarboard"></i> Top Akademik</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#topkokum"><i class="bi bi-trophy"></i> Top Kokum</a></li>
</ul>

<div class="tab-content mt-3">

  <!-- Senarai -->
  <div id="senarai" class="tab-pane fade show active">
    <h4>📋 Senarai Pelajar</h4>
    <form method="post" class="row g-2 mb-3">
      <div class="col-md-2"><input class="form-control" name="nama" placeholder="Nama" required></div>
      <div class="col-md-1">
        <select class="form-select" name="tingkatan">
          <?php for($i=1;$i<=6;$i++): ?>
            <option value="T<?=$i?>">T<?=$i?></option>
          <?php endfor; ?>
        </select>
      </div>
      <div class="col-md-1"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col-md-1"><input class="form-control" type="number" step="0.01" name="gpa" placeholder="GPA"></div>
      <div class="col-md-1"><input class="form-control" type="number" name="markah_koku" placeholder="Koku"></div>
      <div class="col-md-2"><input class="form-control" name="jenis_sukan" placeholder="Sukan"></div>
      <div class="col-md-2"><input class="form-control" name="pencapaian" placeholder="Pencapaian"></div>
      <div class="col-md-2"><input class="form-control" name="peringkat" placeholder="Peringkat"></div>
      <div class="col-md-12"><button class="btn btn-success" name="tambah">Tambah</button></div>
    </form>

    <div class="table-responsive">
    <table class="table table-bordered table-sm align-middle">
      <thead><tr><th>Nama</th><th>Tingkatan</th><th>Kelas</th><th>GPA</th><th>Koku</th><th>Sukan</th><th>Pencapaian</th><th>Peringkat</th><th>Kemaskini</th><th>Padam</th></tr></thead>
      <?php while($p=$pelajar->fetch_assoc()): ?>
      <tr>
        <form method="post" class="d-flex flex-wrap">
          <td><input class="form-control form-control-sm" name="nama" value="<?=safe($p['nama'])?>"></td>
          <td>
            <select class="form-select form-select-sm" name="tingkatan">
              <?php for($i=1;$i<=6;$i++): ?>
                <option value="T<?=$i?>" <?=$p['tingkatan']=="T$i"?'selected':''?>>T<?=$i?></option>
              <?php endfor; ?>
            </select>
          </td>
          <td><input class="form-control form-control-sm" name="kelas" value="<?=safe($p['kelas'])?>"></td>
          <td><input class="form-control form-control-sm" type="number" step="0.01" name="gpa" value="<?=$p['gpa']?>"></td>
          <td><input class="form-control form-control-sm" type="number" name="markah_koku" value="<?=$p['markah_koku']?>"></td>
          <td><input class="form-control form-control-sm" name="jenis_sukan" value="<?=safe($p['jenis_sukan'])?>"></td>
          <td><input class="form-control form-control-sm" name="pencapaian" value="<?=safe($p['pencapaian'])?>"></td>
          <td><input class="form-control form-control-sm" name="peringkat" value="<?=safe($p['peringkat'])?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$p['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini">Simpan</button>
          </td>
        </form>
        <td>
          <form method="post" onsubmit="return confirm('Padam pelajar ini?');">
            <input type="hidden" name="id" value="<?=$p['id']?>">
            <button class="btn btn-danger btn-sm" name="padam">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
    </div>
  </div>

  <!-- Top Akademik -->
  <div id="topakademik" class="tab-pane fade">
    <h4>🎓 3 Pelajar Terbaik Akademik Setiap Tingkatan</h4>
    <?php foreach($topAkademik as $ting=>$res): ?>
      <h5 class="mt-3">Tingkatan <?=$ting?></h5>
      <table class="table table-striped table-sm">
        <thead><tr><th>Rank</th><th>Nama</th><th>Kelas</th><th>GPA</th><th>Kemaskini</th><th>Padam</th></tr></thead>
        <tbody>
        <?php $rank=1; while($a=$res->fetch_assoc()): ?>
          <tr>
            <td><?=$rank++?></td>
            <form method="post" class="d-flex flex-wrap">
              <td><input class="form-control form-control-sm" name="nama" value="<?=safe($a['nama'])?>"></td>
              <td><input class="form-control form-control-sm" name="kelas" value="<?=safe($a['kelas'])?>"></td>
              <td><input class="form-control form-control-sm" type="number" step="0.01" name="gpa" value="<?=$a['gpa']?>"></td>
              <td>
                <input type="hidden" name="id" value="<?=$a['id']?>">
                <input type="hidden" name="tingkatan" value="<?=$a['tingkatan']?>">
                <input type="hidden" name="markah_koku" value="<?=$a['markah_koku']?>">
                <input type="hidden" name="jenis_sukan" value="<?=$a['jenis_sukan']?>">
                <input type="hidden" name="pencapaian" value="<?=$a['pencapaian']?>">
                <input type="hidden" name="peringkat" value="<?=$a['peringkat']?>">
                <button class="btn btn-primary btn-sm" name="kemaskini">Simpan</button>
              </td>
            </form>
            <td>
              <form method="post" onsubmit="return confirm('Padam pelajar ini?');">
                <input type="hidden" name="id" value="<?=$a['id']?>">
                <button class="btn btn-danger btn-sm" name="padam">Padam</button>
              </form>
            </td>
          </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    <?php endforeach; ?>
  </div>

  <!-- Top Kokum -->
  <div id="topkokum" class="tab-pane fade">
    <h4>🏅 3 Pelajar Terbaik Kokurikulum Setiap Tingkatan</h4>
    <?php foreach($topKokum as $ting=>$res): ?>
      <h5 class="mt-3">Tingkatan <?=$ting?></h5>
      <table class="table table-striped table-sm">
        <thead><tr><th>Rank</th><th>Nama</th><th>Kelas</th><th>Koku</th><th>Kemaskini</th><th>Padam</th></tr></thead>
        <tbody>
        <?php $rank=1; while($k=$res->fetch_assoc()): ?>
          <tr>
            <td><?=$rank++?></td>
            <form method="post" class="d-flex flex-wrap">
              <td><input class="form-control form-control-sm" name="nama" value="<?=safe($k['nama'])?>"></td>
              <td><input class="form-control form-control-sm" name="kelas" value="<?=safe($k['kelas'])?>"></td>
              <td><input class="form-control form-control-sm" type="number" name="markah_koku" value="<?=$k['markah_koku']?>"></td>
              <td>
                <input type="hidden" name="id" value="<?=$k['id']?>">
                <input type="hidden" name="tingkatan" value="<?=$k['tingkatan']?>">
                <input type="hidden" name="gpa" value="<?=$k['gpa']?>">
                <input type="hidden" name="jenis_sukan" value="<?=$k['jenis_sukan']?>">
                <input type="hidden" name="pencapaian" value="<?=$k['pencapaian']?>">
                <input type="hidden" name="peringkat" value="<?=$k['peringkat']?>">
                <button class="btn btn-primary btn-sm" name="kemaskini">Simpan</button>
              </td>
            </form>
            <td>
              <form method="post" onsubmit="return confirm('Padam pelajar ini?');">
                <input type="hidden" name="id" value="<?=$k['id']?>">
                <button class="btn btn-danger btn-sm" name="padam">Padam</button>
              </form>
            </td>
          </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    <?php endforeach; ?>
  </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function toggleTheme(){
  document.body.classList.toggle("dark");
  localStorage.setItem("theme",document.body.classList.contains("dark")?"dark":"light");
}
window.onload=function(){
  if(localStorage.getItem("theme")==="dark"){document.body.classList.add("dark");}
};
</script>
</body>
</html>
