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

$sale_id = intval($_GET['sale_id'] ?? 0);
if(!$sale_id){
    exit("Invalid sale ID");
}

// Fetch sale info
$sale = $conn->query("SELECT * FROM sales WHERE id=$sale_id")->fetch_assoc();
if(!$sale){
    exit("Sale not found");
}

// ================= TAX (INCLUSIVE) =================
$total_incl = floatval($sale['total']); // already inclusive

$total_tax_rate = 0.175 + 0.01; // 18.5%

$taxable_base = $total_incl / (1 + $total_tax_rate);

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

// Fetch sale items
$items = $conn->query("
    SELECT si.*, p.name, p.sku 
    FROM sale_items si
    JOIN products p ON si.product_id = p.id
    WHERE si.sale_id=$sale_id
");

// Logo
$logo = "assets/logo.png";
?>

<!DOCTYPE html>
<html>
<head>
    <title>Receipt #<?= $sale_id ?></title>
    <style>
        body { font-family: monospace; width: 80mm; margin: 0; padding: 0; }
        .center { text-align: center; }
        .line { border-bottom: 1px dashed #000; margin: 5px 0; }
        table { width: 100%; border-collapse: collapse; font-size:12px; }
        td, th { padding: 3px 0; }
        .right { text-align: right; }
        @media print { button { display:none; } }
    </style>
</head>
<body onload="window.print()">

<div class="center">
    <img src="<?= $logo ?>" style="width:60px;"><br>
    <h5>Receipt #<?= $sale_id ?></h5>
    <p>
        Web: www.ramslodgemw.com<br>
        Phone: +265 993 899 382<br>
        Phone: +265 883 899 382
    </p>
    <p><?= $sale['sale_date'] ?></p>
    <div class="line"></div>
</div>

<table>
    <thead>
        <tr>
            <th>Item</th>
            <th>Qty</th>
            <th class="right">Price</th>
            <th class="right">Total</th>
        </tr>
    </thead>
    <tbody>
        <?php
        while($item = $items->fetch_assoc()){
            $total = $item['price'] * $item['qty']; // FIXED (no VAT added again)
            echo "<tr>
                    <td>{$item['name']}</td>
                    <td>{$item['qty']}</td>
                    <td class='right'>".number_format($item['price'],2)."</td>
                    <td class='right'>".number_format($total,2)."</td>
                  </tr>";
        }
        ?>
    </tbody>
</table>

<div class="line"></div>

<table>
    <tr>
        <td>Subtotal (Incl):</td>
        <td class="right"><?= number_format($total_incl,2) ?></td>
    </tr>
    <tr>
        <td>VAT (17.5%):</td>
        <td class="right"><?= number_format($vat,2) ?></td>
    </tr>
    <tr>
        <td>Tourism Levy (1%):</td>
        <td class="right"><?= number_format($tourism,2) ?></td>
    </tr>
    <tr>
        <td>Discount:</td>
        <td class="right"><?= number_format($sale['discount'],2) ?></td>
    </tr>
    <tr>
        <td><strong>Total:</strong></td>
        <td class="right"><strong><?= number_format($total_incl,2) ?></strong></td>
    </tr>
    <tr>
        <td>Payment Method:</td>
        <td><?= htmlspecialchars($sale['payment_method']) ?></td>
    </tr>
</table>

<div class="center">
    <p>* Prices are Tax Inclusive *</p>
    <p>Thank you for your purchase!</p>
    <p><b>e-HOTELS</b>&nbsp;Powered by Giant+</p>
</div>

<button onclick="window.print()">🖨 Print</button>

</body>
</html>