<?php
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
    header('Access-Control-Allow-Methods: GET, OPTIONS');
    header('Access-Control-Allow-Headers: Authorization, Content-Type');
    http_response_code(200);
    exit;
}
header('Access-Control-Allow-Origin: https://drarnya.itcprogram.com');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header('Content-Type: application/json');

require_once '../config/database.php';
require_once 'auth.php';

$user = validateToken();
$conn = getConnection();
$result = $conn->query("SELECT id, name FROM units WHERE is_active = 1 ORDER BY name");
$units = [];
while ($row = $result->fetch_assoc()) {
    $units[] = $row;
}
echo json_encode($units);