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

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

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