<?php
include 'includes/auth.php';
include 'db.php';
require_once 'dompdf/vendor/autoload.php'; // DOMPDF

use Dompdf\Dompdf;

// Get stay ID
$stay_id = intval($_GET['stay_id'] ?? 0);
if(!$stay_id) exit("Invalid stay ID.");

// Fetch stay, guest, room
$stay = $conn->query("
    SELECT s.*, g.name AS guest_name, r.room_number, r.room_type, r.price AS room_price
    FROM stays s
    JOIN guests g ON s.guest_id=g.id
    JOIN rooms r ON s.room_id=r.id
    WHERE s.id=$stay_id
")->fetch_assoc();

if(!$stay) exit("Stay not found.");

// Room cost
$room_cost = $stay['room_price'] * $stay['days'];

// Fetch charges
$charges_res = $conn->query("
    SELECT id, name, quantity, amount 
    FROM charges 
    WHERE guest_id={$stay['guest_id']}
    ORDER BY id ASC
");

$service_total = 0;
$charges = [];
if($charges_res){
    while($row = $charges_res->fetch_assoc()){
        $charges[] = $row;
        $service_total += floatval($row['amount']);
    }
}

$total_amount = $room_cost + $service_total;

// =================== FIX LOGO PATH ===================
// Absolute path to logo file on server
$logo_path = realpath(__DIR__ . '/assets/logo.png');
if ($logo_path && file_exists($logo_path)) {
    $logo_path = 'file://' . $logo_path; // DOMPDF requires file:// prefix
} else {
    $logo_path = ''; // fallback: no logo
}

// =================== HTML FOR PDF ===================
$html = '
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: monospace; font-size:12px; width:100%; }
.header { text-align:center; }
.logo { max-width:150px; margin-bottom:5px; }
.phone { font-size:12px; line-height:1.4; margin-bottom:5px; }
h4 { margin:3px 0; }
table { width:100%; border-collapse:collapse; }
td, th { padding:5px; border-bottom:1px dotted #000; }
th { text-align:left; }
.total-row td { border-top:2px solid #000; font-weight:bold; }
</style>
</head>
<body>

<div class="header">';
if($logo_path){
    $html .= '<img src="'.$logo_path.'" class="logo" alt="Hotel Logo">';
}
$html .= '
<h4>Hotel Receipt</h4>
<div class="phone">
Phone: 0993 899 382<br>
Cell: 0883 899 382
</div>
</div>

<hr>

<p>Guest: '.htmlspecialchars($stay['guest_name']).'</p>
<p>Room: '.$stay['room_number'].' ('.$stay['room_type'].')</p>
<p>Check-In: '.$stay['checkin_date'].'</p>
<p>Days: '.$stay['days'].'</p>

<hr>

<table>
<tr>
<th>Description</th>
<th style="text-align:right;">Amount (MK)</th>
</tr>

<tr>
<td>Room ('.$stay['days'].' day'.($stay['days']>1?'s':'').')</td>
<td style="text-align:right;">'.number_format($room_cost,2).'</td>
</tr>';

foreach($charges as $c){
    $html .= '<tr>
        <td>'.htmlspecialchars($c['name']).' '.($c['quantity']>1?"(x{$c['quantity']})":"").'</td>
        <td style="text-align:right;">'.number_format($c['amount'],2).'</td>
    </tr>';
}

$html .= '<tr class="total-row">
<td>Total</td>
<td style="text-align:right;">'.number_format($total_amount,2).'</td>
</tr>
</table>

<hr>
<p style="text-align:center;">Thank you for staying with us!</p>

</body>
</html>
';

// =================== GENERATE PDF ===================
$dompdf = new Dompdf();
$dompdf->loadHtml($html);

// Set paper to A4 portrait; for thermal receipts, you can set custom size
$dompdf->setPaper('A4', 'portrait');

$dompdf->render();

// Download PDF
$dompdf->stream("receipt_{$stay_id}.pdf", ["Attachment" => true]);
exit;
?>
