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

$village_id = $_GET['village_id'];

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

<h3>Sub Admins</h3>

<table class="table table-bordered">
    <tr>
        <th>Name</th>
        <th>Mobile</th>
        <th>Village</th>
        <th>Action</th>
    </tr>

    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['name'] ?></td>
        <td><?= $row['mobile'] ?></td>
        <td><?= $row['village_name'] ?></td>
        <td>
            <a href="edit.php?id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">Edit</a>
            <a href="delete.php?id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Delete?')">Delete</a>
            <a href="password.php?id=<?= $row['id'] ?>" class="btn btn-info btn-sm">Password</a>
        </td>
    </tr>
    <?php } ?>
</table>

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