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

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.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  = '';

    if(isset($_FILES['image']) && $_FILES['image']['error'] === 0){
        $allowed = ['jpg','jpeg','png','webp'];
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if(!in_array($ext, $allowed)){
            $error = 'Invalid image format.';
        } else {
            $image_name = 'room_' . time() . '.' . $ext;
            move_uploaded_file($_FILES['image']['tmp_name'], '../../images/' . $image_name);
        }
    }

    if(!$error){
        $insert = $conn->prepare("INSERT INTO rooms (name, hotel_id, hotel_name, price, num_persons, num_beds, size, view, image, status) VALUES (:name,:hotel_id,:hotel_name,:price,:num_persons,:num_beds,:size,:view,:image,:status)");
        $insert->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,
        ]);
        header("location: index.php?success=Room created successfully");
        exit;
    }
}
?>

<div class="topbar">
    <h4><i class="fa fa-plus mr-2"></i> Add 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;">Add New Room</h5>
                <?php if($error): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

                <form method="POST" action="create.php" 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" placeholder="e.g. Deluxe Suite" 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>
                                    <option value="">Select Hotel</option>
                                    <?php foreach($hotels as $h): ?>
                                    <option value="<?php echo $h->id; ?>" data-name="<?php echo htmlspecialchars($h->name); ?>"><?php echo htmlspecialchars($h->name); ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <input type="hidden" name="hotel_name" id="hotelName">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Price per Night (GH&#8373;)</label>
                                <input type="number" name="price" class="form-control" placeholder="15000" min="0" step="0.01" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Max Persons</label>
                                <input type="number" name="num_persons" class="form-control" placeholder="2" min="1" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Number of Beds</label>
                                <input type="number" name="num_beds" class="form-control" placeholder="1" min="1" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Room Size (m²)</label>
                                <input type="text" name="size" class="form-control" placeholder="35" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>View Type</label>
                                <input type="text" name="view" class="form-control" placeholder="Ocean, City, Garden..." required>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Room Image</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" checked>
                                    <label class="custom-control-label" for="statusSwitch">Active (visible to users)</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <button type="submit" name="submit" class="btn btn-admin-primary"><i class="fa fa-save mr-1"></i> Create 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"; ?>
