<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-file-invoice-dollar me-2 text-primary"></i>Invoice <?= e($invoice['invoice_number']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Invoices') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Invoices/edit/' . (int) $invoice['invoice_id']) ?>"><i class="fas fa-edit me-1"></i>Edit</a>
        <a class="btn btn-sm btn-outline-info" href="#" onclick="previewDocument('<?= url('/Invoices/pdf/' . (int) $invoice['invoice_id']) ?>','Invoice <?= e($invoice['invoice_number']) ?>');return false"><i class="fas fa-file-pdf me-1"></i>PDF</a>
        <?php if ((float) $invoice['balance_due'] > 0): ?>
            <form method="post" action="<?= url('/Invoices/sendReminder/' . (int) $invoice['invoice_id']) ?>" style="display:inline">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <button type="submit" class="btn btn-sm btn-outline-warning"><i class="fas fa-bell me-1"></i>Send Reminder</button>
            </form>
        <?php endif; ?>
    </div>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="row g-3">
    <div class="col-md-8">
        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-2">Customer Details</h6>
            <div class="row g-2 small">
                <div class="col-md-6"><strong>Name:</strong> <?= e($invoice['customer_name'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Contact:</strong> <?= e($invoice['customer_contact'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Email:</strong> <?= e($invoice['customer_email'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Phone:</strong> <?= e($invoice['customer_phone'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Tax ID:</strong> <?= e($invoice['customer_tax_id'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Address:</strong> <?= e($invoice['customer_address'] ?? '—') ?></div>
            </div>
        </div>

        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-2">Line Items</h6>
            <?php if (empty($items)): ?>
                <p class="text-muted small mb-0">No items.</p>
            <?php else: ?>
                <table class="table table-sm mb-0">
                    <thead><tr><th>#</th><th>Description</th><th class="text-end">Qty</th><th class="text-end">Unit Price</th><th class="text-end">Disc %</th><th class="text-end">Line Total</th></tr></thead>
                    <tbody>
                        <?php foreach ($items as $idx => $it): ?>
                            <tr>
                                <td class="small"><?= $idx + 1 ?></td>
                                <td><?= e($it['description']) ?></td>
                                <td class="text-end"><?= e($it['quantity']) ?></td>
                                <td class="text-end"><?= number_format((float) $it['unit_price'], 2) ?></td>
                                <td class="text-end"><?= (float) $it['discount_percent'] ?>%</td>
                                <td class="text-end"><?= number_format((float) $it['line_total'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-2">Payments</h6>
            <?php if (empty($payments)): ?>
                <p class="text-muted small mb-0">No payments recorded yet.</p>
            <?php else: ?>
                <table class="table table-sm mb-0">
                    <thead><tr><th>Date</th><th>Method</th><th>Reference</th><th class="text-end">Amount</th><th>Recorded By</th></tr></thead>
                    <tbody>
                        <?php foreach ($payments as $pmt): ?>
                            <tr>
                                <td class="small"><?= e($pmt['payment_date']) ?></td>
                                <td class="small"><span class="badge bg-secondary"><?= e($pmt['payment_method']) ?></span></td>
                                <td class="small"><?= e($pmt['reference_number'] ?? '—') ?></td>
                                <td class="text-end fw-bold"><?= number_format((float) $pmt['amount'], 2) ?></td>
                                <td class="small text-muted"><?= e($pmt['created_by_name'] ?? '—') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>

    <div class="col-md-4">
        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-2">Summary</h6>
            <div class="d-flex justify-content-between small mb-1"><span>Status:</span><span class="badge <?= $invoice['status'] === 'paid' ? 'bg-success' : ($invoice['status'] === 'overdue' ? 'bg-danger' : ($invoice['status'] === 'partial' ? 'bg-warning text-dark' : 'bg-secondary')) ?> text-uppercase"><?= e($invoice['status']) ?></span></div>
            <div class="d-flex justify-content-between small mb-1"><span>Invoice #:</span><span><?= e($invoice['invoice_number']) ?></span></div>
            <div class="d-flex justify-content-between small mb-1"><span>Due Date:</span><span><?= e($invoice['due_date'] ?? '—') ?></span></div>
            <hr>
            <div class="d-flex justify-content-between mb-1"><span>Subtotal</span><strong><?= number_format((float) $invoice['subtotal'], 2) ?></strong></div>
            <div class="d-flex justify-content-between mb-1"><span>Discount <?= (float) $invoice['discount_percent'] ?>%</span><span>-<?= number_format((float) $invoice['discount_amount'], 2) ?></span></div>
            <div class="d-flex justify-content-between mb-1"><span>Tax</span><strong><?= number_format((float) $invoice['tax_total'], 2) ?></strong></div>
            <hr>
            <div class="d-flex justify-content-between mb-1"><span>Total</span><strong><?= number_format((float) $invoice['grand_total'], 2) ?></strong></div>
            <div class="d-flex justify-content-between mb-1 text-success"><span>Paid</span><strong><?= number_format((float) $invoice['amount_paid'], 2) ?></strong></div>
            <hr>
            <div class="d-flex justify-content-between fw-bold fs-5 <?= (float) $invoice['balance_due'] > 0 ? 'text-danger' : 'text-success' ?>"><span>Balance Due</span><span><?= number_format((float) $invoice['balance_due'], 2) ?></span></div>
        </div>

        <?php if ((float) $invoice['balance_due'] > 0): ?>
            <div class="rs-card p-3 mb-3">
                <h6 class="fw-bold mb-2">Record Payment</h6>
                <form method="post" action="<?= url('/Invoices/addPayment/' . (int) $invoice['invoice_id']) ?>">
                    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                    <div class="mb-2">
                        <label class="form-label small">Amount</label>
                        <input type="number" step="0.01" name="amount" class="form-control form-control-sm" max="<?= (float) $invoice['balance_due'] ?>" required placeholder="<?= number_format((float) $invoice['balance_due'], 2) ?>">
                    </div>
                    <div class="mb-2">
                        <label class="form-label small">Payment Method</label>
                        <select name="payment_method" class="form-select form-select-sm">
                            <?php foreach (\App\Models\Invoice::PAYMENT_METHODS as $m): ?>
                                <option value="<?= $m ?>"><?= e(str_replace('_', ' ', $m)) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-2">
                        <label class="form-label small">Payment Date</label>
                        <input type="date" name="payment_date" class="form-control form-control-sm" value="<?= date('Y-m-d') ?>">
                    </div>
                    <div class="mb-2">
                        <label class="form-label small">Reference Number</label>
                        <input type="text" name="reference_number" class="form-control form-control-sm" placeholder="Optional">
                    </div>
                    <div class="mb-2">
                        <label class="form-label small">Notes</label>
                        <textarea name="notes" class="form-control form-control-sm" rows="2"></textarea>
                    </div>
                    <button type="submit" class="btn btn-sm btn-success w-100"><i class="fas fa-check me-1"></i>Record Payment</button>
                </form>
            </div>
        <?php endif; ?>

        <?php if (!empty($invoice['notes'])): ?>
            <div class="rs-card p-3 mb-3">
                <h6 class="fw-bold mb-1">Notes</h6>
                <p class="small text-muted mb-0"><?= nl2br(e($invoice['notes'])) ?></p>
            </div>
        <?php endif; ?>
        <?php if (!empty($invoice['terms'])): ?>
            <div class="rs-card p-3 mb-3">
                <h6 class="fw-bold mb-1">Terms</h6>
                <p class="small text-muted mb-0"><?= nl2br(e($invoice['terms'])) ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>