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

// Get guest invoice ID
$invoice_id = $_GET['guest_id'] ?? 0;
$invoice_id = (int)$invoice_id;

// Fetch invoice info with guest
$invoice_res = $conn->query("
    SELECT i.*, g.name, g.phone
    FROM invoices i
    JOIN guests g ON i.guest_id = g.id
    WHERE i.id = $invoice_id
");
if($invoice_res->num_rows == 0){
    die("Invoice not found.");
}
$invoice = $invoice_res->fetch_assoc();

// Fetch payments made for this guest
$payments_res = $conn->query("
    SELECT amount, payment_date, type
    FROM payments
    WHERE guest_id = ".$invoice['guest_id']."
    ORDER BY payment_date ASC
");

// Fetch invoice items/services (if you have a table for breakdown)
$items_res = $conn->query("
    SELECT description, quantity, price, (quantity*price) AS total
    FROM invoice_items
    WHERE invoice_id = $invoice_id
");

$total_paid = 0;
while($p = $payments_res->fetch_assoc()){
    $total_paid += (float)$p['amount'];
}
$balance = (float)$invoice['total_amount'] - $total_paid;
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Receipt #<?= $invoice['id'] ?></title>
    <style>
        body {
            font-family: monospace;
            font-size: 12px;
            width: 300px;
            margin: 0 auto;
        }
        .center { text-align: center; }
        .right { text-align: right; }
        hr { border-top: 1px dashed #000; }
        table { width: 100%; border-collapse: collapse; }
        td, th { padding: 2px; }
        th { text-align: left; }
        .bold { font-weight: bold; }
    </style>
</head>
<body onload="window.print();">

<div class="center">
    <h2>🏨 RAMS EXECUTITVE LODGE</h2>
     <p class="center">
        Web:www.ramslodgemw.com<br>
        Phone:+265 993 899 382<br>
         Phone:+265 883 899 382<br>
       >
    </p>
</div>

<hr>
<p><span class="bold">Guest:</span> <?= htmlspecialchars($invoice['name']) ?></p>
<p><span class="bold">Phone:</span> <?= htmlspecialchars($invoice['phone']) ?></p>
<p><span class="bold">Invoice #:</span> <?= $invoice['id'] ?> <span class="right"><span class="bold">Date:</span> <?= date('Y-m-d H:i', strtotime($invoice['created_at'])) ?></span></p>
<hr>

<table>
    <thead>
        <tr>
            <th>Item</th>
            <th class="right">Total</th>
        </tr>
    </thead>
    <tbody>
    <?php if($items_res->num_rows > 0): ?>
        <?php while($item = $items_res->fetch_assoc()): ?>
        <tr>
            <td><?= htmlspecialchars($item['description']) ?> x<?= $item['quantity'] ?></td>
            <td class="right"><?= number_format($item['total'],2) ?></td>
        </tr>
        <?php endwhile; ?>
    <?php else: ?>
        <tr>
            <td colspan="2" class="center">No items found</td>
        </tr>
    <?php endif; ?>
    </tbody>
</table>

<hr>
<p><span class="bold">Total:</span> <?= number_format($invoice['total_amount'],2) ?> MK</p>
<p><span class="bold">Paid:</span> <?= number_format($total_paid,2) ?> MK</p>
<p><span class="bold">Balance:</span> <?= number_format($balance,2) ?> MK</p>
<hr>

<?php if($payments_res->num_rows > 0): ?>
<p class="bold">Payments:</p>
<table>
    <?php
    // Reset payments result pointer
    $payments_res->data_seek(0);
    while($p = $payments_res->fetch_assoc()): ?>
    <tr>
        <td><?= date('Y-m-d H:i', strtotime($p['payment_date'])) ?> (<?= $p['type'] ?>)</td>
        <td class="right"><?= number_format($p['amount'],2) ?></td>
    </tr>
    <?php endwhile; ?>
</table>
<hr>
<?php endif; ?>

<p class="center">Thank you for your stay!</p>

</body>
</html>
