<?php
session_start();

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

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

$basePath = '../../';
include '../../includes/header.php';

$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) ?: [];

/*
    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);
?>

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Invoice #<?= $order['id'] ?></h3>

        <div>
            <a href="print_invoice.php?id=<?= $order['id'] ?>" target="_blank" class="btn btn-primary">
                Print
            </a>

            <a href="export_pdf.php?id=<?= $order['id'] ?>" target="_blank" class="btn btn-danger">
                PDF
            </a>

            <a href="../reports/daily_sales.php" class="btn btn-secondary">
                Back
            </a>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

            <div class="text-center mb-4">
                <h3><?= 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="row mb-3">
                <div class="col-md-6">
                    <strong>Invoice No:</strong> #<?= $order['id'] ?>
                </div>

                <div class="col-md-6 text-end">
                    <strong>Date:</strong>
                    <?= date('d-m-Y h:i A', strtotime($order['order_date'])) ?>
                </div>
            </div>

            <div class="row mb-4">
                <div class="col-md-6">
                    <strong>Table:</strong>
                    <?= htmlspecialchars($order['table_name'] ?? 'N/A') ?>
                </div>

                <div class="col-md-6 text-end">
                    <strong>Status:</strong>
                    <?= htmlspecialchars($order['status']) ?>
                </div>
            </div>

            <table class="table table-bordered">
                <thead class="table-dark">
                    <tr>
                        <th>Item</th>
                        <th>Qty</th>
                        <th>Rate</th>
                        <th>GST</th>
                        <th>Total</th>
                    </tr>
                </thead>

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

                    <tr>
                        <td><?= htmlspecialchars($item['item_name']) ?></td>
                        <td><?= number_format($item['quantity'], 2) ?></td>
                        <td>₹ <?= number_format($item['price'], 2) ?></td>
                        <td>₹ <?= number_format($item['gst'], 2) ?></td>
                        <td>₹ <?= number_format($lineTotal, 2) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>

            <div class="row mt-4">
                <div class="col-md-6"></div>

                <div class="col-md-6">
                    <table class="table table-bordered">
                        <tr>
                            <th>Subtotal</th>
                            <td>₹ <?= number_format($order['total'] - $order['gst'], 2) ?></td>
                        </tr>

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

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

        </div>
    </div>

</div>

<?php include '../../includes/footer.php'; ?>