<?php
// admin_kelas.php — Admin Dashboard Kelas
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'){
  // Mingguan
  if(isset($_POST['tambah_mingguan'])){
    $stmt=$mysqli->prepare("INSERT INTO kelas_terbersih_mingguan (minggu,kelas,catatan) VALUES (?,?,?)");
    $stmt->bind_param('sss',$_POST['minggu'],$_POST['kelas'],$_POST['catatan']);
    $msg=$stmt->execute()?"✅ Mingguan ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_mingguan'])){
    $stmt=$mysqli->prepare("DELETE FROM kelas_terbersih_mingguan WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Mingguan dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
  // Bulanan
  if(isset($_POST['tambah_bulanan'])){
    $stmt=$mysqli->prepare("INSERT INTO kelas_terbersih_bulanan (bulan,kelas,catatan) VALUES (?,?,?)");
    $stmt->bind_param('sss',$_POST['bulan'],$_POST['kelas'],$_POST['catatan']);
    $msg=$stmt->execute()?"✅ Bulanan ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_bulanan'])){
    $stmt=$mysqli->prepare("DELETE FROM kelas_terbersih_bulanan WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Bulanan dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
  // Kurang ceria
  if(isset($_POST['tambah_kurang'])){
    $stmt=$mysqli->prepare("INSERT INTO kelas_kurang_ceria_mingguan (minggu,kelas,catatan) VALUES (?,?,?)");
    $stmt->bind_param('sss',$_POST['minggu'],$_POST['kelas'],$_POST['catatan']);
    $msg=$stmt->execute()?"✅ Kurang ceria ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_kurang'])){
    $stmt=$mysqli->prepare("DELETE FROM kelas_kurang_ceria_mingguan WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Kurang ceria dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
  // Guru kelas
  if(isset($_POST['tambah_guru'])){
    $stmt=$mysqli->prepare("INSERT INTO guru_kelas (kelas,guru) VALUES (?,?)");
    $stmt->bind_param('ss',$_POST['kelas'],$_POST['guru']);
    $msg=$stmt->execute()?"✅ Guru kelas ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_guru'])){
    $stmt=$mysqli->prepare("DELETE FROM guru_kelas WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Guru kelas dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
  // Ketua kelas
  if(isset($_POST['tambah_ketua'])){
    $stmt=$mysqli->prepare("INSERT INTO ketua_kelas (kelas,ketua) VALUES (?,?)");
    $stmt->bind_param('ss',$_POST['kelas'],$_POST['ketua']);
    $msg=$stmt->execute()?"✅ Ketua kelas ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_ketua'])){
    $stmt=$mysqli->prepare("DELETE FROM ketua_kelas WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Ketua kelas dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
}

/* ===================== AMBIL DATA ===================== */
$mingguan=$mysqli->query("SELECT * FROM kelas_terbersih_mingguan ORDER BY minggu DESC LIMIT 20");
$bulanan=$mysqli->query("SELECT * FROM kelas_terbersih_bulanan ORDER BY bulan DESC LIMIT 20");
$kurang=$mysqli->query("SELECT * FROM kelas_kurang_ceria_mingguan ORDER BY minggu DESC LIMIT 20");
$guruKelas=$mysqli->query("SELECT * FROM guru_kelas ORDER BY kelas ASC");
$ketuaKelas=$mysqli->query("SELECT * FROM ketua_kelas ORDER BY kelas ASC");
?>
<!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;}
  
  <style>
/* 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>

</style>
</head>
<body>
<div class="container-fluid p-3 bg-primary text-white d-flex justify-content-between align-items-center">
  <h2>⚙️ Admin Kelas</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; ?>

<ul class="nav nav-tabs">
  <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#mingguan">Terbersih Mingguan</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#bulanan">Terbersih Bulanan</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#kurang">Kurang Ceria</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#guru">Guru Kelas</a></li>
  <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#ketua">Ketua Kelas</a></li>
</ul>

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

  <!-- Mingguan -->
  <div id="mingguan" class="tab-pane fade show active">
    <h4>🧹 Kelas Terbersih Mingguan</h4>
    <form method="post" class="row g-2 mb-2">
      <div class="col"><input class="form-control" name="minggu" placeholder="Minggu"></div>
      <div class="col"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col"><input class="form-control" name="catatan" placeholder="Catatan"></div>
      <div class="col-auto"><button class="btn btn-success" name="tambah_mingguan">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped">
      <tr><th>Minggu</th><th>Kelas</th><th>Catatan</th><th>Padam</th></tr>
      <?php while($r=$mingguan->fetch_assoc()): ?>
      <tr>
        <td><?=safe($r['minggu'])?></td><td><?=safe($r['kelas'])?></td><td><?=safe($r['catatan'])?></td>
        <td>
          <form method="post" onsubmit="return confirm('Padam data ini?')">
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-sm btn-danger" name="padam_mingguan">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
  </div>

  <!-- Bulanan -->
  <div id="bulanan" class="tab-pane fade">
    <h4>🧹 Kelas Terbersih Bulanan</h4>
    <form method="post" class="row g-2 mb-2">
      <div class="col"><input class="form-control" name="bulan" placeholder="Bulan"></div>
      <div class="col"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col"><input class="form-control" name="catatan" placeholder="Catatan"></div>
      <div class="col-auto"><button class="btn btn-success" name="tambah_bulanan">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped">
      <tr><th>Bulan</th><th>Kelas</th><th>Catatan</th><th>Padam</th></tr>
      <?php while($r=$bulanan->fetch_assoc()): ?>
      <tr>
        <td><?=safe($r['bulan'])?></td><td><?=safe($r['kelas'])?></td><td><?=safe($r['catatan'])?></td>
        <td>
          <form method="post" onsubmit="return confirm('Padam data ini?')">
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-sm btn-danger" name="padam_bulanan">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
  </div>

  <!-- Kurang Ceria -->
  <div id="kurang" class="tab-pane fade">
    <h4>😕 Kelas Kurang Ceria Mingguan</h4>
    <form method="post" class="row g-2 mb-2">
      <div class="col"><input class="form-control" name="minggu" placeholder="Minggu"></div>
      <div class="col"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col"><input class="form-control" name="catatan" placeholder="Catatan"></div>
      <div class="col-auto"><button class="btn btn-success" name="tambah_kurang">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped">
      <tr><th>Minggu</th><th>Kelas</th><th>Catatan</th><th>Padam</th></tr>
      <?php while($r=$kurang->fetch_assoc()): ?>
      <tr>
        <td><?=safe($r['minggu'])?></td><td><?=safe($r['kelas'])?></td><td><?=safe($r['catatan'])?></td>
        <td>
          <form method="post" onsubmit="return confirm('Padam data ini?')">
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-sm btn-danger" name="padam_kurang">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
  </div>

  <!-- Guru Kelas -->
  <div id="guru" class="tab-pane fade">
    <h4>👩‍🏫 Guru Kelas</h4>
    <form method="post" class="row g-2 mb-2">
      <div class="col"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col"><input class="form-control" name="guru" placeholder="Nama Guru"></div>
      <div class="col-auto"><button class="btn btn-success" name="tambah_guru">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped">
      <tr><th>Kelas</th><th>Guru</th><th>Padam</th></tr>
      <?php while($r=$guruKelas->fetch_assoc()): ?>
      <tr>
        <td><?=safe($r['kelas'])?></td><td><?=safe($r['guru'])?></td>
        <td>
          <form method="post" onsubmit="return confirm('Padam guru kelas ini?')">
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-sm btn-danger" name="padam_guru">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
  </div>

  <!-- Ketua Kelas -->
  <div id="ketua" class="tab-pane fade">
    <h4>👨‍🎓 Ketua Kelas</h4>
    <form method="post" class="row g-2 mb-2">
      <div class="col"><input class="form-control" name="kelas" placeholder="Kelas"></div>
      <div class="col"><input class="form-control" name="ketua" placeholder="Nama Ketua"></div>
      <div class="col-auto"><button class="btn btn-success" name="tambah_ketua">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped">
      <tr><th>Kelas</th><th>Ketua</th><th>Padam</th></tr>
      <?php while($r=$ketuaKelas->fetch_assoc()): ?>
      <tr>
        <td><?=safe($r['kelas'])?></td><td><?=safe($r['ketua'])?></td>
        <td>
          <form method="post" onsubmit="return confirm('Padam ketua kelas ini?')">
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-sm btn-danger" name="padam_ketua">Padam</button>
          </form>
        </td>
      </tr>
      <?php endwhile; ?>
    </table>
  </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>
