<?php
// index_kurikulum.php — Dashboard Kurikulum (MySQLi)
require_once __DIR__.'/config.php';

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

// ================== Query Data ==================
// Jadual waktu
$jadual_rows = [];
if ($res = $mysqli->query("SELECT * FROM jadual_waktu ORDER BY hari, masa ASC LIMIT 20")) {
  while($row = $res->fetch_assoc()) $jadual_rows[] = $row;
  $res->free();
}

// Prestasi peperiksaan
$exam_rows = [];
if ($res = $mysqli->query("SELECT * FROM peperiksaan ORDER BY tahun DESC, penggal DESC LIMIT 6")) {
  while($row = $res->fetch_assoc()) $exam_rows[] = $row;
  $res->free();
}

// Guru opsyen
$guru_rows = [];
if ($res = $mysqli->query("SELECT * FROM guru_opsyen ORDER BY opsyen ASC")) {
  while($row = $res->fetch_assoc()) $guru_rows[] = $row;
  $res->free();
}

// Pengumuman kurikulum
$ann = [];
if ($res = $mysqli->query("SELECT * FROM kurikulum_announcements ORDER BY tarikh DESC, id DESC LIMIT 5")) {
  while($row = $res->fetch_assoc()) $ann[] = $row;
  $res->free();
}

// Pencapaian akademik
$pencapaian_rows = [];
if ($res = $mysqli->query("SELECT * FROM pencapaian ORDER BY tahun DESC LIMIT 5")) {
  while($row = $res->fetch_assoc()) $pencapaian_rows[] = $row;
  $res->free();
}

// Program khas
$program_rows = [];
if ($res = $mysqli->query("SELECT * FROM program_khas ORDER BY tarikh DESC LIMIT 5")) {
  while($row = $res->fetch_assoc()) $program_rows[] = $row;
  $res->free();
}

// KPI Kurikulum
$kpi_rows = [];
if ($res = $mysqli->query("SELECT * FROM kpi_kurikulum ORDER BY tahun DESC LIMIT 5")) {
  while($row = $res->fetch_assoc()) $kpi_rows[] = $row;
  $res->free();
}

