<?php
/**
 * @var object $invoice
 * @var array $items
 * @var array $payments
 */
?>
<style>
.invoice-container { max-width: 800px; margin: 0 auto; }
.invoice-header { text-align: center; margin-bottom: 2rem; padding-bottom: 1.5rem; border-bottom: 2px solid var(--gold); }
.invoice-header .company-logo { max-height: 80px; margin-bottom: 0.75rem; }
.invoice-header h4 { color: var(--gold); font-weight: 700; margin-bottom: 0.25rem; }
.invoice-header p { margin-bottom: 0; font-size: 0.85rem; color: var(--text-muted); }
.invoice-meta { display: flex; justify-content: space-between; margin-bottom: 1.5rem; flex-wrap: wrap; gap: 1rem; }
.invoice-meta .meta-box { padding: 0.75rem 1rem; background: rgba(255,255,255,0.03); border-radius: 0.5rem; min-width: 200px; }
.invoice-meta .meta-box .label { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.invoice-meta .meta-box .value { font-size: 1rem; font-weight: 600; }
.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 1.5rem; }
.invoice-table th { background: rgba(200,164,78,0.15); color: var(--gold); padding: 0.6rem 0.75rem; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.5px; border: none; }
.invoice-table td { padding: 0.6rem 0.75rem; border-bottom: 1px solid rgba(255,255,255,0.06); font-size: 0.9rem; }
.invoice-table tr:last-child td { border-bottom: none; }
.invoice-totals { margin-left: auto; width: 300px; }
.invoice-totals .total-row { display: flex; justify-content: space-between; padding: 0.35rem 0; font-size: 0.9rem; }
.invoice-totals .total-row.grand-total { font-size: 1.2rem; font-weight: 700; color: var(--gold); border-top: 2px solid var(--gold); margin-top: 0.5rem; padding-top: 0.75rem; }
.payment-details { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid rgba(255,255,255,0.1); }
.payment-details h6 { color: var(--gold); margin-bottom: 0.75rem; }
.status-badge { display: inline-block; padding: 0.25rem 0.75rem; border-radius: 1rem; font-size: 0.8rem; font-weight: 600; }
.status-paid { background: rgba(25,135,84,0.2); color: #198754; }
.status-partial { background: rgba(255,193,7,0.2); color: #ffc107; }
.status-pending { background: rgba(108,117,125,0.2); color: #adb5bd; }
.status-refunded { background: rgba(220,53,69,0.2); color: #dc3545; }
.invoice-actions { display: flex; gap: 0.5rem; justify-content: center; margin-top: 2rem; }
@media print { .invoice-actions, .sidebar-toggle, .top-navbar, .app-footer, .sidebar-wrapper { display: none !important; } }
</style>

<div class="invoice-container">
    <div class="glass-card p-4">
            <div class="invoice-header">
                <?php
                $logoPath = PATH_UPLOADS . DS . 'settings' . DS . 'logo.png';
                if (file_exists($logoPath)): ?>
                    <img src="<?= uploadUrl('settings/logo.png') ?>" alt="<?= APP_NAME ?>" class="company-logo">
                <?php endif; ?>
                <h4><?= e(setting('app_name', APP_NAME)) ?></h4>
                <p><?= e(setting('app_address', '')) ?></p>
                <p>Tel: <?= e(setting('app_phone', '')) ?> | Email: <?= e(setting('app_email', '')) ?></p>
            </div>

        <div class="d-flex justify-content-between mb-3 flex-wrap gap-2">
            <div>
                <h5 class="mb-1">INVOICE</h5>
                <p class="mb-0 text-muted">#<?= e($invoice->invoice_number) ?></p>
            </div>
            <div class="text-end">
                <span class="status-badge status-<?= $invoice->payment_status ?>">
                    <?= e(ucfirst($invoice->payment_status)) ?>
                </span>
                <p class="mb-0 mt-1 text-muted">Date: <?= formatDate($invoice->created_at) ?></p>
            </div>
        </div>

        <div class="invoice-meta">
            <div class="meta-box">
                <div class="label">Bill To</div>
                <div class="value">
                    <?php if ($invoice->customer_id): ?>
                        <?= e($invoice->customer_first_name . ' ' . $invoice->customer_last_name) ?><br>
                        <small class="text-muted"><?= e($invoice->customer_email ?? '') ?></small><br>
                        <small class="text-muted"><?= e($invoice->customer_phone ?? '') ?></small>
                    <?php else: ?>
                        Walk-in Customer
                    <?php endif; ?>
                </div>
            </div>
            <div class="meta-box">
                <div class="label">Cashier</div>
                <div class="value"><?= e($invoice->user_first_name . ' ' . $invoice->user_last_name) ?></div>
            </div>
            <div class="meta-box">
                <div class="label">Branch</div>
                <div class="value"><?= e($invoice->branch_name ?? 'Main Branch') ?></div>
            </div>
        </div>

        <table class="invoice-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Item</th>
                    <th class="text-center">Qty</th>
                    <th class="text-end">Price</th>
                    <th class="text-end">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php $i = 1; foreach ($items as $item): ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td>
                            <?= e($item->item_name) ?>
                            <?php if ($item->reference_type === 'service'): ?>
                                <span class="badge bg-gold ms-1" style="font-size:0.65rem;">Service</span>
                            <?php else: ?>
                                <span class="badge bg-info ms-1" style="font-size:0.65rem;">Product</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center"><?= (int)$item->quantity ?></td>
                        <td class="text-end"><?= currency($item->price) ?></td>
                        <td class="text-end"><?= currency($item->total) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <div class="invoice-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-total">
                <span>Grand Total</span>
                <span><?= currency($invoice->total) ?></span>
            </div>
            <div class="total-row">
                <span>Paid</span>
                <span class="text-success"><?= 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 class="text-warning">Change Due</span>
                    <span class="text-warning"><?= currency($change) ?></span>
                </div>
            <?php elseif ((float)$invoice->balance_amount > 0): ?>
                <div class="total-row">
                    <span class="text-danger">Balance Due</span>
                    <span class="text-danger"><?= currency($invoice->balance_amount) ?></span>
                </div>
            <?php endif; ?>
        </div>

        <?php if (!empty($payments)): ?>
            <div class="payment-details">
                <h6><i class="fas fa-credit-card me-1"></i>Payment Details</h6>
                <table class="table table-dark table-sm table-borderless mb-0" style="max-width:400px;">
                    <thead>
                        <tr>
                            <th>Method</th>
                            <th class="text-end">Amount</th>
                            <th>Reference</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($payments as $p): ?>
                            <tr>
                                <td><?= e(ucwords(str_replace('_', ' ', $p->payment_method))) ?></td>
                                <td class="text-end"><?= 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="mt-3 p-3" style="background:rgba(255,255,255,0.03);border-radius:0.5rem;">
                <small class="text-muted">Note: <?= e($invoice->notes) ?></small>
            </div>
        <?php endif; ?>

        <div class="invoice-actions">
            <a href="<?= url('invoices/' . $invoice->id . '/print') ?>" class="btn btn-gold" target="_blank">
                <i class="fas fa-print me-1"></i>Print
            </a>
            <a href="<?= url('invoices') ?>" class="btn btn-outline-gold">
                <i class="fas fa-arrow-left me-1"></i>Back to Invoices
            </a>
        </div>
        <div class="text-center text-muted mt-3" style="font-size:0.8rem;">
            Printed by <?= e((\core\Auth::user()->first_name ?? '') . ' ' . (\core\Auth::user()->last_name ?? '')) ?> on <?= date('d/m/Y h:i A') ?>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
// Invoice page ready
EOS; ?>
