<!DOCTYPE html>
<html lang="<%= lang || 'id' %>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><%= t('agent.print_thermal', 'Print Thermal') %> Voucher #<%= tx.id %></title>
  <style>
    @page { margin: 0; }
    * { box-sizing: border-box; }
    body {
      font-family: 'Courier New', Courier, monospace;
      font-size: 12px;
      line-height: 1.25;
      color: #000;
      margin: 0 auto;
      padding: 6px 10px;
      max-width: 58mm;
    }
    .text-center { text-align: center; }
    .bold { font-weight: bold; }
    .divider { border-bottom: 1px dashed #000; margin: 8px 0; }
    .row { display: flex; justify-content: space-between; gap: 10px; }
    .mono { font-family: inherit; }
    .small { font-size: 10px; }
    .title { font-size: 14px; letter-spacing: 0.5px; }
    .mt { margin-top: 8px; }
    .mb { margin-bottom: 6px; }
    .code { font-size: 13px; font-weight: bold; }
    .top-actions{
      position: fixed;
      top: 8px;
      right: 8px;
      display: flex;
      gap: 8px;
      z-index: 10;
    }
    .top-actions button{
      font: inherit;
      font-size: 11px;
      padding: 6px 8px;
      border: 1px solid #111;
      background: #fff;
      border-radius: 6px;
      cursor: pointer;
    }
    @media print {
      body { width: 100%; max-width: 100%; margin: 0; padding: 5mm 5mm 0 5mm; }
      .top-actions{ display:none; }
    }
  </style>
</head>
<body>
  <div class="top-actions">
    <button type="button" onclick="window.print()"><%= t('agent.print_thermal', 'Print Thermal') %></button>
    <button type="button" onclick="try{window.close()}catch(e){}">Close</button>
  </div>
  <div class="text-center">
    <div class="title bold"><%= company %></div>
    <% if (settings && settings.company_address) { %>
      <div class="small"><%= settings.company_address %></div>
    <% } %>
    <% const wa = (settings && settings.whatsapp_admin_numbers && settings.whatsapp_admin_numbers.length) ? settings.whatsapp_admin_numbers[0] : (settings && settings.company_phone ? settings.company_phone : ''); %>
    <% if (wa) { %>
      <div class="small">Telp/WA: <%= String(wa).startsWith('0') ? wa : ('+' + wa) %></div>
    <% } %>
  </div>

  <div class="divider"></div>

  <div class="text-center bold mb"><%= t('agent.receipt.voucher_title', 'STRUK VOUCHER HOTSPOT') %></div>

  <div class="small">
    <div class="row"><div><%= t('agent.receipt.transaction_no', 'No. Transaksi') %></div><div class="bold">#<%= tx.id %></div></div>
    <div class="row"><div><%= t('agent.receipt.date', 'Tanggal') %></div><div class="bold"><%= tx.created_at ? formatDateLocal(tx.created_at) : getNowLocal() %></div></div>
    <div class="row"><div><%= t('admin.nav.agents', 'Agent') %></div><div class="bold"><%= tx.agent_name || '' %><%= tx.agent_username ? (' (@' + tx.agent_username + ')') : '' %></div></div>
  </div>

  <div class="divider"></div>

  <div class="small">
    <div class="row"><div><%= t('agent.package', 'Paket') %></div><div class="bold"><%= tx.profile_name || '-' %></div></div>
    <% if (tx.router_name) { %>
      <div class="row"><div><%= t('agent.receipt.router', 'Router') %></div><div class="bold"><%= tx.router_name %></div></div>
    <% } %>
  </div>

  <div class="divider"></div>

  <div class="text-center mt">
    <div class="small"><%= t('agent.receipt.code', 'KODE VOUCHER') %></div>
    <div class="code"><%= tx.voucher_code || '-' %></div>
    <div class="small mt"><%= t('agent.receipt.password', 'PASSWORD') %></div>
    <div class="code"><%= tx.voucher_password || '-' %></div>
  </div>

  <div class="divider"></div>

  <div class="row bold">
    <div><%= t('agent.price', 'HARGA') %></div>
    <div>Rp <%= Number(tx.amount_sell || 0).toLocaleString('id-ID') %></div>
  </div>

  <div class="divider"></div>

  <div class="text-center small mt">
    <%= String(t('agent.receipt.thanks_voucher', 'Terima kasih.\nSimpan struk ini sebagai bukti transaksi.')).replace(/\n/g,'<br>') %>
  </div>

  <script>
    window.onload = function () {
      setTimeout(function () { window.print(); }, 350);
    };
  </script>
</body>
</html>
