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

$stays = $conn->query("
SELECT
s.id,
g.name AS guest_name,
r.room_number,
r.room_type,
s.checkin_date,
s.days,
r.price

FROM stays s

INNER JOIN guests g
ON s.guest_id=g.id

INNER JOIN rooms r
ON s.room_id=r.id

WHERE s.checkout_date IS NULL

ORDER BY s.checkin_date DESC
");
?>

<div class="container mt-4">

<h4>

<i class="fa fa-bed"></i>

Generate Current Stay Invoice

</h4>

<hr>

<form method="POST"
action="bulk_invoice.php"
target="_blank">

<div class="mb-3">

<button class="btn btn-success">

<i class="fa fa-file-invoice"></i>

Generate Invoice

</button>

</div>

<div class="table-responsive">

<table class="table table-bordered table-striped">

<thead class="table-dark">

<tr>

<th>

<input type="checkbox" id="selectAll">

</th>

<th>Guest</th>

<th>Room</th>

<th>Type</th>

<th>Check In</th>

<th>Days</th>

<th>Room Cost</th>

</tr>

</thead>

<tbody>

<?php
while($row=$stays->fetch_assoc()):

$room_cost=$row['price']*$row['days'];
?>

<tr>

<td>

<input
type="checkbox"
name="stay_ids[]"
value="<?= $row['id'] ?>">

</td>

<td><?= htmlspecialchars($row['guest_name']) ?></td>

<td><?= $row['room_number'] ?></td>

<td><?= $row['room_type'] ?></td>

<td><?= $row['checkin_date'] ?></td>

<td><?= $row['days'] ?></td>

<td><?= number_format($room_cost,2) ?></td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</form>

</div>

<script>

document.getElementById("selectAll").addEventListener("change",function(){

document.querySelectorAll("input[name='stay_ids[]']")
.forEach(cb=>cb.checked=this.checked);

});

document.querySelector("form").addEventListener("submit",function(e){

let checked=document.querySelectorAll("input[name='stay_ids[]']:checked");

if(checked.length==0){

alert("Please select at least one guest.");

e.preventDefault();

}

});

</script>

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