<?php
declare(strict_types=1);

require_once __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../../config/auth.php';

require_login();
header('Content-Type: application/json; charset=utf-8');

$block = strtoupper(trim((string)($_GET['block_id'] ?? '')));
if ($block === '') {
    echo json_encode([]);
    exit;
}

try {
    $st = $pdo->prepare('SELECT id, name FROM gps WHERE block_id = ? ORDER BY name ASC');
    $st->execute([$block]);
    $rows = $st->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($rows);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode([]);
}
