<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<h3 class="text-orange fw-bold">Add New House</h3>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $street_id = $_POST['street_id'];
    $house_number = $_POST['house_number'];
    $occupant_name = $_POST['occupant_name'];
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];
    $description = $_POST['description'];
    // $street_id = $_POST['street_id'];

// 1. Validate street exists
$check = $conn->query("SELECT street_id FROM registered_streets WHERE street_id = '$street_id'");

if (!$check || $check->num_rows == 0) {
    die("❌ Error: The selected street does not exist. Please add the street first.");
}


    $sql = "INSERT INTO houses (street_id, house_number, occupant_name, latitude, longitude, description)
            VALUES ('$street_id', '$house_number', '$occupant_name', '$latitude', '$longitude', '$description')";

    if ($conn->query($sql)) {
        echo "<div class='alert alert-success'>House added successfully.</div>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . $conn->error . "</div>";
    }
}
?>

<div class="card shadow mt-4">
    <div class="card-body">
        <form method="POST">

            <div class="mb-3">
                <label class="form-label">Street</label>
                <select name="street_id" class="form-control" required>
                    <option value="">Select Street</option>
                    <?php
                    $streets = $conn->query("SELECT * FROM registered_streets ORDER BY street_name ASC");
                    while ($row = $streets->fetch_assoc()) {
                        echo "<option value='{$row['street_id']}'>{$row['street_name']}</option>";
                    }
                    ?>
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">House Number</label>
                <input type="text" name="house_number" class="form-control" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Occupant Name (optional)</label>
                <input type="text" name="occupant_name" class="form-control">
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Latitude</label>
                    <input type="text" name="latitude" class="form-control">
                </div>

                <div class="col-md-6 mb-3">
                    <label class="form-label">Longitude</label>
                    <input type="text" name="longitude" class="form-control">
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Description (optional)</label>
                <textarea name="description" class="form-control" rows="3"></textarea>
            </div>

            <button type="submit" class="btn btn-orange">Save House</button>

        </form>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
