<?php
// admin_kokurikulum.php — Admin Dashboard Kokurikulum (Bootstrap + Dark/Light Theme, Fullscreen + CRUD)
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'){
  // Penyertaan
  if(isset($_POST['tambah_penyertaan'])){
    $stmt=$mysqli->prepare("INSERT INTO penyertaan_kokum (kategori,nama,bilangan) VALUES (?,?,?)");
    $stmt->bind_param('ssi',$_POST['kategori'],$_POST['nama'],$_POST['bilangan']);
    $msg=$stmt->execute()?"✅ Penyertaan ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini_penyertaan'])){
    $stmt=$mysqli->prepare("UPDATE penyertaan_kokum SET kategori=?,nama=?,bilangan=? WHERE id=?");
    $stmt->bind_param('ssii',$_POST['kategori'],$_POST['nama'],$_POST['bilangan'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Penyertaan dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_penyertaan'])){
    $stmt=$mysqli->prepare("DELETE FROM penyertaan_kokum WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Penyertaan dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }

  // Pencapaian
  if(isset($_POST['tambah_pencapaian'])){
    $stmt=$mysqli->prepare("INSERT INTO pencapaian_kokum (tahun,acara,pencapaian,peringkat) VALUES (?,?,?,?)");
    $stmt->bind_param('isss',$_POST['tahun'],$_POST['acara'],$_POST['pencapaian'],$_POST['peringkat']);
    $msg=$stmt->execute()?"✅ Pencapaian ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini_pencapaian'])){
    $stmt=$mysqli->prepare("UPDATE pencapaian_kokum SET tahun=?,acara=?,pencapaian=?,peringkat=? WHERE id=?");
    $stmt->bind_param('isssi',$_POST['tahun'],$_POST['acara'],$_POST['pencapaian'],$_POST['peringkat'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Pencapaian dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_pencapaian'])){
    $stmt=$mysqli->prepare("DELETE FROM pencapaian_kokum WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Pencapaian dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }

  // Jadual
  if(isset($_POST['tambah_jadual'])){
    $stmt=$mysqli->prepare("INSERT INTO jadual_kokum (tarikh,aktiviti,tempat) VALUES (?,?,?)");
    $stmt->bind_param('sss',$_POST['tarikh'],$_POST['aktiviti'],$_POST['tempat']);
    $msg=$stmt->execute()?"✅ Jadual ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini_jadual'])){
    $stmt=$mysqli->prepare("UPDATE jadual_kokum SET tarikh=?,aktiviti=?,tempat=? WHERE id=?");
    $stmt->bind_param('sssi',$_POST['tarikh'],$_POST['aktiviti'],$_POST['tempat'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Jadual dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_jadual'])){
    $stmt=$mysqli->prepare("DELETE FROM jadual_kokum WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Jadual dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }

  // KPI
  if(isset($_POST['tambah_kpi'])){
    $stmt=$mysqli->prepare("INSERT INTO kpi_kokum (tahun,sasaran,pencapaian) VALUES (?,?,?)");
    $stmt->bind_param('idd',$_POST['tahun'],$_POST['sasaran'],$_POST['pencapaian']);
    $msg=$stmt->execute()?"✅ KPI ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini_kpi'])){
    $stmt=$mysqli->prepare("UPDATE kpi_kokum SET tahun=?,sasaran=?,pencapaian=? WHERE id=?");
    $stmt->bind_param('iddi',$_POST['tahun'],$_POST['sasaran'],$_POST['pencapaian'],$_POST['id']);
    $msg=$stmt->execute()?"✅ KPI dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_kpi'])){
    $stmt=$mysqli->prepare("DELETE FROM kpi_kokum WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ KPI dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }

  // Pengumuman
  if(isset($_POST['tambah_ann'])){
    $stmt=$mysqli->prepare("INSERT INTO kokum_announcements (tajuk,tarikh) VALUES (?,?)");
    $stmt->bind_param('ss',$_POST['tajuk'],$_POST['tarikh']);
    $msg=$stmt->execute()?"✅ Pengumuman ditambah":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['kemaskini_ann'])){
    $stmt=$mysqli->prepare("UPDATE kokum_announcements SET tajuk=?,tarikh=? WHERE id=?");
    $stmt->bind_param('ssi',$_POST['tajuk'],$_POST['tarikh'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Pengumuman dikemaskini":"❌ ".$stmt->error;
    $stmt->close();
  }
  if(isset($_POST['padam_ann'])){
    $stmt=$mysqli->prepare("DELETE FROM kokum_announcements WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Pengumuman dipadam":"❌ ".$stmt->error;
    $stmt->close();
  }
}

/* ===================== AMBIL DATA ===================== */
$penyertaan=$mysqli->query("SELECT * FROM penyertaan_kokum ORDER BY kategori ASC");
$pencapaian=$mysqli->query("SELECT * FROM pencapaian_kokum ORDER BY tahun DESC");
$jadual=$mysqli->query("SELECT * FROM jadual_kokum ORDER BY tarikh DESC");
$kpi=$mysqli->query("SELECT * FROM kpi_kokum ORDER BY tahun DESC");
$ann=$mysqli->query("SELECT * FROM kokum_announcements ORDER BY tarikh DESC");
?>
<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Kokurikulum</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
  body{transition:0.3s;}
  body.dark{background:#121212;color:#eee;}
  body.dark .table{color:#eee;}
  body.dark .table thead{background:#333;color:#fff;}
  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 class="bg-light">

<div class="container-fluid p-3 bg-primary text-white d-flex justify-content-between align-items-center">
  <h3 class="m-0">📊 Admin Kokurikulum</h3>
  <button onclick="toggleTheme()" class="btn btn-light btn-sm">🌙/☀️</button>
</div>

<div class="container-fluid my-4">
<?php if($msg): ?><div class="alert alert-info"><?=safe($msg)?></div><?php endif; ?>

<!-- Tab Navigation -->
<ul class="nav nav-tabs">
  <li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#penyertaan">Penyertaan</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#pencapaian">Pencapaian</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#jadual">Jadual</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#kpi">KPI</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#ann">Pengumuman</button></li>
</ul>

<div class="tab-content p-3 border border-top-0">

  <!-- Penyertaan -->
  <div class="tab-pane fade show active" id="penyertaan">
    <h5>📊 Penyertaan Kokum</h5>
    <form method="post" class="row g-2 mb-3">
      <div class="col"><input name="kategori" class="form-control" placeholder="Kategori" required></div>
      <div class="col"><input name="nama" class="form-control" placeholder="Nama" required></div>
      <div class="col"><input type="number" name="bilangan" class="form-control" placeholder="Bilangan"></div>
      <div class="col"><button class="btn btn-success w-100" name="tambah_penyertaan">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped table-sm">
      <thead class="table-light"><tr><th>Kategori</th><th>Nama</th><th>Bilangan</th><th>Tindakan</th></tr></thead>
      <tbody>
      <?php while($r=$penyertaan->fetch_assoc()): ?>
      <tr>
        <form method="post">
          <td><input class="form-control form-control-sm" name="kategori" value="<?=safe($r['kategori'])?>"></td>
          <td><input class="form-control form-control-sm" name="nama" value="<?=safe($r['nama'])?>"></td>
          <td><input class="form-control form-control-sm" type="number" name="bilangan" value="<?=$r['bilangan']?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini_penyertaan">Simpan</button>
            <button class="btn btn-danger btn-sm" name="padam_penyertaan" onclick="return confirm('Padam rekod ini?')">Padam</button>
          </td>
        </form>
      </tr>
      <?php endwhile; ?>
      </tbody>
    </table>
  </div>

  <!-- Pencapaian -->
  <div class="tab-pane fade" id="pencapaian">
    <h5>🏆 Pencapaian Kokum</h5>
    <form method="post" class="row g-2 mb-3">
      <div class="col"><input type="number" name="tahun" class="form-control" placeholder="Tahun"></div>
      <div class="col"><input name="acara" class="form-control" placeholder="Acara"></div>
      <div class="col"><input name="pencapaian" class="form-control" placeholder="Pencapaian"></div>
      <div class="col"><input name="peringkat" class="form-control" placeholder="Peringkat"></div>
      <div class="col"><button class="btn btn-success w-100" name="tambah_pencapaian">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped table-sm">
      <thead class="table-light"><tr><th>Tahun</th><th>Acara</th><th>Pencapaian</th><th>Peringkat</th><th>Tindakan</th></tr></thead>
      <tbody>
      <?php while($r=$pencapaian->fetch_assoc()): ?>
      <tr>
        <form method="post">
          <td><input type="number" class="form-control form-control-sm" name="tahun" value="<?=$r['tahun']?>"></td>
          <td><input class="form-control form-control-sm" name="acara" value="<?=safe($r['acara'])?>"></td>
          <td><input class="form-control form-control-sm" name="pencapaian" value="<?=safe($r['pencapaian'])?>"></td>
          <td><input class="form-control form-control-sm" name="peringkat" value="<?=safe($r['peringkat'])?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini_pencapaian">Simpan</button>
            <button class="btn btn-danger btn-sm" name="padam_pencapaian" onclick="return confirm('Padam rekod ini?')">Padam</button>
          </td>
        </form>
      </tr>
      <?php endwhile; ?>
      </tbody>
    </table>
  </div>

  <!-- Jadual -->
  <div class="tab-pane fade" id="jadual">
    <h5>📅 Jadual Aktiviti Kokum</h5>
    <form method="post" class="row g-2 mb-3">
      <div class="col"><input type="date" name="tarikh" class="form-control"></div>
      <div class="col"><input name="aktiviti" class="form-control" placeholder="Aktiviti"></div>
      <div class="col"><input name="tempat" class="form-control" placeholder="Tempat"></div>
      <div class="col"><button class="btn btn-success w-100" name="tambah_jadual">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped table-sm">
      <thead class="table-light"><tr><th>Tarikh</th><th>Aktiviti</th><th>Tempat</th><th>Tindakan</th></tr></thead>
      <tbody>
      <?php while($r=$jadual->fetch_assoc()): ?>
      <tr>
        <form method="post">
          <td><input type="date" class="form-control form-control-sm" name="tarikh" value="<?=$r['tarikh']?>"></td>
          <td><input class="form-control form-control-sm" name="aktiviti" value="<?=safe($r['aktiviti'])?>"></td>
          <td><input class="form-control form-control-sm" name="tempat" value="<?=safe($r['tempat'])?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini_jadual">Simpan</button>
            <button class="btn btn-danger btn-sm" name="padam_jadual" onclick="return confirm('Padam rekod ini?')">Padam</button>
          </td>
        </form>
      </tr>
      <?php endwhile; ?>
      </tbody>
    </table>
  </div>

  <!-- KPI -->
  <div class="tab-pane fade" id="kpi">
    <h5>✅ KPI Kokum</h5>
    <form method="post" class="row g-2 mb-3">
      <div class="col"><input type="number" name="tahun" class="form-control" placeholder="Tahun"></div>
      <div class="col"><input type="number" step="0.01" name="sasaran" class="form-control" placeholder="Sasaran %"></div>
      <div class="col"><input type="number" step="0.01" name="pencapaian" class="form-control" placeholder="Pencapaian %"></div>
      <div class="col"><button class="btn btn-success w-100" name="tambah_kpi">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped table-sm">
      <thead class="table-light"><tr><th>Tahun</th><th>Sasaran</th><th>Pencapaian</th><th>Tindakan</th></tr></thead>
      <tbody>
      <?php while($r=$kpi->fetch_assoc()): ?>
      <tr>
        <form method="post">
          <td><input type="number" class="form-control form-control-sm" name="tahun" value="<?=$r['tahun']?>"></td>
          <td><input type="number" step="0.01" class="form-control form-control-sm" name="sasaran" value="<?=$r['sasaran']?>"></td>
          <td><input type="number" step="0.01" class="form-control form-control-sm" name="pencapaian" value="<?=$r['pencapaian']?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini_kpi">Simpan</button>
            <button class="btn btn-danger btn-sm" name="padam_kpi" onclick="return confirm('Padam rekod ini?')">Padam</button>
          </td>
        </form>
      </tr>
      <?php endwhile; ?>
      </tbody>
    </table>
  </div>

  <!-- Pengumuman -->
  <div class="tab-pane fade" id="ann">
    <h5>📢 Pengumuman Kokum</h5>
    <form method="post" class="row g-2 mb-3">
      <div class="col"><input name="tajuk" class="form-control" placeholder="Tajuk"></div>
      <div class="col"><input type="date" name="tarikh" class="form-control"></div>
      <div class="col"><button class="btn btn-success w-100" name="tambah_ann">Tambah</button></div>
    </form>
    <table class="table table-bordered table-striped table-sm">
      <thead class="table-light"><tr><th>Tajuk</th><th>Tarikh</th><th>Tindakan</th></tr></thead>
      <tbody>
      <?php while($r=$ann->fetch_assoc()): ?>
      <tr>
        <form method="post">
          <td><input class="form-control form-control-sm" name="tajuk" value="<?=safe($r['tajuk'])?>"></td>
          <td><input type="date" class="form-control form-control-sm" name="tarikh" value="<?=$r['tarikh']?>"></td>
          <td>
            <input type="hidden" name="id" value="<?=$r['id']?>">
            <button class="btn btn-primary btn-sm" name="kemaskini_ann">Simpan</button>
            <button class="btn btn-danger btn-sm" name="padam_ann" onclick="return confirm('Padam rekod ini?')">Padam</button>
          </td>
        </form>
      </tr>
      <?php endwhile; ?>
      </tbody>
    </table>
  </div>

</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>
