<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

if (!isset($_SESSION['last_order_id'])) {
    exit('No order available for printing');
}

$db = (new Database())->connect();

$orderId = (int)$_SESSION['last_order_id'];

$stmt = $db->prepare("
    SELECT *
    FROM orders
    WHERE id = ?
");

$stmt->execute([$orderId]);
$order = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$order) {
    exit('Order not found');
}

$stmt = $db->prepare("
    SELECT
        oi.quantity,
        oi.price,
        oi.gst,
        i.name
    FROM order_items oi
    INNER JOIN items i ON oi.item_id = i.id
    WHERE oi.order_id = ?
");

$stmt->execute([$orderId]);
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);

$customerName = $_SESSION['last_customer_name'] ?? '';
$customerMobile = $_SESSION['last_customer_mobile'] ?? '';
$paymentMode = $_SESSION['last_payment_mode'] ?? 'Cash';
$discount = $_SESSION['last_discount'] ?? 0;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Print Bill</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            width: 300px;
            margin: auto;
        }

        h2, h4, p {
            text-align: center;
            margin: 5px 0;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        td, th {
            padding: 5px;
            border-bottom: 1px dashed #000;
            font-size: 12px;
        }

        .right {
            text-align: right;
        }

        .center {
            text-align: center;
        }

        @media print {
            button {
                display: none;
            }
        }
    </style>
</head>
<body>

<h2>Restaurant Billing</h2>
<p>Order #: <?= $orderId ?></p>
<p><?= date('d-m-Y h:i A', strtotime($order['order_date'])) ?></p>

<?php if (!empty($customerName)): ?>
<p>Customer: <?= htmlspecialchars($customerName) ?></p>
<?php endif; ?>

<?php if (!empty($customerMobile)): ?>
<p>Mobile: <?= htmlspecialchars($customerMobile) ?></p>
<?php endif; ?>

<hr>

<table>
    <thead>
        <tr>
            <th>Item</th>
            <th class="center">Qty</th>
            <th class="right">Amt</th>
        </tr>
    </thead>
    <tbody>

    <?php foreach ($items as $item): ?>
        <?php
        $amount = ($item['price'] * $item['quantity']);
        $gstAmount = ($amount * $item['gst']) / 100;
        $finalAmount = $amount + $gstAmount;
        ?>
        <tr>
            <td><?= htmlspecialchars($item['name']) ?></td>
            <td class="center"><?= $item['quantity'] ?></td>
            <td class="right"><?= number_format($finalAmount, 2) ?></td>
        </tr>
    <?php endforeach; ?>

    </tbody>
</table>

<hr>

<p>GST: ₹ <?= number_format($order['gst'], 2) ?></p>
<p>Discount: ₹ <?= number_format($discount, 2) ?></p>
<h4>Total: ₹ <?= number_format($order['total'], 2) ?></h4>
<p>Payment: <?= htmlspecialchars($paymentMode) ?></p>

<br>

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

<script>
window.onload = function() {
    window.print();
};
</script>

</body>
</html>