<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header("Location: ../auth/login.php");
    exit;
}

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

$result = $conn->query("
    SELECT villages.*, blocks.name as block_name 
    FROM villages
    LEFT JOIN blocks ON villages.block_id = blocks.id
    ORDER BY villages.id DESC
");
?>

<h3>Villages</h3>

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

<table class="table table-bordered">
    <tr>
        <th>ID</th>
        <th>Village</th>
        <th>District</th>
        <th>Block</th>
        <th>Action</th>
    </tr>

    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= $row['village_name'] ?></td>
        <td><?= $row['district'] ?></td>
        <td><?= $row['block_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>
        </td>
    </tr>
    <?php } ?>
</table>

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