<?php

require 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

include 'db.php';

$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);

$html='
<h2 style="text-align:center">Invoices Report</h2>

<table border="1" width="100%" cellspacing="0" cellpadding="5">

<tr>
<th>Invoice</th>
<th>Guest</th>
<th>Total</th>
<th>Status</th>
</tr>
';

while($row=$result->fetch_assoc()){

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

}

$html.='</table>';

$dompdf = new Dompdf();

$dompdf->loadHtml($html);

$dompdf->setPaper('A4','portrait');

$dompdf->render();

$dompdf->stream("invoices.pdf");

?>