<?php
session_start();
require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');

if (!isset($_SESSION['userid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$db = new App_DB(DBU::$dba_user);
$data = json_decode(file_get_contents('php://input'), true);

$darja = $data['darja_sno'] ?? '';

if (empty($darja)) {
    echo json_encode(['success' => false, 'message' => 'Missing darja parameter']);
    exit();
}

$sql = "SELECT DISTINCT YEAR(promotionDate) as year 
        FROM stddarjaat 
        WHERE darja = :darja 
        ORDER BY year DESC";

$years = $db->getRecordSet($sql, ['darja' => $darja]);

echo json_encode(['success' => true, 'years' => $years]);
