<?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");

// JOIN with districts
$result = $conn->query("
    SELECT blocks.*, districts.name as district_name 
    FROM blocks 
    LEFT JOIN districts ON blocks.district_id = districts.id
    ORDER BY blocks.id DESC
");
?>

<h3>Blocks</h3>

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

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

    <?php while($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= $row['name'] ?></td>
        <td><?= $row['district_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"); ?>