<?php
/**
 * @var object $invoice
 * @var array $items
 * @var array $payments
 */
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Invoice #<?= e($invoice->invoice_number) ?></title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Courier New', monospace; font-size: 12px; line-height: 1.5; color: #000; background: #fff; padding: 20px; }
        .no-print { display: none; }
        .header { text-align: center; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #000; }
        .header h2 { font-size: 18px; margin-bottom: 3px; }
        .header p { font-size: 11px; margin: 1px 0; }
        .meta { display: flex; justify-content: space-between; margin-bottom: 10px; flex-wrap: wrap; }
        .meta .block { width: 48%; }
        .meta .block .label { font-weight: bold; font-size: 10px; text-transform: uppercase; }
        .meta .block .value { font-size: 12px; }
        table { width: 100%; border-collapse: collapse; margin: 10px 0; }
        th { border-bottom: 2px solid #000; padding: 5px 4px; text-align: left; font-size: 10px; text-transform: uppercase; }
        td { border-bottom: 1px solid #ccc; padding: 4px; font-size: 11px; }
        th:nth-child(3), td:nth-child(3) { text-align: center; }
        th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5) { text-align: right; }
        .totals { margin-left: auto; width: 250px; }
        .total-row { display: flex; justify-content: space-between; padding: 2px 0; font-size: 11px; }
        .total-row.grand { font-size: 14px; font-weight: bold; border-top: 2px solid #000; margin-top: 5px; padding-top: 5px; }
        .payments { margin-top: 10px; padding-top: 5px; border-top: 1px solid #000; }
        .payments table { width: 100%; }
        .note { margin-top: 10px; font-style: italic; font-size: 10px; }
        .footer { text-align: center; margin-top: 20px; padding-top: 10px; border-top: 1px solid #000; font-size: 10px; }
        @media print {
            body { padding: 0; }
            @page { margin: 10mm; }
        }
    </style>
</head>
<body>
    <div class="header">
        <h2><?= e(setting('app_name', APP_NAME)) ?></h2>
        <p><?= e(setting('app_address', '')) ?></p>
        <p>Tel: <?= e(setting('app_phone', '')) ?> | Email: <?= e(setting('app_email', '')) ?></p>
    </div>

    <div class="meta">
        <div class="block">
            <div class="label">Invoice #</div>
            <div class="value"><?= e($invoice->invoice_number) ?></div>
        </div>
        <div class="block" style="text-align:right;">
            <div class="label">Date</div>
            <div class="value"><?= formatDate($invoice->created_at) ?></div>
        </div>
    </div>

    <div class="meta">
        <div class="block">
            <div class="label">Bill To</div>
            <div class="value">
                <?php if ($invoice->customer_id): ?>
                    <?= e($invoice->customer_first_name . ' ' . $invoice->customer_last_name) ?><br>
                    <?= e($invoice->customer_phone ?? '') ?><br>
                    <?= e($invoice->customer_email ?? '') ?>
                <?php else: ?>
                    Walk-in Customer
                <?php endif; ?>
            </div>
        </div>
        <div class="block" style="text-align:right;">
            <div class="label">Cashier</div>
            <div class="value"><?= e($invoice->user_first_name . ' ' . $invoice->user_last_name) ?></div>
            <div class="label" style="margin-top:5px;">Status</div>
            <div class="value"><?= e(ucfirst($invoice->payment_status)) ?></div>
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Item</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 1; foreach ($items as $item): ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><?= e($item->item_name) ?></td>
                    <td><?= (int)$item->quantity ?></td>
                    <td><?= currency($item->price) ?></td>
                    <td><?= currency($item->total) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="totals">
        <div class="total-row">
            <span>Subtotal</span>
            <span><?= currency($invoice->subtotal) ?></span>
        </div>
        <?php if ((float)$invoice->discount_total > 0): ?>
            <div class="total-row">
                <span>Discount</span>
                <span>-<?= currency($invoice->discount_total) ?></span>
            </div>
        <?php endif; ?>
        <div class="total-row">
            <span>Tax</span>
            <span><?= currency($invoice->tax_total) ?></span>
        </div>
        <div class="total-row grand">
            <span>Grand Total</span>
            <span><?= currency($invoice->total) ?></span>
        </div>
        <div class="total-row">
            <span>Paid</span>
            <span><?= currency($invoice->paid_amount) ?></span>
        </div>
        <?php $change = (float)$invoice->paid_amount - (float)$invoice->grand_total; ?>
        <?php if ($change > 0): ?>
            <div class="total-row">
                <span>Change Due</span>
                <span><?= currency($change) ?></span>
            </div>
        <?php elseif ((float)$invoice->balance_amount > 0): ?>
            <div class="total-row">
                <span>Balance Due</span>
                <span><?= currency($invoice->balance_amount) ?></span>
            </div>
        <?php endif; ?>
    </div>

    <?php if (!empty($payments)): ?>
        <div class="payments">
            <table>
                <thead>
                    <tr>
                        <th>Payment Method</th>
                        <th style="text-align:right;">Amount</th>
                        <th>Reference</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($payments as $p): ?>
                        <tr>
                            <td><?= e(ucwords(str_replace('_', ' ', $p->payment_method))) ?></td>
                            <td style="text-align:right;"><?= currency($p->amount) ?></td>
                            <td><?= e($p->reference_number ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>

    <?php if (!empty($invoice->notes)): ?>
        <div class="note">Note: <?= e($invoice->notes) ?></div>
    <?php endif; ?>

    <div class="footer">
        <p>Thank you for your business!</p>
        <p><?= APP_NAME ?> | <?= APP_VERSION ?></p>
        <p style="font-size:9px;margin-top:5px;">Printed by <?= e((\core\Auth::user()->first_name ?? '') . ' ' . (\core\Auth::user()->last_name ?? '')) ?> on <?= date('d/m/Y h:i A') ?></p>
    </div>

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