<div class="d-flex flex-wrap gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/pembayaran') ?>"><i class="bi bi-arrow-left me-1"></i>Kembali</a>
  <?php if (can('pembayaran','edit') && $pay['status'] !== 'reversed'): ?>
    <a class="btn btn-sm btn-outline-primary" href="<?= url('/pembayaran/'.(int)$pay['id'].'/ubah') ?>"><i class="bi bi-pencil me-1"></i>Ubah</a><?php endif; ?>
  <?php if (can('pembayaran','print')): ?>
    <a class="btn btn-sm btn-outline-dark" target="_blank" href="<?= url('/pembayaran/'.(int)$pay['id'].'/cetak') ?>"><i class="bi bi-printer me-1"></i>Cetak Kuitansi</a><?php endif; ?>
  <?php if (can('pembayaran','approve') && $pay['approval_status'] === 'pending'): ?>
    <form method="post" action="<?= url('/pembayaran/'.(int)$pay['id'].'/setujui') ?>"><?= csrf() ?>
      <button class="btn btn-sm btn-success" data-konfirmasi="Setujui pembayaran ini? Saldo invoice akan diperbarui." data-konfirmasi-warna="success"><i class="bi bi-check-lg me-1"></i>Setujui</button></form>
  <?php endif; ?>
  <?php if (can('pembayaran','edit') && $pay['status'] === 'posted'): ?>
    <button class="btn btn-sm btn-outline-danger ms-auto" data-bs-toggle="modal" data-bs-target="#modalBalik"><i class="bi bi-arrow-counterclockwise me-1"></i>Balikkan (Reversal)</button>
  <?php endif; ?>
</div>

<?php if ($pay['status'] === 'reversed'): ?>
  <div class="alert alert-dark"><i class="bi bi-arrow-counterclockwise me-1"></i>Pembayaran ini telah <strong>dibalik</strong>.
    <?= $pay['reversal_reason'] ? 'Alasan: ' . e($pay['reversal_reason']) : '' ?></div>
<?php endif; ?>
<?php if ($reversal): ?>
  <div class="alert alert-info">Transaksi ini adalah pembalikan atas pembayaran
    <a href="<?= url('/pembayaran/'.(int)$reversal['id']) ?>"><strong><?= e($reversal['payment_no']) ?></strong></a>.</div>
<?php endif; ?>

