<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Invoice #<?= e($sale['sale_no']) ?></title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Courier New', monospace; font-size: 12px; line-height: 1.3; color: #000; width: 80mm; margin: 0 auto; }
        .container { padding: 5mm; }
        .header { text-align: center; margin-bottom: 10px; border-bottom: 1px dashed #000; padding-bottom: 10px; }
        .header h1 { font-size: 16px; font-weight: bold; margin-bottom: 5px; }
        .header p { font-size: 10px; margin-bottom: 2px; }
        .invoice-info { text-align: center; margin-bottom: 10px; }
        .invoice-info p { margin-bottom: 2px; }
        .items-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
        .items-table th { text-align: left; border-bottom: 1px dashed #000; padding: 3px 0; font-weight: bold; }
        .items-table td { padding: 3px 0; border-bottom: 1px dotted #ccc; }
        .items-table .text-right { text-align: right; }
        .totals { margin-top: 10px; border-top: 1px dashed #000; padding-top: 5px; }
        .totals-row { display: flex; justify-content: space-between; margin-bottom: 3px; }
        .totals-row.grand-total { font-weight: bold; font-size: 14px; border-top: 1px dashed #000; padding-top: 5px; margin-top: 5px; }
        .footer { text-align: center; margin-top: 15px; padding-top: 10px; border-top: 1px dashed #000; font-size: 10px; }
        @media print {
            body { width: 80mm; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1><?= e($tenant['business_name'] ?? 'Your Company') ?></h1>
            <p><?= e($tenant['business_address'] ?? '') ?></p>
            <p><?= e($tenant['business_phone'] ?? '') ?></p>
        </div>
        
        <div class="invoice-info">
            <p><strong>INVOICE</strong></p>
            <p>#<?= e($sale['sale_no']) ?></p>
            <p><?= date('M j, Y g:i A', strtotime($sale['created_at'])) ?></p>
            <p><?= e($location['name'] ?? '') ?></p>
        </div>
        
        <?php if ($customer): ?>
        <div style="margin-bottom: 10px;">
            <p><strong>Customer:</strong> <?= e($customer['name']) ?></p>
        </div>
        <?php endif; ?>
        
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width: 45%;">Item</th>
                    <th style="width: 15%;">Qty</th>
                    <th style="width: 20%;">Price</th>
                    <th style="width: 20%;" class="text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sale['items'] as $item): ?>
                <tr>
                    <td><?= e($item['product_name']) ?></td>
                    <td><?= number_format($item['quantity'], 2) ?></td>
                    <td><?= number_format($item['unit_price'], 2) ?></td>
                    <td class="text-right"><?= number_format($item['quantity'] * $item['unit_price'] - ($item['discount'] ?? 0), 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <div class="totals">
            <div class="totals-row">
                <span>Subtotal:</span>
                <span><?= number_format($sale['subtotal'], 2) ?></span>
            </div>
            <?php if ($sale['discount_amount'] > 0): ?>
            <div class="totals-row">
                <span>Discount:</span>
                <span>-<?= number_format($sale['discount_amount'], 2) ?></span>
            </div>
            <?php endif; ?>
            <?php if ($sale['tax_amount'] > 0): ?>
            <div class="totals-row">
                <span>Tax:</span>
                <span><?= number_format($sale['tax_amount'], 2) ?></span>
            </div>
            <?php endif; ?>
            <div class="totals-row grand-total">
                <span>TOTAL:</span>
                <span><?= number_format($sale['total'], 2) ?></span>
            </div>
            <?php $paidAmount = $sale['paid_amount'] ?? $sale['amount_paid'] ?? 0; ?>
            <?php if ($paidAmount > 0): ?>
            <div class="totals-row">
                <span>Paid:</span>
                <span><?= number_format($paidAmount, 2) ?></span>
            </div>
            <?php endif; ?>
            <?php $dueAmount = $sale['total'] - $paidAmount; ?>
            <?php if ($dueAmount > 0): ?>
            <div class="totals-row" style="font-weight: bold;">
                <span>Due:</span>
                <span><?= number_format($dueAmount, 2) ?></span>
            </div>
            <?php endif; ?>
        </div>
        
        <div class="footer">
            <p>Payment: <?= strtoupper($sale['payment_method']) ?></p>
            <p>Status: <?= strtoupper($sale['payment_status']) ?></p>
            <p>Thank you for your business!</p>
        </div>
    </div>
    
    <script>
        window.onload = function() {
            window.print();
        };
    </script>
</body>
</html>
