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

// ================= FETCH REVERSED CHECKOUTS =================
// Assuming you track reversals in `stays` by checking checkout_date=NULL and days=0
$reversed_checkouts = $conn->query("
    SELECT s.id AS stay_id,
           g.name AS guest_name,
           r.room_number,
           r.room_type,
           s.checkin_date,
           s.updated_at AS reversal_date
    FROM stays s
    JOIN guests g ON s.guest_id = g.id
    JOIN rooms r ON s.room_id = r.id
    WHERE s.checkout_date IS NULL AND s.days = 0
    ORDER BY s.updated_at DESC
");
?>

<div class="container mt-4">
    <button class="btn btn-secondary btn-custom" onclick="history.back()">
<i class="fa fa-arrow-left"></i> Back
</button>

    <h4><i class="fa fa-undo"></i> Reversed Checkouts</h4>
    <hr>

    <!-- Table -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-sm" id="reversedCheckoutTable">
            <thead class="bg-secondary text-white sticky-top">
                <tr>
                    <th>#</th>
                    <th>Guest</th>
                    <th>Room</th>
                    <th>Room Type</th>
                    <th>Original Check-In</th>
                    <th>Reversal Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $i = 1;
                while($s = $reversed_checkouts->fetch_assoc()):
                ?>
                <tr class="table-warning">
                    <td><?= $i ?></td>
                    <td><?= htmlspecialchars($s['guest_name']) ?></td>
                    <td><?= $s['room_number'] ?></td>
                    <td><?= $s['room_type'] ?></td>
                    <td><?= date('Y-m-d', strtotime($s['checkin_date'])) ?></td>
                    <td><?= date('Y-m-d H:i', strtotime($s['reversal_date'])) ?></td>
                    <td>
                        <a href="restore_checkout.php?stay_id=<?= $s['stay_id'] ?>" class="btn btn-success btn-sm" title="Restore Checkout" onclick="return confirm('Are you sure you want to restore this checkout?');">
                            <i class="fa fa-undo-alt"></i> Restore
                        </a>
                        <a href="invoice.php?stay_id=<?= $s['stay_id'] ?>" class="btn btn-dark btn-sm" title="View Invoice">
                            <i class="fa fa-file-invoice-dollar"></i>
                        </a>
                    </td>
                </tr>
                <?php $i++; endwhile; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
    // Optional: Add search/filter
    function filterReversedTable() {
        let input = document.getElementById("reversedSearch").value.toLowerCase();
        let table = document.getElementById("reversedCheckoutTable");
        let tr = table.getElementsByTagName("tr");
        for (let i=1; i<tr.length; i++) {
            let tds = tr[i].getElementsByTagName("td");
            let show = false;
            for(let j=1; j<5; j++){
                if(tds[j] && tds[j].textContent.toLowerCase().includes(input)){
                    show = true;
                    break;
                }
            }
            tr[i].style.display = show ? "" : "none";
        }
    }
</script>

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