<?php
include("../includes/auth.php");
include("../config/db.php");
include("../includes/header.php");
include("../includes/sidebar.php");

$result = $conn->query("
    SELECT villages.id, villages.village_name, COUNT(users.id) as total_sub_admins
    FROM villages
    LEFT JOIN users ON users.village_id = villages.id AND users.role='sub_admin'
    GROUP BY villages.id
");
?>

<h3>Sub Admin List</h3>

<table class="table table-bordered">
    <tr>
        <th>Village</th>
        <th>Total Sub Admin</th>
        <th>Action</th>
    </tr>

    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['village_name'] ?></td>
        <td><?= $row['total_sub_admins'] ?></td>
        <td>
            <a href="view.php?village_id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">
                View All
            </a>
        </td>
    </tr>
    <?php } ?>
</table>

<?php include("../includes/footer.php"); ?>