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

// Fetch guest history from stays
$history = $conn->query("
    SELECT 
        g.name, 
         
        r.room_number, 
        s.checkin_date, 
        s.checkout_date
    FROM stays s
    JOIN guests g ON s.guest_id = g.id
    JOIN rooms r ON s.room_id = r.id
    ORDER BY s.checkin_date DESC
");
?>

<div class="container mt-4">
    <h4>Guest History</h4>
    <hr>

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>Guest</th>
                <th>Room</th>
                <th>Check-In</th>
                <th>Check-Out</th>
            </tr>
        </thead>
        <tbody>
            <?php while($h = $history->fetch_assoc()): ?>
            <tr>
                <td><?= htmlspecialchars($h['name']) ?></td>
                <td><?= htmlspecialchars($h['room_number']) ?></td>
                <td><?= $h['checkin_date'] ? date('Y-m-d H:i', strtotime($h['checkin_date'])) : '-' ?></td>
                <td><?= $h['checkout_date'] ? date('Y-m-d H:i', strtotime($h['checkout_date'])) : '-' ?></td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

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