<?php require "../includes/header.php"; ?>
<?php require "../../config/config.php"; ?>

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }
if(!isset($_GET['id'])){ header("location: index.php"); exit; }

$id     = (int)$_GET['id'];
$room   = $conn->prepare("SELECT * FROM rooms WHERE id = :id");
$room->execute([':id' => $id]);
$room   = $room->fetch(PDO::FETCH_OBJ);
if(!$room){ header("location: index.php"); exit; }

$hotels = $conn->query("SELECT * FROM hotels WHERE status = 1")->fetchAll(PDO::FETCH_OBJ);
$error  = '';

if(isset($_POST['submit'])){
    $name        = $_POST['name'];
    $hotel_id    = $_POST['hotel_id'];
    $hotel_name  = $_POST['hotel_name'];
    $price       = $_POST['price'];
    $num_persons = $_POST['num_persons'];
    $num_beds    = $_POST['num_beds'];
    $size        = $_POST['size'];
    $view        = $_POST['view'];
    $status      = isset($_POST['status']) ? 1 : 0;
    $image_name  = $room->image;

    if(isset($_FILES['image']) && $_FILES['image']['error'] === 0){
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        $image_name = 'room_' . time() . '.' . $ext;
        move_uploaded_file($_FILES['image']['tmp_name'], '../../images/' . $image_name);
    }

    $upd = $conn->prepare("UPDATE rooms SET name=:name,hotel_id=:hotel_id,hotel_name=:hotel_name,price=:price,num_persons=:num_persons,num_beds=:num_beds,size=:size,view=:view,image=:image,status=:status WHERE id=:id");
    $upd->execute([':name'=>$name,':hotel_id'=>$hotel_id,':hotel_name'=>$hotel_name,':price'=>$price,':num_persons'=>$num_persons,':num_beds'=>$num_beds,':size'=>$size,':view'=>$view,':image'=>$image_name,':status'=>$status,':id'=>$id]);
    header("location: index.php?success=Room updated successfully");
    exit;
}
?>

<div class="topbar">
    <h4><i class="fa fa-edit mr-2"></i> Edit Room</h4>
    <div class="admin-badge">
        <div class="avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <span><?php echo htmlspecialchars($_SESSION['admin_username']); ?></span>
        <a href="../logout.php"><i class="fa fa-sign-out"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="admin-form-card">
                <h5 class="mb-4" style="color:#1a2d5a;font-weight:700;">Edit Room: <?php echo htmlspecialchars($room->name); ?></h5>

                <?php if($room->image): ?>
                <div class="mb-3"><img src="<?php echo APPURL; ?>/images/<?php echo htmlspecialchars($room->image); ?>" style="height:100px;border-radius:8px;"></div>
                <?php endif; ?>

                <form method="POST" action="update.php?id=<?php echo $id; ?>" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Room Name</label>
                                <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($room->name); ?>" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Hotel</label>
                                <select name="hotel_id" class="form-control" id="hotelSelect" onchange="setHotelName()" required>
                                    <?php foreach($hotels as $h): ?>
                                    <option value="<?php echo $h->id; ?>" data-name="<?php echo htmlspecialchars($h->name); ?>" <?php echo $h->id == $room->hotel_id ? 'selected' : ''; ?>><?php echo htmlspecialchars($h->name); ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <input type="hidden" name="hotel_name" id="hotelName" value="<?php echo htmlspecialchars($room->hotel_name ?? ''); ?>">
                            </div>
                        </div>
                        <div class="col-md-4"><div class="form-group"><label>Price/Night (GH&#8373;)</label><input type="number" name="price" class="form-control" value="<?php echo $room->price ?? 0; ?>" step="0.01"></div></div>
                        <div class="col-md-4"><div class="form-group"><label>Max Persons</label><input type="number" name="num_persons" class="form-control" value="<?php echo $room->num_persons; ?>"></div></div>
                        <div class="col-md-4"><div class="form-group"><label>Num Beds</label><input type="number" name="num_beds" class="form-control" value="<?php echo $room->num_beds; ?>"></div></div>
                        <div class="col-md-6"><div class="form-group"><label>Size (m²)</label><input type="text" name="size" class="form-control" value="<?php echo $room->size; ?>"></div></div>
                        <div class="col-md-6"><div class="form-group"><label>View</label><input type="text" name="view" class="form-control" value="<?php echo $room->view; ?>"></div></div>
                        <div class="col-md-12"><div class="form-group"><label>Replace Image (optional)</label><input type="file" name="image" class="form-control" accept="image/*"></div></div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <div class="custom-control custom-switch">
                                    <input type="checkbox" class="custom-control-input" id="statusSwitch" name="status" <?php echo $room->status ? 'checked' : ''; ?>>
                                    <label class="custom-control-label" for="statusSwitch">Active</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <button type="submit" name="submit" class="btn btn-admin-primary"><i class="fa fa-save mr-1"></i> Update Room</button>
                    <a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function setHotelName(){
    var sel = document.getElementById('hotelSelect');
    document.getElementById('hotelName').value = sel.options[sel.selectedIndex].getAttribute('data-name');
}
</script>

<?php require "../includes/footer.php"; ?>
