<?php
// admin_hem.php — Admin Dashboard Hal Ehwal Murid (Bootstrap + Dark/Light Theme + Full 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'){
  /* Kehadiran */
  if(isset($_POST['tambah_kehadiran'])){
    $stmt=$mysqli->prepare("INSERT INTO attendance (tarikh,hadir,tidak_hadir,lewat,peratus) VALUES (?,?,?,?,?)");
    $stmt->bind_param('siiid',$_POST['tarikh'],$_POST['hadir'],$_POST['tidak_hadir'],$_POST['lewat'],$_POST['peratus']);
    $msg=$stmt->execute()?"✅ Kehadiran ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_kehadiran'])){
    $stmt=$mysqli->prepare("UPDATE attendance SET tarikh=?,hadir=?,tidak_hadir=?,lewat=?,peratus=? WHERE id=?");
    $stmt->bind_param('siiidi',$_POST['tarikh'],$_POST['hadir'],$_POST['tidak_hadir'],$_POST['lewat'],$_POST['peratus'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Kehadiran dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_kehadiran'])){
    $stmt=$mysqli->prepare("DELETE FROM attendance WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Kehadiran dipadam":"❌ ".$stmt->error; $stmt->close();
  }

  /* Disiplin */
  if(isset($_POST['tambah_disiplin'])){
    $stmt=$mysqli->prepare("INSERT INTO discipline (bulan,kes,perubahan,top1,top2,top3) VALUES (?,?,?,?,?,?)");
    $stmt->bind_param('siisss',$_POST['bulan'],$_POST['kes'],$_POST['perubahan'],$_POST['top1'],$_POST['top2'],$_POST['top3']);
    $msg=$stmt->execute()?"✅ Disiplin ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_disiplin'])){
    $stmt=$mysqli->prepare("UPDATE discipline SET bulan=?,kes=?,perubahan=?,top1=?,top2=?,top3=? WHERE id=?");
    $stmt->bind_param('siisssi',$_POST['bulan'],$_POST['kes'],$_POST['perubahan'],$_POST['top1'],$_POST['top2'],$_POST['top3'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Disiplin dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_disiplin'])){
    $stmt=$mysqli->prepare("DELETE FROM discipline WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Disiplin dipadam":"❌ ".$stmt->error; $stmt->close();
  }

  /* Kebajikan */
  if(isset($_POST['tambah_welfare'])){
    $stmt=$mysqli->prepare("INSERT INTO welfare (rmt,kwapm,zakat,bsh,b40_peratus,updated_at) VALUES (?,?,?,?,?,NOW())");
    $stmt->bind_param('iiiid',$_POST['rmt'],$_POST['kwapm'],$_POST['zakat'],$_POST['bsh'],$_POST['b40_peratus']);
    $msg=$stmt->execute()?"✅ Kebajikan ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_welfare'])){
    $stmt=$mysqli->prepare("UPDATE welfare SET rmt=?,kwapm=?,zakat=?,bsh=?,b40_peratus=?,updated_at=NOW() WHERE id=?");
    $stmt->bind_param('iiiidi',$_POST['rmt'],$_POST['kwapm'],$_POST['zakat'],$_POST['bsh'],$_POST['b40_peratus'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Kebajikan dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_welfare'])){
    $stmt=$mysqli->prepare("DELETE FROM welfare WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Kebajikan dipadam":"❌ ".$stmt->error; $stmt->close();
  }

  /* Kesihatan */
  if(isset($_POST['tambah_health'])){
    $stmt=$mysqli->prepare("INSERT INTO health (hari,tarikh,sakit,kemalangan,ponteng,keluar_tanpa_izin) VALUES (?,?,?,?,?,?)");
    $stmt->bind_param('ssiiii',$_POST['hari'],$_POST['tarikh'],$_POST['sakit'],$_POST['kemalangan'],$_POST['ponteng'],$_POST['keluar_tanpa_izin']);
    $msg=$stmt->execute()?"✅ Kesihatan ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_health'])){
    $stmt=$mysqli->prepare("UPDATE health SET hari=?,tarikh=?,sakit=?,kemalangan=?,ponteng=?,keluar_tanpa_izin=? WHERE id=?");
    $stmt->bind_param('ssiiiii',$_POST['hari'],$_POST['tarikh'],$_POST['sakit'],$_POST['kemalangan'],$_POST['ponteng'],$_POST['keluar_tanpa_izin'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Kesihatan dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_health'])){
    $stmt=$mysqli->prepare("DELETE FROM health WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Kesihatan dipadam":"❌ ".$stmt->error; $stmt->close();
  }

  /* Pengumuman */
  if(isset($_POST['tambah_pengumuman'])){
    $stmt=$mysqli->prepare("INSERT INTO announcements (tajuk,butiran,tarikh) VALUES (?,?,?)");
    $stmt->bind_param('sss',$_POST['tajuk'],$_POST['butiran'],$_POST['tarikh']);
    $msg=$stmt->execute()?"✅ Pengumuman ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_pengumuman'])){
    $stmt=$mysqli->prepare("UPDATE announcements SET tajuk=?,butiran=?,tarikh=? WHERE id=?");
    $stmt->bind_param('sssi',$_POST['tajuk'],$_POST['butiran'],$_POST['tarikh'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Pengumuman dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_pengumuman'])){
    $stmt=$mysqli->prepare("DELETE FROM announcements WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Pengumuman dipadam":"❌ ".$stmt->error; $stmt->close();
  }

  /* Ticker */
  if(isset($_POST['tambah_ticker'])){
    $stmt=$mysqli->prepare("INSERT INTO ticker (teks,is_active) VALUES (?,1)");
    $stmt->bind_param('s',$_POST['teks']);
    $msg=$stmt->execute()?"✅ Ticker ditambah":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['kemaskini_ticker'])){
    $stmt=$mysqli->prepare("UPDATE ticker SET teks=?,is_active=? WHERE id=?");
    $stmt->bind_param('sii',$_POST['teks'],$_POST['is_active'],$_POST['id']);
    $msg=$stmt->execute()?"✅ Ticker dikemaskini":"❌ ".$stmt->error; $stmt->close();
  }
  if(isset($_POST['padam_ticker'])){
    $stmt=$mysqli->prepare("DELETE FROM ticker WHERE id=?");
    $stmt->bind_param('i',$_POST['id']);
    $msg=$stmt->execute()?"🗑️ Ticker dipadam":"❌ ".$stmt->error; $stmt->close();
  }
}

/* ===================== AMBIL DATA ===================== */
$att=$mysqli->query("SELECT * FROM attendance ORDER BY tarikh DESC LIMIT 20");
$disc=$mysqli->query("SELECT * FROM discipline ORDER BY id DESC LIMIT 20");
$wel=$mysqli->query("SELECT * FROM welfare ORDER BY updated_at DESC LIMIT 10");
$health=$mysqli->query("SELECT * FROM health ORDER BY id DESC LIMIT 20");
$ann=$mysqli->query("SELECT * FROM announcements ORDER BY tarikh DESC,id DESC LIMIT 20");
$tickers=$mysqli->query("SELECT * FROM ticker ORDER BY id DESC LIMIT 20");
?>
<!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">
  <h3 class="m-0">📊 Admin Hal Ehwal Murid (HEM)</h3>
  <button onclick="toggleTheme()" class="btn btn-light btn-sm">🌙/☀️</button>
</div>

<div class="mt-3">
<?php if($msg): ?><div class="alert alert-info"><?=safe($msg)?></div><?php endif; ?>

<!-- Tab Navigation -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#att">Kehadiran</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#disc">Disiplin</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#wel">Kebajikan</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#health">Kesihatan</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#ann">Pengumuman</button></li>
  <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tick">Ticker</button></li>
</ul>

<div class="tab-content mt-3">
  <!-- Kehadiran -->
  <div class="tab-pane fade show active" id="att">
    <div class="card p-3">
      <h5>📅 Kehadiran Murid</h5>
      <form method="post" class="row g-2 mb-3">
        <div class="col"><input type="date" name="tarikh" class="form-control" required></div>
        <div class="col"><input type="number" name="hadir" class="form-control" placeholder="Hadir"></div>
        <div class="col"><input type="number" name="tidak_hadir" class="form-control" placeholder="Tidak Hadir"></div>
        <div class="col"><input type="number" name="lewat" class="form-control" placeholder="Lewat"></div>
        <div class="col"><input type="number" step="0.01" name="peratus" class="form-control" placeholder="%"></div>
        <div class="col"><button class="btn btn-success w-100" name="tambah_kehadiran">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>Tarikh</th><th>Hadir</th><th>Tidak Hadir</th><th>Lewat</th><th>%</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($a=$att->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input type="date" name="tarikh" value="<?=$a['tarikh']?>" class="form-control"></td>
                <td><input type="number" name="hadir" value="<?=$a['hadir']?>" class="form-control"></td>
                <td><input type="number" name="tidak_hadir" value="<?=$a['tidak_hadir']?>" class="form-control"></td>
                <td><input type="number" name="lewat" value="<?=$a['lewat']?>" class="form-control"></td>
                <td><input type="number" step="0.01" name="peratus" value="<?=$a['peratus']?>" class="form-control"></td>
                <td>
                  <input type="hidden" name="id" value="<?=$a['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_kehadiran">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_kehadiran" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Disiplin -->
  <div class="tab-pane fade" id="disc">
    <div class="card p-3">
      <h5>⚠️ Rekod Disiplin</h5>
      <form method="post" class="row g-2 mb-3">
        <div class="col"><input name="bulan" class="form-control" placeholder="2025-09"></div>
        <div class="col"><input type="number" name="kes" class="form-control" placeholder="Kes"></div>
        <div class="col"><input type="number" name="perubahan" class="form-control" placeholder="Perubahan"></div>
        <div class="col"><input name="top1" class="form-control" placeholder="Top1"></div>
        <div class="col"><input name="top2" class="form-control" placeholder="Top2"></div>
        <div class="col"><input name="top3" class="form-control" placeholder="Top3"></div>
        <div class="col"><button class="btn btn-success w-100" name="tambah_disiplin">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>Bulan</th><th>Kes</th><th>Perubahan</th><th>Top1</th><th>Top2</th><th>Top3</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($d=$disc->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input name="bulan" value="<?=$d['bulan']?>" class="form-control"></td>
                <td><input type="number" name="kes" value="<?=$d['kes']?>" class="form-control"></td>
                <td><input type="number" name="perubahan" value="<?=$d['perubahan']?>" class="form-control"></td>
                <td><input name="top1" value="<?=$d['top1']?>" class="form-control"></td>
                <td><input name="top2" value="<?=$d['top2']?>" class="form-control"></td>
                <td><input name="top3" value="<?=$d['top3']?>" class="form-control"></td>
                <td>
                  <input type="hidden" name="id" value="<?=$d['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_disiplin">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_disiplin" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Kebajikan -->
  <div class="tab-pane fade" id="wel">
    <div class="card p-3">
      <h5>🤝 Kebajikan & Bantuan</h5>
      <form method="post" class="row g-2 mb-3">
        <div class="col"><input type="number" name="rmt" class="form-control" placeholder="RMT"></div>
        <div class="col"><input type="number" name="kwapm" class="form-control" placeholder="KWAPM"></div>
        <div class="col"><input type="number" name="zakat" class="form-control" placeholder="Zakat"></div>
        <div class="col"><input type="number" name="bsh" class="form-control" placeholder="BSH"></div>
        <div class="col"><input type="number" step="0.01" name="b40_peratus" class="form-control" placeholder="%B40"></div>
        <div class="col"><button class="btn btn-success w-100" name="tambah_welfare">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>RMT</th><th>KWAPM</th><th>Zakat</th><th>BSH</th><th>%B40</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($w=$wel->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input type="number" name="rmt" value="<?=$w['rmt']?>" class="form-control"></td>
                <td><input type="number" name="kwapm" value="<?=$w['kwapm']?>" class="form-control"></td>
                <td><input type="number" name="zakat" value="<?=$w['zakat']?>" class="form-control"></td>
                <td><input type="number" name="bsh" value="<?=$w['bsh']?>" class="form-control"></td>
                <td><input type="number" step="0.01" name="b40_peratus" value="<?=$w['b40_peratus']?>" class="form-control"></td>
                <td>
                  <input type="hidden" name="id" value="<?=$w['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_welfare">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_welfare" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Kesihatan -->
  <div class="tab-pane fade" id="health">
    <div class="card p-3">
      <h5>🏥 Kesihatan Murid</h5>
      <form method="post" class="row g-2 mb-3">
        <div class="col"><input name="hari" class="form-control" placeholder="Hari"></div>
        <div class="col"><input type="date" name="tarikh" class="form-control"></div>
        <div class="col"><input type="number" name="sakit" class="form-control" placeholder="Sakit"></div>
        <div class="col"><input type="number" name="kemalangan" class="form-control" placeholder="Kemalangan"></div>
        <div class="col"><input type="number" name="ponteng" class="form-control" placeholder="Ponteng"></div>
        <div class="col"><input type="number" name="keluar_tanpa_izin" class="form-control" placeholder="Keluar TI"></div>
        <div class="col"><button class="btn btn-success w-100" name="tambah_health">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>Hari</th><th>Tarikh</th><th>Sakit</th><th>Kemalangan</th><th>Ponteng</th><th>Keluar TI</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($h=$health->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input name="hari" value="<?=$h['hari']?>" class="form-control"></td>
                <td><input type="date" name="tarikh" value="<?=$h['tarikh']?>" class="form-control"></td>
                <td><input type="number" name="sakit" value="<?=$h['sakit']?>" class="form-control"></td>
                <td><input type="number" name="kemalangan" value="<?=$h['kemalangan']?>" class="form-control"></td>
                <td><input type="number" name="ponteng" value="<?=$h['ponteng']?>" class="form-control"></td>
                <td><input type="number" name="keluar_tanpa_izin" value="<?=$h['keluar_tanpa_izin']?>" class="form-control"></td>
                <td>
                  <input type="hidden" name="id" value="<?=$h['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_health">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_health" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Pengumuman -->
  <div class="tab-pane fade" id="ann">
    <div class="card p-3">
      <h5>📢 Pengumuman</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 name="butiran" class="form-control" placeholder="Butiran"></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_pengumuman">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>Tajuk</th><th>Butiran</th><th>Tarikh</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($a=$ann->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input name="tajuk" value="<?=$a['tajuk']?>" class="form-control"></td>
                <td><input name="butiran" value="<?=$a['butiran']?>" class="form-control"></td>
                <td><input type="date" name="tarikh" value="<?=$a['tarikh']?>" class="form-control"></td>
                <td>
                  <input type="hidden" name="id" value="<?=$a['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_pengumuman">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_pengumuman" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Ticker -->
  <div class="tab-pane fade" id="tick">
    <div class="card p-3">
      <h5>📺 Ticker</h5>
      <form method="post" class="row g-2 mb-3">
        <div class="col-8"><input name="teks" class="form-control" placeholder="Teks ticker"></div>
        <div class="col-2"><select name="is_active" class="form-control"><option value="1">Aktif</option><option value="0">Nonaktif</option></select></div>
        <div class="col-2"><button class="btn btn-success w-100" name="tambah_ticker">Tambah</button></div>
      </form>
      <table class="table table-bordered table-sm">
        <thead class="table-light"><tr><th>Teks</th><th>Status</th><th>Tindakan</th></tr></thead>
        <tbody>
          <?php while($t=$tickers->fetch_assoc()): ?>
            <tr>
              <form method="post">
                <td><input name="teks" value="<?=$t['teks']?>" class="form-control"></td>
                <td>
                  <select name="is_active" class="form-control">
                    <option value="1" <?=$t['is_active']?'selected':''?>>Aktif</option>
                    <option value="0" <?=!$t['is_active']?'selected':''?>>Nonaktif</option>
                  </select>
                </td>
                <td>
                  <input type="hidden" name="id" value="<?=$t['id']?>">
                  <button class="btn btn-primary btn-sm" name="kemaskini_ticker">Simpan</button>
                  <button class="btn btn-danger btn-sm" name="padam_ticker" onclick="return confirm('Padam rekod ini?')">Padam</button>
                </td>
              </form>
            </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </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>
