<%- include('partials/head') %>
<div class="page-head">
  <div>
    <h1><%= t('wallet.heading') %></h1>
    <p class="muted small"><%= t('wallet.sub') %></p>
  </div>
  <div class="balance-big"><span class="muted small"><%= t('wallet.available') %></span><%= fmt(user.balance) %></div>
</div>

<div class="grid two">
  <div class="panel">
    <% if (paymentsOnline.stripe || paymentsOnline.paypal) { %>
      <h2><%= t('wallet.pay_online') %></h2>
      <p class="muted small"><%= t('wallet.pay_online_sub') %></p>
      <form method="post" action="/wallet/checkout" class="form">
        <input type="hidden" name="_csrf" value="<%= csrf %>">
        <label><%= t('wallet.amount_usd') %>
          <input type="number" name="amount" min="1" max="10000" step="0.01" required placeholder="<%= t('wallet.amount_placeholder') %>">
        </label>
        <div class="cta-row">
          <% if (paymentsOnline.stripe) { %>
            <button class="btn btn-primary" type="submit" name="provider" value="stripe"><%= t('wallet.pay_stripe') %></button>
          <% } %>
          <% if (paymentsOnline.paypal) { %>
            <button class="btn btn-ghost" type="submit" name="provider" value="paypal"><%= t('wallet.pay_paypal') %></button>
          <% } %>
        </div>
      </form>
    <% } %>
    <h2 class="<%= (paymentsOnline.stripe || paymentsOnline.paypal) ? 'mt-sm' : '' %>"><%= t('wallet.deposit_title') %></h2>
    <p class="muted small"><%= t('wallet.deposit_sub') %></p>
    <form method="post" action="/wallet/deposits" class="form">
      <input type="hidden" name="_csrf" value="<%= csrf %>">
      <label><%= t('wallet.amount_usd') %>
        <input type="number" name="amount" min="1" max="10000" step="0.01" required placeholder="<%= t('wallet.amount_placeholder') %>">
      </label>
      <label><%= t('wallet.method') %>
        <select name="method" required>
          <% Object.keys(DEPOSIT_METHODS).forEach(function (key) { %>
            <option value="<%= key %>"><%= DEPOSIT_METHODS[key] %></option>
          <% }) %>
        </select>
      </label>
      <label><%= t('wallet.reference_optional') %>
        <input name="reference" maxlength="100" placeholder="<%= t('wallet.reference_placeholder') %>">
      </label>
      <button class="btn btn-primary btn-block" type="submit"><%= t('wallet.submit_deposit') %></button>
    </form>
  </div>

  <div class="panel">
    <div class="panel-head">
      <h2><%= t('wallet.tx_title') %></h2>
    </div>
    <% if (!transactions.length) { %>
      <div class="empty"><%= t('wallet.tx_empty') %></div>
    <% } else { %>
      <table class="table">
        <thead><tr><th><%= t('common.status') %></th><th><%= t('common.amount') %></th><th><%= t('wallet.balance_after') %></th><th><%= t('common.reference') %></th><th><%= t('common.date') %></th></tr></thead>
        <tbody>
          <% transactions.forEach(function (tx) { %>
            <tr>
              <td><%= TX_TYPES[tx.type] || tx.type %></td>
              <td class="mono <%= tx.amount < 0 ? 'neg' : 'pos' %>"><%= tx.amount < 0 ? '-' : '+' %><%= fmt(Math.abs(tx.amount)) %></td>
              <td class="mono"><%= fmt(tx.balance_after) %></td>
              <td class="muted small"><%= tx.ref %></td>
              <td class="muted small"><%= tx.created_at.slice(0, 16) %></td>
            </tr>
          <% }) %>
        </tbody>
      </table>
    <% } %>
  </div>
</div>

<div class="panel">
  <div class="panel-head"><h2><%= t('wallet.deposits_title') %></h2></div>
  <% const DEP_ST = { pending: t('wallet.deposit_pending'), approved: t('wallet.deposit_approved'), rejected: t('wallet.deposit_rejected') }; %>
  <% if (!deposits.length) { %>
    <div class="empty"><%= t('wallet.deposits_empty') %></div>
  <% } else { %>
    <table class="table">
      <thead><tr><th>#</th><th><%= t('common.amount') %></th><th><%= t('common.method') %></th><th><%= t('common.reference') %></th><th><%= t('common.status') %></th><th><%= t('common.date') %></th></tr></thead>
      <tbody>
        <% deposits.forEach(function (d) { %>
          <tr>
            <td class="mono">#<%= d.id %></td>
            <td class="mono"><%= fmt(d.amount) %></td>
            <td><%= DEPOSIT_METHODS[d.method] || (d.method === 'stripe' ? 'Stripe' : d.method === 'paypal' ? 'PayPal' : d.method) %></td>
            <td class="muted small"><%= d.reference || '—' %></td>
            <td><span class="badge badge-<%= d.status %>"><%= DEP_ST[d.status] %></span></td>
            <td class="muted small"><%= d.created_at.slice(0, 16) %></td>
          </tr>
        <% }) %>
      </tbody>
    </table>
  <% } %>
</div>
<%- include('partials/foot') %>
