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

// Handle form submission
if(isset($_POST['add_room'])){

    $room_number = $_POST['room_number'];
    $room_type = $_POST['room_type'];
    $floor = intval($_POST['floor']);
    $capacity = intval($_POST['capacity']);
    $status = $_POST['status'];
    $housekeeping_status = $_POST['housekeeping_status'];
    $last_cleaned = !empty($_POST['last_cleaned']) ? $_POST['last_cleaned'] : NULL;
    $notes = $_POST['notes'];

    $plan_name = $_POST['plan_name'];
    $price = floatval($_POST['price']);

    // 1️⃣ Insert Room
    $stmt = $conn->prepare("INSERT INTO rooms 
        (room_number, room_type, floor, capacity, status, housekeeping_status, last_cleaned, notes) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

    $stmt->bind_param("ssiissss", 
        $room_number, 
        $room_type, 
        $floor, 
        $capacity, 
        $status, 
        $housekeeping_status, 
        $last_cleaned, 
        $notes
    );

    if($stmt->execute()){

        $room_id = $stmt->insert_id;

        // 2️⃣ Insert Pricing Plan
        $pricing = $conn->prepare("INSERT INTO room_pricing 
            (room_id, plan_name, capacity, price) 
            VALUES (?, ?, ?, ?)");

        $pricing->bind_param("isid", $room_id, $plan_name, $capacity, $price);
        $pricing->execute();

        echo "<script>alert('Room added successfully');window.location='room_availability.php';</script>";
        exit;
    }
}
?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
<h4>Add New Room</h4>
<hr>

<form method="POST" class="row g-3">
    <div class="col-md-3">
        <input type="text" name="room_number" class="form-control" placeholder="Room Number" required>
    </div>
    <div class="col-md-3">
        <input type="text" name="room_type" class="form-control" placeholder="Room Type (e.g. Single, Double)" required>
    </div>
    <div class="col-md-2">
        <input type="number" name="floor" class="form-control" placeholder="Floor">
    </div>
    <div class="col-md-2">
        <input type="number" name="capacity" class="form-control" placeholder="Capacity" value="1">
    </div>
    <div class="col-md-2">
        <input type="number" step="0.01" name="price" class="form-control" placeholder="Price per Night" required>
    </div>
    <div class="col-md-3">
    <input type="text" name="plan_name" class="form-control" placeholder="Plan Name (Single / Family)" required>
</div>


    <div class="col-md-3">
        <select name="status" class="form-control">
            <option value="Available">Available</option>
            <option value="Occupied">Occupied</option>
            <option value="Reserved">Reserved</option>
            <option value="Maintenance">Maintenance</option>
            <option value="Dirty">Dirty</option>
            <option value="Out of Order">Out of Order</option>
        </select>
    </div>
    <div class="col-md-3">
        <select name="housekeeping_status" class="form-control">
            <option value="Clean">Clean</option>
            <option value="Dirty">Dirty</option>
            <option value="Inspected">Inspected</option>
        </select>
    </div>
    <div class="col-md-3">
        <input type="datetime-local" name="last_cleaned" class="form-control">
    </div>
    <div class="col-md-12">
        <textarea name="notes" class="form-control" placeholder="Notes (Optional)"></textarea>
    </div>
    <div class="col-md-12 mt-2">
        <button type="submit" name="add_room" class="btn btn-success btn-custom">
            <i class="fa fa-plus"></i> Add Room
        </button>
    </div>
</form>
</div>

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