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

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

<h3>Village Admin</h3>

<a href="add.php" class="btn btn-primary mb-3">+ Add Village Admin</a>

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

    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= $row['name'] ?></td>
        <td><?= $row['mobile'] ?></td>
        <td><?= $row['village_name'] ?></td>
        <td><?= $row['status'] ?></td>
        <td>
            <a href="edit.php?id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">Edit</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"); ?>