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

<?php
require 'includes/db_connect.php';

$id = intval($_GET['id']);

$result = $conn->query("SELECT * FROM houses WHERE id = $id");
$house = $result->fetch_assoc();

if (!$house) {
    echo "<div class='alert alert-danger'>House not found.</div>";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $street_id     = $_POST['street_id']; // now safe
    $house_number  = $_POST['house_number'];
    $occupant_name = $_POST['occupant_name'];
    $latitude      = $_POST['latitude'];
    $longitude     = $_POST['longitude'];
    $status        = $_POST['status'];
    $description   = $_POST['description'];

    $stmt = $conn->prepare("
        UPDATE houses SET 
            street_id = ?,
            house_number = ?,
            occupant_name = ?,
            latitude = ?,
            longitude = ?,
            status = ?,
            description = ?
        WHERE id = ?
    ");

    $stmt->bind_param(
        "issssssi",
        $street_id,
        $house_number,
        $occupant_name,
        $latitude,
        $longitude,
        $status,
        $description,
        $id
    );

    if ($stmt->execute()) {
        echo "<div class='alert alert-success'>House updated successfully.</div>";
    } else {
        echo "<div class='alert alert-danger'>Update failed.</div>";
    }
}
?>


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

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

        <form method="POST">

            <div class="mb-3">
    <label class="form-label">Street</label>

    <!-- HIDDEN INPUT (THIS FIXES NULL ISSUE) -->
    <input type="hidden" name="street_id" value="<?= $house['street_id']; ?>">

    <select class="form-control" disabled>
        <?php
        $streets = $conn->query("SELECT * FROM registered_streets ORDER BY street_name ASC");
        while ($row = $streets->fetch_assoc()) {
            $selected = ($row['id'] == $house['street_id']) ? "selected" : "";
            echo "<option $selected>{$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" value="<?= $house['house_number'] ?>" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Occupant Name</label>
                <input type="text" name="occupant_name" class="form-control" value="<?= $house['occupant_name'] ?>">
            </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" value="<?= $house['latitude'] ?>">
                </div>

                <div class="col-md-6 mb-3">
                    <label class="form-label">Longitude</label>
                    <input type="text" name="longitude" class="form-control" value="<?= $house['longitude'] ?>">
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Status</label>
                <select name="status" class="form-control">
                    <option value="pending" <?= $house['status']=='pending'?'selected':'' ?>>Pending</option>
                    <option value="verified" <?= $house['status']=='verified'?'selected':'' ?>>Verified</option>
                </select>
            </div>

            <div class="mb-3">
                <label>Description</label>
                <textarea name="description" class="form-control" rows="3"><?= $house['description'] ?></textarea>
            </div>

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

        </form>

    </div>
</div>

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