<div class="row g-3">
  <div class="col-lg-5">
    <div class="card"><div class="card-header"><i class="bi bi-info-circle me-1"></i>Informasi Pembayaran</div>
      <div class="card-body"><dl class="dl-grid">
        <dt>Nomor Pembayaran</dt><dd class="fw-bold"><?= e($pay['payment_no']) ?></dd>
        <dt>Tanggal</dt><dd><?= tgl($pay['payment_date']) ?></dd>
        <dt>Customer</dt><dd><?= e($pay['customer_name'] ?: '-') ?></dd>
        <dt>Metode</dt><dd><?= e(\App\Services\PaymentService::METHODS[$pay['method']] ?? $pay['method']) ?></dd>
        <dt>Bank</dt><dd><?= e($pay['bank_name'] ?: '-') ?></dd>
        <dt>Nomor Referensi</dt><dd><?= e($pay['reference_no'] ?: '-') ?></dd>
        <dt>Jenis</dt><dd><?= e(\App\Services\PaymentService::TYPES[$pay['payment_type']] ?? $pay['payment_type']) ?></dd>
        <dt>Nilai Diterima</dt><dd class="fw-bold fs-6"><?= rph($pay['amount']) ?></dd>
        <dt>Teralokasi</dt><dd><?= rph($pay['allocated_amount']) ?></dd>
        <dt>Status</dt><dd><?= badge(['posted'=>'Diposting','draft'=>'Draf','reversed'=>'Dibalik'][$pay['status']] ?? $pay['status'],
              $pay['status']==='posted'?'success':($pay['status']==='reversed'?'dark':'warning')) ?></dd>
        <dt>Status Persetujuan</dt><dd><?= badge(['approved'=>'Disetujui','pending'=>'Menunggu','rejected'=>'Ditolak','not_required'=>'Tidak Diperlukan'][$pay['approval_status']] ?? $pay['approval_status'],
              in_array($pay['approval_status'],['approved','not_required'],true)?'success':($pay['approval_status']==='rejected'?'danger':'warning')) ?></dd>
        <dt>Dibuat Oleh</dt><dd><?= e($pay['created_by_name'] ?: '-') ?> · <?= tglWaktu($pay['created_at']) ?></dd>
        <?php if ($pay['approved_by_name']): ?><dt>Disetujui Oleh</dt><dd><?= e($pay['approved_by_name']) ?> · <?= tglWaktu($pay['approved_at']) ?></dd><?php endif; ?>
      </dl>
      <?php if ($pay['notes']): ?><hr><div class="small"><strong>Catatan:</strong> <?= nlbr($pay['notes']) ?></div><?php endif; ?>
      </div></div>
  </div>
  <div class="col-lg-7">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-list-check me-1"></i>Alokasi ke Invoice (<?= count($allocations) ?>)</div>
      <div class="table-wrap"><table class="table table-sm table-hover mb-0">
        <thead><tr><th>Invoice</th><th class="ctr">Tgl Invoice</th><th class="ctr">Jatuh Tempo</th><th class="num">Total Tagihan</th>
          <th class="num">Alokasi</th><th class="num">Saldo Kini</th><th class="ctr">Status</th></tr></thead>
        <tbody>
        <?php foreach ($allocations as $a): ?>
          <tr><td><a href="<?= url('/invoice/'.(int)$a['invoice_id']) ?>"><?= e($a['invoice_no']) ?></a></td>
            <td class="ctr"><?= tgl($a['invoice_date']) ?></td><td class="ctr"><?= tgl($a['due_date']) ?></td>
            <td class="num"><?= rph($a['net_amount']) ?></td><td class="num cell-strong"><?= rph($a['amount']) ?></td>
            <td class="num"><?= rph($a['outstanding_amount']) ?></td>
            <td class="ctr"><?= statusBadge($a['payment_status_code']) ?></td></tr>
        <?php endforeach; ?>
        </tbody>
        <tfoot><tr><td colspan="4">TOTAL</td><td class="num"><?= rph(array_sum(array_map(fn($a)=>(float)$a['amount'],$allocations))) ?></td><td colspan="2"></td></tr></tfoot>
      </table></div>
    </div>
    <?php if ($audit): ?>
    <div class="card"><div class="card-header"><i class="bi bi-clock-history me-1"></i>Riwayat</div>
      <div class="card-body"><div class="timeline">
        <?php foreach ($audit as $a): ?>
          <div class="timeline-item"><div class="timeline-time"><?= tglWaktu($a['created_at']) ?></div>
            <div class="small"><strong><?= e(\App\Core\Audit::actionLabel($a['action'])) ?></strong> — <?= e($a['user_name'] ?: 'Sistem') ?></div>
            <div class="small text-body-secondary"><?= e($a['description']) ?></div></div>
        <?php endforeach; ?>
      </div></div></div>
    <?php endif; ?>
  </div>
</div>

<div class="modal fade" id="modalBalik" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
  <form method="post" action="<?= url('/pembayaran/'.(int)$pay['id'].'/balik') ?>">
    <?= csrf() ?>
    <div class="modal-header"><h5 class="modal-title">Balikkan Pembayaran</h5>
      <button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body">
      <p class="small">Pembalikan membuat transaksi negatif berpasangan sehingga jejak audit tetap utuh dan saldo invoice kembali seperti sebelum pembayaran.</p>
      <label class="form-label required" for="alasan">Alasan Pembalikan</label>
      <textarea class="form-control" id="alasan" name="alasan" rows="3" required maxlength="400"></textarea>
    </div>
    <div class="modal-footer"><button type="button" class="btn btn-light" data-bs-dismiss="modal">Batal</button>
      <button class="btn btn-danger" type="submit">Balikkan Pembayaran</button></div>
  </form>
</div></div></div>
