<?php
session_start();
require_once '../config/database.php';
require_once '../config/function.php';

header('Content-Type: application/json');

if (!isset($_SESSION['id_user'])) {
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

$tahun_ajaran = getTahunAjaranAktif();
$id_tahun_ajaran = $tahun_ajaran ? $tahun_ajaran['id_tahun_ajaran'] : 0;

$query = "SELECT lm.*, s.nama as nama_siswa 
          FROM bkcare_laporan_masalah lm
          JOIN bkcare_siswa s ON lm.id_siswa = s.id_siswa
          WHERE lm.id_tahun_ajaran = '$id_tahun_ajaran'
          ORDER BY lm.tanggal DESC
          LIMIT 5";

$result = mysqli_query($conn, $query);
$data = [];

while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

echo json_encode($data);
?>