<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$user_role = $_SESSION['role'] ?? 'guest';

// Only admin or manager can edit
if(!in_array($user_role,['admin','manager'])){
    echo "<div class='alert alert-danger'>You do not have permission to edit rooms.</div>";
    exit;
}

$room_id = intval($_GET['id'] ?? 0);
if(!$room_id){
    echo "<div class='alert alert-danger'>Invalid room ID.</div>";
    exit;
}

// Fetch room details
$result = $conn->query("SELECT * FROM rooms WHERE id=$room_id");
if($result->num_rows == 0){
    echo "<div class='alert alert-danger'>Room not found.</div>";
    exit;
}

$room = $result->fetch_assoc();
$message = '';

// ==================== Handle Update ====================
if(isset($_POST['update_room'])){
    $room_number = $conn->real_escape_string($_POST['room_number']);
    $room_type   = $conn->real_escape_string($_POST['room_type']);
    $capacity    = intval($_POST['capacity']);
    $price       = floatval($_POST['price']);
    $status      = $conn->real_escape_string($_POST['status']);

    $update_sql = "UPDATE rooms SET 
                    room_number='$room_number',
                    room_type='$room_type',
                    capacity=$capacity,
                    price=$price,
                    status='$status'
                   WHERE id=$room_id";

    if($conn->query($update_sql)){
        $message = "<div class='alert alert-success'>Room updated successfully!</div>";
        // Refresh room data
        $result = $conn->query("SELECT * FROM rooms WHERE id=$room_id");
        $room = $result->fetch_assoc();
    } else {
        $message = "<div class='alert alert-danger'>Error: ".$conn->error."</div>";
    }
}
?>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Edit Room <?= htmlspecialchars($room['room_number']) ?></h3>
        <a href="rooms.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Back</a>
    </div>
    <hr>
    <?= $message ?>

    <div class="card shadow-sm">
        <div class="card-body">
            <form method="POST" class="row g-3">
                <div class="col-md-3">
                    <label>Room Number</label>
                    <input type="text" name="room_number" class="form-control" value="<?= htmlspecialchars($room['room_number']) ?>" required>
                </div>
                <div class="col-md-3">
                    <label>Room Type</label>
                    <input type="text" name="room_type" class="form-control" value="<?= htmlspecialchars($room['room_type']) ?>" required>
                </div>
                <div class="col-md-2">
                    <label>Capacity</label>
                    <input type="number" name="capacity" class="form-control" value="<?= intval($room['capacity']) ?>" required>
                </div>
                <div class="col-md-2">
                    <label>Price (MK)</label>
                    <input type="number" step="0.01" name="price" class="form-control" value="<?= number_format($room['price'],2) ?>" required>
                </div>
                <div class="col-md-2">
                    <label>Status</label>
                    <select name="status" class="form-control" required>
                        <?php
                        $statuses = ['Available','Occupied','Reserved','Maintenance','Dirty','Out of Order'];
                        foreach($statuses as $s){
                            $selected = ($s==$room['status'])?'selected':'';
                            echo "<option value='$s' $selected>$s</option>";
                        }
                        ?>
                    </select>
                </div>
                <div class="col-md-12 mt-3">
                    <button type="submit" name="update_room" class="btn btn-primary w-100">
                        <i class="fa fa-save"></i> Update Room
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

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