<?php
session_start();

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

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

$orderId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($orderId <= 0) {
    die('Invalid order ID.');
}

/*
    SETTINGS
*/
$stmt = $db->prepare("
    SELECT *
    FROM settings
    LIMIT 1
");
$stmt->execute();

$settings = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];

$paperWidth = (int)($settings['paper_width'] ?? 80);

if ($paperWidth <= 0) {
    $paperWidth = 80;
}

/*
    ORDER
*/
$stmt = $db->prepare("
    SELECT
        o.*,
        '' as table_name
    FROM orders o
    WHERE o.id = ?
");
$stmt->execute([$orderId]);

$order = $stmt->fetch(PDO::FETCH_ASSOC);

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

/*
    ITEMS
*/
$stmt = $db->prepare("
    SELECT
        oi.*,
        i.name AS item_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);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Print Invoice</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            width: <?= $paperWidth ?>mm;
            margin: 0 auto;
            padding: 10px;
            font-size: 12px;
        }

        .center {
            text-align: center;
        }

        .divider {
            border-top: 1px dashed #000;
            margin: 8px 0;
        }

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

        th, td {
            padding: 4px 0;
            vertical-align: top;
        }

        .right {
            text-align: right;
        }

        .bold {
            font-weight: bold;
        }

        .no-print {
            text-align: center;
            margin-top: 15px;
        }

        @media print {
            .no-print {
                display: none;
            }

            body {
                margin: 0;
                padding: 0;
            }
        }
    </style>
</head>
<body>

<div class="center">
    <h3 style="margin:0;">
        <?= htmlspecialchars($settings['business_name'] ?? 'Restaurant') ?>
    </h3>

    <div>
        <?= htmlspecialchars($settings['address'] ?? '') ?>
    </div>

    <div>
        Phone: <?= htmlspecialchars($settings['phone'] ?? '') ?>
    </div>

    <div>
        Email: <?= htmlspecialchars($settings['email'] ?? '') ?>
    </div>
</div>

<div class="divider"></div>

<table>
    <tr>
        <td>Invoice:</td>
        <td class="right">#<?= $order['id'] ?></td>
    </tr>

    <tr>
        <td>Date:</td>
        <td class="right">
            <?= date('d-m-Y h:i A', strtotime($order['order_date'])) ?>
        </td>
    </tr>

    <tr>
        <td>Table:</td>
        <td class="right">
            <?= htmlspecialchars($order['table_name'] ?? 'N/A') ?>
        </td>
    </tr>

    <tr>
        <td>Status:</td>
        <td class="right">
            <?= htmlspecialchars($order['status']) ?>
        </td>
    </tr>
</table>

<div class="divider"></div>

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

    <tbody>
    <?php foreach ($items as $item): ?>
        <?php $lineTotal = $item['quantity'] * $item['price']; ?>

        <tr>
            <td><?= htmlspecialchars($item['item_name']) ?></td>

            <td class="right">
                <?= number_format($item['quantity'], 2) ?>
            </td>

            <td class="right">
                <?= number_format($item['price'], 2) ?>
            </td>

            <td class="right">
                <?= number_format($lineTotal, 2) ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<div class="divider"></div>

<table>
    <tr>
        <td>Subtotal</td>
        <td class="right">
            ₹ <?= number_format($order['total'] - $order['gst'], 2) ?>
        </td>
    </tr>

    <tr>
        <td>GST</td>
        <td class="right">
            ₹ <?= number_format($order['gst'], 2) ?>
        </td>
    </tr>

    <tr class="bold">
        <td>Grand Total</td>
        <td class="right">
            ₹ <?= number_format($order['total'], 2) ?>
        </td>
    </tr>
</table>

<div class="divider"></div>

<div class="center">
    Thank You! Visit Again
</div>

<div class="no-print">
    <button onclick="window.print()">Print</button>
</div>

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

</body>
</html>