<?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;
}

// 🔍 JOIN QUERY (FULL DETAILS)
$query = "SELECT 
    u.id,
    u.name,
    u.father_name,
    u.mobile,
    u.dob,
    u.created_at,

    v.village_name,
    b.name AS block_name,
    d.name AS district_name

FROM users u

LEFT JOIN villages v ON u.village_id = v.id
LEFT JOIN blocks b ON v.block_id = b.id
LEFT JOIN districts d ON b.district_id = d.id

WHERE u.village_id = '$village_id'
AND u.role = 'sub_admin'
AND u.status = 'pending'

ORDER BY u.id DESC";

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

$data = [];

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

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