<?php $this->start('body'); ?>
<?php $p = $this->data['p']; $allocs = $this->data['allocations'];
$bg = ['recorded'=>'success','void'=>'dark'][$p['status']] ?? 'light';
$unalloc = (float)$p['amount'] - (float)$p['amount_allocated'];
?>

<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-money-bill-transfer me-2 text-muted"></i>
      <span class="font-monospace"><?= htmlspecialchars($p['payment_no']); ?></span>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.65em;vertical-align:middle;text-transform:capitalize;"><?= htmlspecialchars($p['status']); ?></span>
    </h5>
    <div class="text-muted small"><?= htmlspecialchars($p['vendor_name'] ?? '—'); ?> · <?= humanDate($p['payment_date']); ?> · <?= htmlspecialchars(str_replace('_', ' ', $p['payment_method'])); ?></div>
  </div>
  <a href="<?= PROOT; ?>SupplierPayments" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Payments</a>
</div>

<?= flashHtml(); ?>

<?php if ($p['status'] === 'void'): ?>
  <div class="alert alert-dark py-2 small"><strong>Void:</strong> <?= htmlspecialchars($p['void_reason'] ?: '(no reason)'); ?></div>
<?php endif; ?>

<div class="row g-2 mb-3">
  <div class="col-6 col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Amount</div>
    <div class="fs-4 fw-bold text-primary">TZS <?= number_format((float)$p['amount'], 2); ?></div>
  </div></div></div>
  <div class="col-6 col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Allocated</div>
    <div class="fs-5 fw-bold text-success">TZS <?= number_format((float)$p['amount_allocated'], 2); ?></div>
    <div class="small text-muted"><?= count($allocs); ?> bill(s)</div>
  </div></div></div>
  <div class="col-md-4"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Unallocated (on account)</div>
    <div class="fs-5 fw-bold <?= $unalloc > 0.005 ? 'text-warning' : ''; ?>">TZS <?= number_format($unalloc, 2); ?></div>
  </div></div></div>
</div>

<div class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2">
    <?php if ($p['journal_entry_id']): ?>
      <a href="<?= PROOT; ?>Journals/view/<?= (int)$p['journal_entry_id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-file-alt me-1"></i>Journal</a>
    <?php endif; ?>
    <?php if ($p['status'] !== 'void'): ?>
      <button type="button" class="btn btn-sm btn-outline-danger ms-auto" onclick="doVoid()"><i class="fas fa-ban me-1"></i>Void</button>
    <?php endif; ?>
  </div>
</div>

<div class="card border-0 shadow-sm">
  <div class="card-body p-0">
    <div class="p-3 pb-2"><h6 class="text-uppercase text-muted small mb-0">Allocations</h6></div>
    <?php if (empty($allocs)): ?>
      <div class="text-center text-muted small py-4">Payment sits unallocated on the supplier's account.</div>
    <?php else: ?>
      <table class="table table-sm mb-0">
        <thead class="table-light"><tr><th>Bill</th><th>Supplier ref</th><th>Bill date</th><th class="money">Total</th><th class="money">Applied</th></tr></thead>
        <tbody>
          <?php foreach ($allocs as $a): ?>
            <tr>
              <td class="font-monospace small"><a href="<?= PROOT; ?>SupplierBills/view/<?= (int)$a['bill_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($a['bill_no']); ?></a></td>
              <td class="small text-muted"><?= htmlspecialchars($a['supplier_ref'] ?: '—'); ?></td>
              <td class="small"><?= humanDate($a['bill_date']); ?></td>
              <td class="money font-monospace small"><?= number_format((float)$a['total_amount'], 2); ?></td>
              <td class="money font-monospace"><?= number_format((float)$a['amount_applied'], 2); ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>
  </div>
</div>

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

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function doVoid() {
    $.confirm({
        title: 'Void this payment?', type: 'red',
        content: 'Posts a reversing JE and clears the allocations. Bills marked "paid" will revert to "posted".<br><br><label class="form-label small">Reason <span class="text-danger">*</span></label><textarea id="rn" class="form-control form-control-sm" rows="3"></textarea>',
        buttons: {
            go: { text: 'Void', btnClass: 'btn-danger', action: function() {
                const r = document.getElementById('rn').value.trim();
                if (!r) { $.alert('Reason required.'); return false; }
                document.getElementById('fReason').value = r;
                document.getElementById('voidForm').submit();
            }},
            back: function(){}
        }
    });
}
</script>
<?php $this->end(); ?>
