<?php
include 'db.php';

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=invoices.xls");

$status_filter = $_GET['status'] ?? '';

$where = [];
if($status_filter) $where[] = "i.status='".$conn->real_escape_string($status_filter)."'";

$where_sql = count($where) ? "WHERE ".implode(" AND ", $where) : "";

$query = "
SELECT i.invoice_number,g.name guest_name,i.total_amount,i.status
FROM invoices i
JOIN guests g ON i.guest_id=g.id
$where_sql
ORDER BY i.id DESC
";

$result = $conn->query($query);
?>

<table border="1">
<tr>
<th>Invoice</th>
<th>Guest</th>
<th>Total</th>
<th>Status</th>
</tr>

<?php while($row=$result->fetch_assoc()){ ?>

<tr>
<td><?= $row['invoice_number'] ?></td>
<td><?= $row['guest_name'] ?></td>
<td><?= number_format($row['total_amount'],2) ?></td>
<td><?= $row['status'] ?></td>
</tr>

<?php } ?>

</table>