<?php
/**
 * SISPENRA - API: Get Progress Input Nilai
 */
require_once '../includes/functions.php';
requireLogin();
header('Content-Type: application/json');

$kelasId = intval($_GET['kelas_id'] ?? 0);
$pengaturan = getPengaturan();

$data = [];
$mapels = db_fetchAll("SELECT * FROM mata_pelajaran WHERE status='aktif' ORDER BY urutan");
$totalSiswa = $kelasId ? db_fetch("SELECT COUNT(*) as c FROM siswa WHERE kelas_id = ? AND status='aktif'", [$kelasId])['c'] : 0;

foreach ($mapels as $m) {
    $sudah = $kelasId ? db_fetch("SELECT COUNT(*) as c FROM nilai WHERE kelas_id = ? AND mapel_id = ? AND tahun_ajaran = ? AND semester = ?",
        [$kelasId, $m['id'], $pengaturan['tahun_ajaran_aktif'], $pengaturan['semester_aktif']])['c'] : 0;

    $data[] = [
        'mapel_id' => $m['id'],
        'nama_mapel' => $m['nama_mapel'],
        'sudah' => $sudah,
        'total' => $totalSiswa,
        'persen' => $totalSiswa > 0 ? round(($sudah / $totalSiswa) * 100) : 0,
    ];
}

echo json_encode(['data' => $data]);