// Sekolah
$NAMA_SEKOLAH = 'SMK MUHIBBAH SANDAKAN';
$LOGO = 'logo.png';
?>
<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title>Dashboard Kurikulum</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
  :root{ --pad:clamp(10px,1.6vw,28px); --radius:18px; --card-bg:rgba(0,0,0,0.35); --accent:#1976d2; --text-shadow:1px 1px 4px rgba(0,0,0,0.5); --footer-h:44px;}
  html,body{height:100%;background:#0b1d33;color:#fff;}
  body{margin:0;font-family:Segoe UI,system-ui,Roboto,Arial,sans-serif;}
  .header{display:flex;align-items:center;justify-content:space-between;padding:var(--pad);gap:var(--pad);background:linear-gradient(90deg,rgba(25,118,210,0.25),rgba(0,0,0,0));border-bottom:1px solid rgba(255,255,255,0.08);}
  .brand{display:flex;align-items:center;gap:12px;}
  .brand img{width:64px;height:64px;object-fit:contain;}
  .brand .name{font-size:clamp(18px,2.2vw,32px);font-weight:700;text-shadow:var(--text-shadow);}
  .brand .subtitle{font-size:clamp(12px,1.2vw,16px);opacity:.9;}
  .clock{text-align:right;min-width:240px;}
  .clock .date{font-size:clamp(12px,1.2vw,16px);opacity:.9;}
  .clock .time{font-size:clamp(20px,3vw,40px);font-weight:700;letter-spacing:1px;}
  .container{padding:var(--pad);padding-bottom:calc(var(--footer-h) + var(--pad));
    display:grid;grid-template-columns:repeat(12,1fr);grid-auto-rows:minmax(120px,auto);gap:var(--pad);}
  .card{background:var(--card-bg);border-radius:var(--radius);padding:var(--pad);backdrop-filter:blur(4px);box-shadow:0 8px 22px rgba(0,0,0,.25);}
  .card h3{margin:0 0 10px;font-size:clamp(14px,1.4vw,20px);font-weight:700;display:flex;align-items:center;gap:10px;}
  table.slim{width:100%;border-collapse:collapse;font-size:clamp(12px,1.1vw,15px);}
  table.slim th,table.slim td{border-bottom:1px dashed rgba(255,255,255,0.12);padding:6px 8px;text-align:left;}
  table.slim th{opacity:.8;font-weight:600;}
  .muted{opacity:.8;}
  .grid-6{grid-column:span 6;} .grid-12{grid-column:1 / -1;}
  @media(max-width:1024px){.grid-6{grid-column:1 / -1;}}
  .ticker-wrap{position:fixed;left:0;right:0;bottom:0;height:var(--footer-h);background:linear-gradient(90deg,#0e2647,#0b1d33);
    display:flex;align-items:center;overflow:hidden;border-top:1px solid rgba(255,255,255,.1);}
  .ticker-label{flex:0 0 auto;padding:0 14px;font-weight:700;background:rgba(255,255,255,.06);height:100%;display:flex;align-items:center;border-right:1px solid rgba(255,255,255,.08);}
  .ticker{white-space:nowrap;will-change:transform;padding-left:100%;animation:marquee 28s linear infinite;font-size:14px;}
  @keyframes marquee{0%{transform:translateX(0);}100%{transform:translateX(-100%);}}
</style>
</head>
<body>

<main class="container">
    
      <!-- Pengumuman -->
  <section class="card grid-6">
    <h3>📢 Pengumuman Kurikulum</h3>
    <?php if($ann): ?>
    <table class="slim">
      <thead><tr><th>Tajuk</th><th class="muted">Tarikh</th></tr></thead>
      <tbody>
        <?php foreach($ann as $a): ?>
        <tr>
          <td><?= safe($a['tajuk']) ?></td>
          <td class="muted"><?= safe($a['tarikh']) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada pengumuman.</div><?php endif; ?>
  </section>
  
    <!-- Program Khas -->
  <section class="card grid-6">
    <h3>🌟 Program Khas</h3>
    <?php if($program_rows): ?>
    <table class="slim">
      <thead><tr><th>Nama Program</th><th>Tarikh</th><th>Keterangan</th></tr></thead>
      <tbody>
        <?php foreach($program_rows as $r): ?>
        <tr>
          <td><?= safe($r['nama_program']) ?></td>
          <td><?= safe($r['tarikh']) ?></td>
          <td><?= safe($r['keterangan']) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada program khas.</div><?php endif; ?>
  </section>
  
  <!-- Jadual Waktu -->
  <section class="card grid-6">
    <h3>📚 Jadual Waktu</h3>
    <?php if($jadual_rows): ?>
    <table class="slim">
      <thead><tr><th>Hari</th><th>Masa</th><th>Kelas</th><th>Mata Pelajaran</th><th>Guru</th></tr></thead>
      <tbody>
        <?php foreach($jadual_rows as $r): ?>
        <tr>
          <td><?= safe($r['hari']) ?></td>
          <td><?= safe($r['masa']) ?></td>
          <td><?= safe($r['kelas']) ?></td>
          <td><?= safe($r['subjek']) ?></td>
          <td><?= safe($r['guru']) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada data jadual.</div><?php endif; ?>
  </section>

  <!-- Prestasi Peperiksaan -->
  <section class="card grid-6">
    <h3>📝 Prestasi Peperiksaan</h3>
    <?php if($exam_rows): ?>
    <table class="slim">
      <thead><tr><th>Tahun</th><th>Penggal</th><th>GPMP</th><th>Peratus Lulus</th></tr></thead>
      <tbody>
        <?php foreach($exam_rows as $r): ?>
        <tr>
          <td><?= safe($r['tahun']) ?></td>
          <td><?= safe($r['penggal']) ?></td>
          <td><?= safe($r['gpmp']) ?></td>
          <td><?= safe($r['peratus_lulus']) ?>%</td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada data peperiksaan.</div><?php endif; ?>
  </section>

  <!-- Bilangan Guru Mengikut Opsyen -->
  <section class="card grid-6">
    <h3>👩‍🏫 Bilangan Guru Mengikut Opsyen</h3>
    <?php if($guru_rows): ?>
    <table class="slim">
      <thead><tr><th>Opsyen</th><th>Bilangan Guru</th></tr></thead>
      <tbody>
        <?php foreach($guru_rows as $r): ?>
        <tr><td><?= safe($r['opsyen']) ?></td><td><?= safe($r['bilangan']) ?></td></tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada data guru opsyen.</div><?php endif; ?>
  </section>

  <!-- Pencapaian Akademik -->
  <section class="card grid-6">
    <h3>📈 Pencapaian Akademik</h3>
    <?php if($pencapaian_rows): ?>
    <table class="slim">
      <thead><tr><th>Tahun</th><th>Jenis Peperiksaan</th><th>Pencapaian</th><th>Kedudukan</th></tr></thead>
      <tbody>
        <?php foreach($pencapaian_rows as $r): ?>
        <tr>
          <td><?= safe($r['tahun']) ?></td>
          <td><?= safe($r['peperiksaan']) ?></td>
          <td><?= safe($r['pencapaian']) ?></td>
          <td><?= safe($r['kedudukan']) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada data pencapaian.</div><?php endif; ?>
  </section>

  <!-- KPI Kurikulum -->
  <section class="card grid-6">
    <h3>✅ Kelulusan KPI Kurikulum</h3>
    <?php if($kpi_rows): ?>
    <table class="slim">
      <thead><tr><th>Tahun</th><th>Sasaran</th><th>Pencapaian</th><th>Status</th></tr></thead>
      <tbody>
        <?php foreach($kpi_rows as $r): ?>
        <tr>
          <td><?= safe($r['tahun']) ?></td>
          <td><?= safe($r['sasaran']) ?>%</td>
          <td><?= safe($r['pencapaian']) ?>%</td>
          <td><?= ($r['pencapaian']>=$r['sasaran']? '✅ Tercapai':'❌ Belum Capai') ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php else: ?><div class="muted">Tiada data KPI.</div><?php endif; ?>
  </section>

  <!-- Info -->
  <section class="card grid-12">
    <h3>ℹ️ Nota / Info Kurikulum</h3>
    <div class="muted">Letakkan dasar kurikulum, KPI, atau info berkaitan di sini.</div>
  </section>
</main>

<div class="ticker-wrap">
  <div class="ticker-label">KURIKULUM</div>
  <div class="ticker">Selamat datang ke Dashboard Kurikulum.</div>
</div>

<script>
  const days=['Ahad','Isnin','Selasa','Rabu','Khamis','Jumaat','Sabtu'];
  const months=['Januari','Februari','Mac','April','Mei','Jun','Julai','Ogos','September','Oktober','November','Disember'];
  const pad=n=>n.toString().padStart(2,'0');
  function tick(){
    const d=new Date();
    const dateStr=`${days[d.getDay()]}, ${pad(d.getDate())} ${months[d.getMonth()]} ${d.getFullYear()}`;
    const timeStr=`${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
    document.getElementById('dateStr').textContent=dateStr;
    document.getElementById('timeStr').textContent=timeStr;
  }
  setInterval(tick,1000); tick();
</script>
</body>
</html>
