<?php $this->start('body'); ?>
<?php $r = $this->data['rcpt']; $al = $this->data['allocations'];
$bg = $r['status'] === 'void' ? 'dark' : 'success';
?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-hand-holding-usd me-2 text-muted"></i><span class="font-monospace"><?= htmlspecialchars($r['receipt_no']); ?></span>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.65em;vertical-align:middle;text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span>
    </h5>
    <div class="text-muted small">
      <a href="<?= PROOT; ?>Customers/view/<?= (int)$r['customer_id']; ?>"><?= htmlspecialchars($r['customer_name']); ?></a>
      · <?= humanDate($r['receipt_date']); ?>
      · <?= htmlspecialchars(str_replace('_', ' ', $r['payment_method'])); ?>
      <?php if ($r['reference']): ?> · <?= htmlspecialchars($r['reference']); ?><?php endif; ?>
    </div>
  </div>
  <a href="<?= PROOT; ?>CustomerReceipts" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= flashHtml(); ?>

<?php if ($r['status'] === 'void' && $r['void_reason']): ?>
  <div class="alert alert-dark py-2 small"><i class="fas fa-ban me-1"></i><strong>Void:</strong> <?= htmlspecialchars($r['void_reason']); ?></div>
<?php endif; ?>

<div class="row g-3">
  <div class="col-lg-8">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Allocations</h6>
        <table class="table table-sm">
          <thead class="table-light"><tr><th>Invoice #</th><th>Total</th><th>Paid</th><th class="money">Applied here</th><th>Now</th></tr></thead>
          <tbody>
            <?php $totApplied = 0; foreach ($al as $a): $totApplied += (float)$a['amount_applied']; ?>
              <tr>
                <td class="small"><a href="<?= PROOT; ?>CustomerInvoices/view/<?= (int)$a['invoice_id']; ?>" class="font-monospace"><?= htmlspecialchars($a['invoice_no']); ?></a></td>
                <td class="money small"><?= number_format((float)$a['total_amount'], 2, '.', ','); ?></td>
                <td class="money small text-success"><?= number_format((float)$a['amount_paid'], 2, '.', ','); ?></td>
                <td class="money"><?= number_format((float)$a['amount_applied'], 2, '.', ','); ?></td>
                <td><span class="badge bg-<?= $a['status'] === 'paid' ? 'success' : 'warning'; ?>"><?= htmlspecialchars(str_replace('_', ' ', $a['status'])); ?></span></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
          <tfoot class="table-light">
            <tr><th colspan="3" class="text-end">Total applied</th><th class="money"><?= number_format($totApplied, 2, '.', ','); ?></th><th></th></tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>

  <div class="col-lg-4">
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Receipt</h6>
        <dl class="row mb-0 small">
          <dt class="col-5 text-muted fw-normal">Amount</dt><dd class="col-7 fw-semibold"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['amount'], 2, '.', ','); ?></dd>
          <dt class="col-5 text-muted fw-normal">Allocated</dt><dd class="col-7"><?= number_format((float)$r['amount_allocated'], 2, '.', ','); ?></dd>
          <dt class="col-5 text-muted fw-normal">Method</dt><dd class="col-7" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_', ' ', $r['payment_method'])); ?></dd>
          <?php if ($r['reference']): ?><dt class="col-5 text-muted fw-normal">Reference</dt><dd class="col-7 font-monospace"><?= htmlspecialchars($r['reference']); ?></dd><?php endif; ?>
          <?php if ($r['journal_entry_id']): ?><dt class="col-5 text-muted fw-normal">Journal</dt><dd class="col-7"><a href="<?= PROOT; ?>Journals/view/<?= (int)$r['journal_entry_id']; ?>">JE #<?= (int)$r['journal_entry_id']; ?></a></dd><?php endif; ?>
          <?php if ($r['notes']): ?><dt class="col-5 text-muted fw-normal">Notes</dt><dd class="col-7"><?= nl2br(htmlspecialchars($r['notes'])); ?></dd><?php endif; ?>
        </dl>
      </div>
    </div>

    <?php if ($r['status'] !== 'void'): ?>
      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <h6 class="text-uppercase text-muted small text-danger mb-3">Danger zone</h6>
          <button class="btn btn-sm btn-outline-danger w-100" onclick="doVoid()"><i class="fas fa-ban me-1"></i>Void receipt</button>
        </div>
      </div>
    <?php endif; ?>
  </div>
</div>

<form id="voidForm" method="POST" action="<?= PROOT; ?>CustomerReceipts/void/<?= (int)$r['c0d']; ?>" class="d-none"><?= csrf_field(); ?><input type="hidden" name="reason" id="voidReason"></form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function doVoid() {
    $.confirm({ title: 'Void this receipt?', content: '<div class="mb-2 small">Reverses the JE and unpays every allocated invoice.</div><label class="form-label small">Reason <span class="text-danger">*</span></label><textarea id="rn" class="form-control form-control-sm" rows="3"></textarea>', type: 'red',
        buttons: { go: { text: 'Void', btnClass: 'btn-danger', action: function() {
            const rn = document.getElementById('rn').value.trim();
            if (!rn) { $.alert('Reason required.'); return false; }
            document.getElementById('voidReason').value = rn;
            document.getElementById('voidForm').submit();
        }}, back: function(){} } });
}
</script>
<?php $this->end(); ?>
