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

$stay_id = intval($_GET['stay_id'] ?? 0);
$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");

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

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

// Calculate total charges
$service_total = 0;
$charges = [];
if($charges_res){
    while($row = $charges_res->fetch_assoc()){
        $charges[] = $row;
        $service_total += floatval($row['amount']) * floatval($row['quantity']); // Multiply by quantity
    }
}

// ================= TAXES =================
// ================= TAXES (INCLUSIVE) =================
$subtotal = $room_cost + $service_total;

// Total rate = VAT + Tourism
$total_tax_rate = 0.175 + 0.01; // 18.5%

// Extract taxes from inclusive amount
$taxable_base = $subtotal / (1 + $total_tax_rate);

$vat = $taxable_base * 0.175;
$tourism = $taxable_base * 0.01;

// TOTAL stays the SAME (already inclusive)
$total_amount = $subtotal;
?>

<!DOCTYPE html>
<html>
<head>
<style>
body{
    font-family: monospace;
    width: 300px;
}

.header{
    text-align:center;
}

.logo{
    max-width:120px;
    margin-bottom:5px;
}

.phone{
    font-size:12px;
    margin-bottom:5px;
    line-height:1.4;
}

h4{
    margin:3px 0;
}

table{
    width:100%;
    border-collapse:collapse;
}

td, th{
    padding:3px;
    border-bottom:1px dotted #000;
}

th{
    text-align:left;
}

.total-row td{
    border-top:2px solid #000;
    font-weight:bold;
}

/* PRINT BUTTON */
.print-btn{
    width:100%;
    padding:8px;
    margin-bottom:10px;
    background:#28a745;
    color:#fff;
    border:none;
    cursor:pointer;
    font-weight:bold;
}

/* Hide button when printing */
@media print{
    .print-btn{
        display:none;
    }
}
</style>
</head>
<body>
<button class="print-btn"
        onclick="window.location.href='receipt_pdf.php?stay_id=<?= $stay_id ?>'">
    📄 Download PDF
</button>

<button class="print-btn" onclick="window.print()">🖨️ Print Receipt</button>

<div class="header">
    <img src="assets/logo.png" class="logo" alt="Hotel Logo">
    <h4>Hotel Receipt</h4>
    <div class="phone">
        Phone: 0993 899 382<br>
        Cell: 0883 899 382
      <br/>
        Website:www.ramslodgemw.com
    
       
    </p>
    </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>

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

<tr>
    <td>VAT (17.5%)</td>
    <td style="text-align:right;"><?= number_format($vat,2) ?></td>
</tr>

<tr>
    <td>Tourism Levy (1%)</td>
    <td style="text-align:right;"><?= number_format($tourism,2) ?></td>
</tr>

<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>
