<?php
/**
 * SISPENRA - Edit Nilai Individual
 */
require_once '../../includes/functions.php';
requireLogin();

$pageTitle = 'Edit Nilai';
$activeMenu = 'nilai-input';
$id = intval($_GET['id'] ?? 0);

$nilai = db_fetch("
    SELECT n.*, s.nama as nama_siswa, s.nis, m.nama_mapel, m.kkm, k.nama_kelas
    FROM nilai n
    JOIN siswa s ON s.id = n.siswa_id
    JOIN mata_pelajaran m ON m.id = n.mapel_id
    JOIN kelas k ON k.id = n.kelas_id
    WHERE n.id = ?
", [$id]);

if (!$nilai) { setFlash('danger', 'Data nilai tidak ditemukan'); header('Location: ' . url('modules/nilai/input.php')); exit; }
$data = $nilai;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nt  = floatval($_POST['nilai_tugas']);
    $nh  = floatval($_POST['nilai_harian']);
    $uts = floatval($_POST['nilai_uts']);
    $uas = floatval($_POST['nilai_uas']);
    $akhir = round(($nt * 0.20) + ($nh * 0.20) + ($uts * 0.30) + ($uas * 0.30), 2);

    db_update('nilai', [
        'nilai_tugas' => $nt, 'nilai_harian' => $nh,
        'nilai_uts' => $uts, 'nilai_uas' => $uas,
        'nilai_akhir' => $akhir,
        'predikat' => getPredikat($akhir, $nilai['kkm']),
        'catatan' => sanitize($_POST['catatan'] ?? ''),
    ], 'id = :id', ['id' => $id]);

    setFlash('success', 'Nilai berhasil diperbarui!');
    header("Location: input.php?kelas_id={$nilai['kelas_id']}&mapel_id={$nilai['mapel_id']}");
    exit;
}

include '../../templates/header.php';
?>
<div class="card" style="max-width:600px;">
  <div class="card-header">
    <span class="card-title">Edit Nilai: <?= htmlspecialchars($nilai['nama_siswa']) ?></span>
    <a href="input.php?kelas_id=<?= $nilai['kelas_id'] ?>&mapel_id=<?= $nilai['mapel_id'] ?>" class="btn btn-outline btn-sm">Kembali</a>
  </div>
  <div class="card-body">
    <div style="background:var(--bg);border-radius:8px;padding:12px 16px;margin-bottom:16px;font-size:13px;">
      <strong><?= htmlspecialchars($nilai['nama_mapel']) ?></strong> &bull;
      Kelas <?= htmlspecialchars($nilai['nama_kelas']) ?> &bull;
      NIS: <?= htmlspecialchars($nilai['nis']) ?> &bull;
      KKM: <?= $nilai['kkm'] ?>
    </div>
    <form method="POST">
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">Nilai Tugas (20%)</label>
          <input type="number" name="nilai_tugas" id="nilai_tugas" class="form-control nilai-input" min="0" max="100" step="0.5" value="<?= $data['nilai_tugas'] ?>" oninput="hitungNilaiAkhir()">
        </div>
        <div class="form-group">
          <label class="form-label">Nilai Harian (20%)</label>
          <input type="number" name="nilai_harian" id="nilai_harian" class="form-control nilai-input" min="0" max="100" step="0.5" value="<?= $data['nilai_harian'] ?>" oninput="hitungNilaiAkhir()">
        </div>
        <div class="form-group">
          <label class="form-label">Nilai UTS (30%)</label>
          <input type="number" name="nilai_uts" id="nilai_uts" class="form-control nilai-input" min="0" max="100" step="0.5" value="<?= $data['nilai_uts'] ?>" oninput="hitungNilaiAkhir()">
        </div>
        <div class="form-group">
          <label class="form-label">Nilai UAS (30%)</label>
          <input type="number" name="nilai_uas" id="nilai_uas" class="form-control nilai-input" min="0" max="100" step="0.5" value="<?= $data['nilai_uas'] ?>" oninput="hitungNilaiAkhir()">
        </div>
      </div>
      <div style="background:var(--bg);border-radius:8px;padding:14px 16px;margin-bottom:16px;display:flex;align-items:center;gap:20px;">
        <div>
          <div style="font-size:12px;color:var(--text-muted);">Nilai Akhir</div>
          <div style="font-size:28px;font-weight:800;color:var(--primary);" id="nilai_akhir_preview"><?= number_format($data['nilai_akhir'], 2) ?></div>
        </div>
        <div>
          <div style="font-size:12px;color:var(--text-muted);">Predikat</div>
          <span class="badge badge-<?= ['A'=>'success','B'=>'info','C'=>'warning','D'=>'danger'][$data['predikat']] ?? 'gray' ?>" id="predikat_preview" style="font-size:16px;padding:5px 12px;"><?= $data['predikat'] ?></span>
        </div>
      </div>
      <div class="form-group">
        <label class="form-label">Catatan</label>
        <textarea name="catatan" class="form-control" rows="2"><?= htmlspecialchars($data['catatan'] ?? '') ?></textarea>
      </div>
      <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
        <a href="input.php?kelas_id=<?= $nilai['kelas_id'] ?>&mapel_id=<?= $nilai['mapel_id'] ?>" class="btn btn-outline">Batal</a>
      </div>
    </form>
  </div>
</div>
<?php include '../../templates/footer.php'; ?>
