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

<?php
if(!isset($_SESSION['username'])){
    header("location: " . APPURL . "/auth/login.php");
    exit;
}

$user_id = $_SESSION['id'];

// Fetch all bookings for logged-in user
$stmt = $conn->prepare("SELECT * FROM bookings WHERE user_id = :uid ORDER BY id DESC");
$stmt->execute([':uid' => $user_id]);
$bookings = $stmt->fetchAll(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">Your Reservations</h2>
            <h1 class="mb-4">My Bookings</h1>
          </div>
        </div>
      </div>
    </div>

    <section class="ftco-section">
      <div class="container">
        <div class="row">
          <div class="col-12">
            <h3 class="mb-4">Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?></h3>

            <?php if(count($bookings) === 0): ?>
              <div class="alert alert-info">You have no bookings yet. <a href="<?php echo APPURL; ?>">Browse rooms</a></div>
            <?php else: ?>
            <div class="table-responsive">
              <table class="table table-bordered table-hover">
                <thead class="thead-dark">
                  <tr>
                    <th>#</th>
                    <th>Hotel</th>
                    <th>Room</th>
                    <th>Check-In</th>
                    <th>Check-Out</th>
                    <th>Amount</th>
                    <th>Payment</th>
                    <th>Transaction Ref</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach($bookings as $b): ?>
                  <tr>
                    <td><?php echo $b->id; ?></td>
                    <td><?php echo htmlspecialchars($b->hotel_name); ?></td>
                    <td><?php echo htmlspecialchars($b->room_name); ?></td>
                    <td><?php echo htmlspecialchars($b->check_in); ?></td>
                    <td><?php echo htmlspecialchars($b->check_out); ?></td>
                    <td>GH&#8373;<?php echo number_format($b->price, 2); ?></td>
                    <td>
                      <?php if($b->payment_status === 'paid'): ?>
                        <span class="badge badge-success" style="background:#28a745;color:#fff;padding:5px 8px;">PAID</span>
                      <?php else: ?>
                        <span class="badge badge-warning" style="background:#ffc107;color:#000;padding:5px 8px;">PENDING</span>
                      <?php endif; ?>
                    </td>
                    <td><?php echo $b->transaction_ref ? htmlspecialchars($b->transaction_ref) : '<em>N/A</em>'; ?></td>
                    <td>
                      <?php if($b->payment_status !== 'paid'): ?>
                        <a href="<?php echo APPURL; ?>/bookings/pay.php?retry=<?php echo $b->id; ?>" class="btn btn-sm btn-primary">Pay Now</a>
                      <?php else: ?>
                        <span class="text-success"><i class="fa fa-check"></i> Done</span>
                      <?php endif; ?>
                    </td>
                  </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
            </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </section>

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

