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

<?php
$hotel_id = isset($_GET['id']) ? (int)$_GET['id'] : null;

if($hotel_id){
    $stmt = $conn->prepare("SELECT * FROM rooms WHERE status = 1 AND hotel_id = :hid ORDER BY id DESC");
    $stmt->execute([':hid' => $hotel_id]);
} else {
    $stmt = $conn->query("SELECT * FROM rooms WHERE status = 1 ORDER BY id DESC");
    $stmt->execute();
}
$rooms = $stmt->fetchAll(PDO::FETCH_OBJ);

// Get hotel name if filtering
$hotel = null;
if($hotel_id){
    $hs = $conn->prepare("SELECT * FROM hotels WHERE id = :id");
    $hs->execute([':id' => $hotel_id]);
    $hotel = $hs->fetch(PDO::FETCH_OBJ);
}
?>

    <div class="hero-wrap js-fullheight" style="background-image: url('<?php echo APPURL; ?>/images/hotel-bg.jpg');" data-stellar-background-ratio="0.5">
      <div class="overlay"></div>
      <div class="container">
        <div class="row no-gutters slider-text js-fullheight align-items-center justify-content-start" data-scrollax-parent="true">
          <div class="col-md-7 ftco-animate">
            <h2 class="subheading">Welcome to Vacation Rental</h2>
            <h1 class="mb-4"><?php echo $hotel ? htmlspecialchars($hotel->name) . ' Rooms' : 'All Apartment Rooms'; ?></h1>
          </div>
        </div>
      </div>
    </div>

    <section class="ftco-section bg-light">
      <div class="container-fluid px-md-0">
        <div class="row no-gutters justify-content-center pb-5 mb-3">
          <div class="col-md-7 heading-section text-center ftco-animate">
            <h2><?php echo $hotel ? htmlspecialchars($hotel->name) . ' â€” Available Rooms' : 'All Available Rooms'; ?></h2>
            <?php if($hotel): ?>
              <p><?php echo htmlspecialchars($hotel->description ?? ''); ?></p>
              <p>Location: <strong><?php echo htmlspecialchars($hotel->location ?? ''); ?></strong></p>
            <?php endif; ?>
          </div>
        </div>

        <?php if(count($rooms) === 0): ?>
          <div class="container">
            <div class="alert alert-info text-center">No rooms available at this time. <a href="<?php echo APPURL; ?>">Go back home</a></div>
          </div>
        <?php else: ?>
        <div class="row no-gutters">
          <?php foreach($rooms as $room): ?>
          <div class="col-lg-6">
            <div class="room-wrap d-md-flex">
              <a href="<?php echo APPURL; ?>/rooms/room-single.php?id=<?php echo $room->id; ?>" class="img" style="background-image: url(<?php echo APPURL; ?>/images/<?php echo $room->image; ?>);"></a>
              <div class="half left-arrow d-flex align-items-center">
                <div class="text p-4 p-xl-5 text-center">
                  <p class="star mb-0">
                    <span class="fa fa-star"></span><span class="fa fa-star"></span>
                    <span class="fa fa-star"></span><span class="fa fa-star"></span>
                    <span class="fa fa-star"></span>
                  </p>
                  <p class="mb-0"><span class="price mr-1">GH&#8373;<?php echo number_format($room->price ?? 0, 2); ?></span> <span class="per">per night</span></p>
                  <h3 class="mb-3"><a href="<?php echo APPURL; ?>/rooms/room-single.php?id=<?php echo $room->id; ?>" class="room"><?php echo htmlspecialchars($room->name); ?></a></h3>
                  <ul class="list-accomodation">
                    <li><span>Max:</span> <?php echo htmlspecialchars($room->num_persons); ?> Persons</li>
                    <li><span>Size:</span> <?php echo htmlspecialchars($room->size); ?> m2</li>
                    <li><span>View:</span> <?php echo htmlspecialchars($room->view); ?> View</li>
                    <li><span>Bed:</span> <?php echo htmlspecialchars($room->num_beds); ?></li>
                  </ul>
                  <p class="pt-1"><a href="<?php echo APPURL; ?>/rooms/room-single.php?id=<?php echo $room->id; ?>" class="btn-custom px-3 py-2">View Room Details <span class="icon-long-arrow-right"></span></a></p>
                </div>
              </div>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </div>
    </section>

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

