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

<?php
// $id = $_GET['id'];
// $result = $conn->query("SELECT * FROM registered_streets WHERE id=$id");
// $street = $result->fetch_assoc();

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

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

$sql = "SELECT * FROM registered_streets WHERE street_id = $id";
$result = $conn->query($sql);

if (!$result) {
    die("Query failed: " . $conn->error);
}

$row = $result->fetch_assoc();

if (!$row) {
    die("No street found with this ID");
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // $street_name = $_POST['street_name'];
    $town = $_POST['town'];
    $street_numbered = $_POST['street_numbered'];
    // $description = $_POST['description'];

    $sql = "UPDATE registered_streets SET 
            
            town='$town',
            street_numbered='$street_numbered'
            
            WHERE street_id=$id";

    if ($conn->query($sql)) {
        echo "<div class='alert alert-success'>Street updated successfully.</div>";
    } else {
        echo "<div class='alert alert-danger'>Error updating street.</div>";
    }
}
?>

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

<div class="card shadow mt-4">
    <div class="card-body">
        <form method="POST">
            <div class="mb-3">
                <label class="form-label">Street Name</label>
                <input type="text" name="street_name" class="form-control" value="<?= $row['street_name'] ?>" required disabled>
            </div>

            <div class="mb-3">
                <label class="form-label">Location</label>
                <input type="text" name="town" class="form-control" value="<?= $row['town'] ?>" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Is Street Numbered?</label>
                <select name="street_numbered" class="form-control">
                    <option value="NO" <?= $row['street_numbered']=='no'?'selected':'' ?>>No</option>
                    <option value="YES" <?= $row['street_numbered']=='Yes'?'selected':'' ?>>Yes</option>
                </select>
            </div>

           

            <button type="submit" class="btn btn-success">Update Street</button>
        </form>
    </div>
</div>

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