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

include("../config/db.php");

// fetch districts
$districts = $conn->query("SELECT * FROM districts");

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $district_id = $_POST['district_id'];

    $conn->query("INSERT INTO blocks (name, district_id) VALUES ('$name', '$district_id')");
    header("Location: index.php");
}
?>

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

<h3>Add Block</h3>

<form method="POST">
    <div class="mb-3">
        <label>Block Name</label>
        <input type="text" name="name" class="form-control" required>
    </div>

    <div class="mb-3">
        <label>Select District</label>
        <select name="district_id" class="form-control" required>
            <option value="">-- Select District --</option>
            <?php while($d = $districts->fetch_assoc()) { ?>
                <option value="<?= $d['id'] ?>"><?= $d['name'] ?></option>
            <?php } ?>
        </select>
    </div>

    <button name="submit" class="btn btn-success">Save</button>
</form>

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