<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-file-invoice me-2 text-primary"></i>Quote <?= e($quote['quote_number']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Quotes') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Quotes/edit/' . (int) $quote['quote_id']) ?>"><i class="fas fa-edit me-1"></i>Edit</a>
        <a class="btn btn-sm btn-outline-info" href="#" onclick="previewDocument('<?= url('/Quotes/pdf/' . (int) $quote['quote_id']) ?>','Quote <?= e($quote['quote_number']) ?>');return false"><i class="fas fa-file-pdf me-1"></i>PDF</a>
        <?php if ($quote['status'] !== 'converted'): ?>
            <form method="post" action="<?= url('/Quotes/convertToInvoice/' . (int) $quote['quote_id']) ?>" style="display:inline">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <button type="submit" class="btn btn-sm btn-success" onclick="return confirm('Convert this quote to an invoice?')"><i class="fas fa-exchange-alt me-1"></i>Convert to Invoice</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($quote['customer_name'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Contact:</strong> <?= e($quote['customer_contact'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Email:</strong> <?= e($quote['customer_email'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Phone:</strong> <?= e($quote['customer_phone'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Tax ID:</strong> <?= e($quote['customer_tax_id'] ?? '—') ?></div>
                <div class="col-md-6"><strong>Address:</strong> <?= e($quote['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>

    <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 bg-secondary text-uppercase"><?= e($quote['status']) ?></span></div>
            <div class="d-flex justify-content-between small mb-1"><span>Valid Until:</span><span><?= e($quote['valid_until'] ?? '—') ?></span></div>
            <hr>
            <div class="d-flex justify-content-between mb-1"><span>Subtotal</span><strong><?= number_format((float) $quote['subtotal'], 2) ?></strong></div>
            <div class="d-flex justify-content-between mb-1"><span>Discount <?= (float) $quote['discount_percent'] ?>%</span><span>-<?= number_format((float) $quote['discount_amount'], 2) ?></span></div>
            <div class="d-flex justify-content-between mb-1"><span>Tax</span><strong><?= number_format((float) $quote['tax_total'], 2) ?></strong></div>
            <hr>
            <div class="d-flex justify-content-between fw-bold fs-5"><span>Total</span><span><?= number_format((float) $quote['grand_total'], 2) ?></span></div>
        </div>

        <?php if (!empty($quote['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($quote['notes'])) ?></p>
            </div>
        <?php endif; ?>
        <?php if (!empty($quote['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($quote['terms'])) ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>