<?php $this->start('body');
    $r     = $this->data['r'];
    $types = $this->data['types'] ?? [];
    $je    = $this->data['je']    ?? [];
    $jls   = $this->data['je_lines'] ?? [];
    $t     = $types[$r['receipt_type']] ?? ['label'=>$r['receipt_type'],'icon'=>'fas fa-receipt'];
    $cls   = ['EFD_VAT'=>'bg-success','EFD_NON_VAT'=>'bg-info text-white','NORMAL'=>'bg-light text-muted'][$r['receipt_type']] ?? 'bg-light text-muted';
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-receipt me-2 text-muted"></i><?= htmlspecialchars($r['receipt_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <span class="badge <?= $cls; ?>"><i class="<?= $t['icon']; ?> me-1"></i><?= htmlspecialchars($t['label']); ?></span> ·
            <?= htmlspecialchars($r['receipt_date']); ?> ·
            <?= htmlspecialchars($r['customer_name']); ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>Receipts" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All receipts</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Amount</div><div style="font-weight:700;font-size:1.15rem;"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['amount']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Of which VAT</div><div style="font-weight:600;color:<?= (float)$r['efd_output_vat']>0?'#22c55e':''; ?>;"><?= (float)$r['efd_output_vat'] > 0 ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['efd_output_vat']) : '—'; ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Method / ref</div><div style="font-weight:600;"><?= htmlspecialchars(ucwords(strtolower(str_replace('_',' ',$r['payment_method'])))); ?></div><div class="text-muted small"><?= htmlspecialchars($r['bank_reference'] ?: '—'); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Bank</div><div style="font-weight:600;"><?= htmlspecialchars($r['bank_name']); ?></div><div class="text-muted small"><?= htmlspecialchars($r['bank_account_name'] ?: ''); ?></div></div></div></div>
</div>

<div class="row g-3 mb-3">
    <?php if ((int)$r['sale_id'] > 0): ?>
    <div class="col-md-6"><div class="card border-0 shadow-sm"><div class="card-body p-3">
        <div class="text-muted small mb-1">Applied to invoice</div>
        <a href="<?= PROOT; ?>Sales/view/<?= (int)$r['sale_id']; ?>" class="text-decoration-none">
            <strong><?= htmlspecialchars($r['sale_no'] ?: $r['invoice_ref']); ?></strong>
        </a>
        <div class="text-muted small">Invoice total: <?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['sale_total']); ?></div>
    </div></div></div>
    <?php endif; ?>
    <?php if (!empty($r['efd_receipt_number'])): ?>
    <div class="col-md-6"><div class="card border-0 shadow-sm"><div class="card-body p-3">
        <div class="text-muted small mb-1"><i class="fas fa-print me-1"></i>EFD fiscal receipt</div>
        <div><strong>#<?= htmlspecialchars($r['efd_receipt_number']); ?></strong> · <?= htmlspecialchars($r['device_model'] ?: $r['efd_serial']); ?></div>
        <div class="text-muted small">Serial: <?= htmlspecialchars($r['efd_serial']); ?><?php if ($r['efd_z_number']): ?> · Z: <?= htmlspecialchars($r['efd_z_number']); ?><?php endif; ?></div>
        <?php if ($r['efd_signature']): ?><div class="text-muted small">Sig: <code class="text-body"><?= htmlspecialchars($r['efd_signature']); ?></code></div><?php endif; ?>
        <?php if ($r['efd_issued_at']): ?><div class="text-muted small">Issued at: <?= htmlspecialchars($r['efd_issued_at']); ?></div><?php endif; ?>
    </div></div></div>
    <?php endif; ?>
</div>

<?php if (!empty($je)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <strong><i class="fas fa-balance-scale me-2 text-muted"></i>Money-in journal</strong>
        <a href="<?= PROOT; ?>Journals/view/<?= (int)$je['c0d']; ?>" class="btn btn-sm btn-outline-primary"><?= htmlspecialchars($je['je_no']); ?> <i class="fas fa-arrow-right ms-1"></i></a>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Account</th><th>Narrative</th><th class="text-end">Debit</th><th class="text-end">Credit</th></tr>
            </thead>
            <tbody>
                <?php foreach ($jls as $ln): ?>
                    <tr>
                        <td><code class="text-body"><?= htmlspecialchars($ln['account_code']); ?></code> · <?= htmlspecialchars($ln['account_name']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($ln['narrative']); ?></td>
                        <td class="text-end"><?= (float)$ln['debit']  > 0 ? number_format((float)$ln['debit'])  : '—'; ?></td>
                        <td class="text-end"><?= (float)$ln['credit'] > 0 ? number_format((float)$ln['credit']) : '—'; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $this->end(); ?>
