<?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;
$today = date('Y-m-d');

$query = "SELECT jk.*, s.nama as nama_siswa, u.nama as nama_guru 
          FROM bkcare_jadwal_konseling jk
          JOIN bkcare_siswa s ON jk.id_siswa = s.id_siswa
          LEFT JOIN bkcare_users u ON jk.id_guru_bk = u.id_user
          WHERE jk.tanggal = '$today' AND jk.id_tahun_ajaran = '$id_tahun_ajaran'
          ORDER BY jk.jam ASC";

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

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

echo json_encode($data);
?>