<?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;

// Total siswa
$query = "SELECT COUNT(*) as total FROM bkcare_siswa WHERE status = 'aktif'";
$result = mysqli_query($conn, $query);
$total_siswa = mysqli_fetch_assoc($result)['total'];

// Total konseling aktif
$query = "SELECT COUNT(*) as total FROM bkcare_konseling WHERE status = 'proses' AND id_tahun_ajaran = '$id_tahun_ajaran'";
$result = mysqli_query($conn, $query);
$total_konseling = mysqli_fetch_assoc($result)['total'];

// Total laporan baru
$query = "SELECT COUNT(*) as total FROM bkcare_laporan_masalah WHERE status = 'baru' AND id_tahun_ajaran = '$id_tahun_ajaran'";
$result = mysqli_query($conn, $query);
$total_laporan = mysqli_fetch_assoc($result)['total'];

// Total pelanggaran
$query = "SELECT COUNT(*) as total FROM bkcare_pelanggaran WHERE id_tahun_ajaran = '$id_tahun_ajaran'";
$result = mysqli_query($conn, $query);
$total_pelanggaran = mysqli_fetch_assoc($result)['total'];

echo json_encode([
    'total_siswa' => $total_siswa,
    'total_konseling' => $total_konseling,
    'total_laporan' => $total_laporan,
    'total_pelanggaran' => $total_pelanggaran
]);
?>