<?php
include 'db.php';




$sale_id = intval($_GET['id']);

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

// Check print count
if($sale['print_count'] >= 2){
    die("This receipt has already been printed twice.");
}

// Increment print count
$conn->query("UPDATE sales SET print_count = print_count + 1 WHERE id=$sale_id");

// Generate receipt HTML/PDF here



// Helper to format numbers safely
function fmt($num) {
    return number_format($num ?? 0, 2);
}

// Check if sale ID is provided
$sale_id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;
if ($sale_id <= 0) die("Error: Sale ID not provided or invalid.");

// Fetch sale record
$sale_q = $conn->query("
    SELECT s.*, c.name AS customer_name
    FROM sales s
    LEFT JOIN customers c ON s.customer_id=c.id
    WHERE s.id=$sale_id
");
if ($sale_q->num_rows == 0) die("Error: Sale not found.");
$sale = $sale_q->fetch_assoc();

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

$total_tax_rate = 0.175 + 0.01; // 18.5%

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

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

// Fetch sale items
$items_q = $conn->query("
    SELECT si.*, m.name AS menu_name
    FROM sale_items si
    LEFT JOIN restaurant_menu m ON si.menu_id = m.id
    WHERE si.sale_id=$sale_id
");

// Logo path
$logo = 'assets/logo.png';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Sale #<?= $sale['id'] ?> Receipt</title>
    <style>
        /* ====== Thermal printer style ====== */
        body {
            font-family: monospace;
            font-size: 12px;
            width: 72mm; /* adjust 58mm or 80mm depending on printer */
            margin: 0;
            padding: 0;
        }
        .center { text-align: center; }
        hr { border: none; border-top: 1px dashed #000; margin: 3px 0; }
        table { width: 100%; border-collapse: collapse; }
        td { padding: 1px 0; }
        td.right { text-align: right; }
        .total { font-weight: bold; font-size: 14px; }
        .logo { width: 60px; margin: 0 auto; display: block; }
        @media print {
            body { width: auto; margin: 0; padding: 0; }
            button { display: none; }
        }
        button.print-btn {
            display: block;
            width: 100%;
            padding: 6px;
            margin: 5px 0;
            background: #28a745;
            color: #fff;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <img src="<?= $logo ?>" class="logo">
    <h3 class="center">🍽 HAVEN DINE RESTAURANT</h3>
        <p class="center">
        Web:www.ramslodgemw.com<br>
        Phone:+265 993 899 382<br>
          Phone:+265 883 899 382<br>
       
    </p>
    <p class="center">
        Sale #: <?= $sale['id'] ?><br>
        Customer: <?= $sale['customer_name'] ?? 'Walk-in' ?><br>
        Date: <?= date('d-m-Y H:i', strtotime($sale['sale_date'])) ?><br>
        Payment: <?= $sale['payment_method'] ?? 'N/A' ?>
    </p>
    <hr>
    <table>
        <?php while($item = $items_q->fetch_assoc()): ?>
        <tr>
            <td><?= $item['qty'] ?> x <?= $item['menu_name'] ?? 'Item '.$item['menu_id'] ?></td>
            <td class="right"><?= fmt($item['price'] * $item['qty']) ?></td>
        </tr>
        <?php endwhile; ?>
    </table>
    <hr>
   <p>
    Subtotal (Incl): <?= fmt($subtotal) ?><br>
    VAT (17.5%): <?= fmt($vat) ?><br>
    Tourism Levy (1%): <?= fmt($tourism) ?><br>
    Discount: <?= fmt($sale['discount']) ?><br>
    <span class="total">Total: <?= fmt($subtotal) ?></span>
</p>
<p class="center">* Prices are Tax Inclusive *</p>
    <p class="center">Thank you for dining with us!</p>
    <button class="print-btn" onclick="window.print()">🖨 Print Receipt</button>
</body>
</html>
