<?php

header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

require_once("../config/db.php");

$village_id = intval($_GET['village_id'] ?? 0);

if (!$village_id) {
    echo json_encode([
        "status" => false,
        "message" => "Village ID required"
    ]);
    exit;
}

// ✅ GET APPROVED USERS
$query = "SELECT id, name, father_name, mobile, dob, created_at 
FROM users 
WHERE village_id = '$village_id' 
AND role = 'sub_admin' 
AND status = 'approved'
ORDER BY id DESC";

$result = $conn->query($query);

$data = [];

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

echo json_encode([
    "status" => true,
    "data" => $data
]);