<?php
require 'db.php';
header('Content-Type: application/json');

if (isset($_GET['country_id'])) {
    $country_id = (int)$_GET['country_id'];
    $states = [];
    
    $stmt = $mysqli1->prepare("SELECT id, name FROM states WHERE country_id = ?");
    $stmt->bind_param("i", $country_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        $states[] = $row;
    }
    
    echo json_encode($states);
} else {
    echo json_encode([]);
}
?>