<?php

require_once __DIR__ . '/../includes/config.php';

header('Content-Type: application/json');

$db = getDB();

$state_id = (int)($_GET['state_id'] ?? 0);

if (!$state_id) {
    echo json_encode([
        'success' => false,
        'data' => []
    ]);
    exit;
}

$stmt = $db->prepare("
    SELECT id, name
    FROM cities
    WHERE state_id = ?
    AND status = 1
    ORDER BY name ASC
");

$stmt->execute([$state_id]);

$cities = $stmt->fetchAll(PDO::FETCH_ASSOC);

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