<section class="section narrow">
  <div class="section-header">
    <p class="eyebrow">Cashless wallet</p>
    <h1><%= wallet.balance %> <%= wallet.currency %></h1>
  </div>
  <form class="filters" onsubmit="return topup(event)">
    <input name="amount" type="number" min="1" step="0.01" placeholder="Suma RON" required>
    <button>Incarca mock</button>
  </form>
  <form class="filters" onsubmit="return refundWallet(event)">
    <button>Cere refund sold</button>
  </form>
</section>
<section class="section">
  <div class="table-wrap"><table><thead><tr><th>Tip</th><th>Suma</th><th>Sold dupa</th><th>Status</th><th>Data</th></tr></thead><tbody>
    <% transactions.forEach((tx) => { %><tr><td><%= tx.type %></td><td><%= tx.amount %></td><td><%= tx.balance_after %></td><td><%= tx.status %></td><td><%= tx.created_at %></td></tr><% }) %>
  </tbody></table></div>
</section>
<script>
async function topup(event) {
  event.preventDefault();
  const response = await fetch('/api/cashless/topup', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'x-csrf-token': '<%= csrfToken %>' },
    body: JSON.stringify({ wallet_id: '<%= wallet.id %>', amount: event.target.amount.value, idempotency_key: 'topup:<%= wallet.id %>:' + Date.now() })
  });
  if (!response.ok) alert('Top-up esuat.');
  window.location.reload();
}
async function refundWallet(event) {
  event.preventDefault();
  const response = await fetch('/api/cashless/refund-request', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'x-csrf-token': '<%= csrfToken %>' },
    body: JSON.stringify({ wallet_id: '<%= wallet.id %>' })
  });
  if (!response.ok) alert('Refund esuat sau fereastra a expirat.');
  window.location.reload();
}
</script>
