<div class="table-responsive">
  <style>
    .table { background:#fff; border-collapse:separate; border-spacing:0; }
    .table thead th { background:#0f172a; color:#fff; border-color:#0f172a; }
    .table tbody tr:hover { background:#f8fafc; }
    .table td,.table th { vertical-align:middle; padding:.5rem .6rem; font-variant-numeric:tabular-nums; }
    .cell-right{ text-align:right; }
    .cell-left{ text-align:left; }
    tfoot .total-row{ background:#ecfdf5; font-weight:700; }
    .badge-range{ display:inline-block; margin-bottom:10px; padding:.25rem .5rem; border-radius:999px; background:#eef2ff; color:#312e81; border:1px solid #e0e7ff; font-weight:600; }
  </style>

  <?php $rekap = $rekap ?? []; ?>
  <div class="badge-range">Periode: <?= htmlspecialchars($range_label ?? '-', ENT_QUOTES); ?></div>

  <table class="table table-bordered table-hover table-sm">
    <thead>
      <tr>
        <th class="cell-left">User</th>
        <th class="cell-right">Penjualan Tunai</th>
        <th class="cell-right">Penjualan Transfer</th>
        <th class="cell-right">Total Penjualan</th>
        <th class="cell-right">Pengeluaran</th>
        <th class="cell-right">Pembelian</th>
        <th class="cell-right">Total Uang Keluar</th>
        <th class="cell-right">Saldo</th>
      </tr>
    </thead>
    <tbody>
      <?php
      $gt_tunai = $gt_transfer = $gt_penjualan = $gt_keluar = $gt_pengeluaran = $gt_pembelian = $gt_saldo = 0;
      if (!empty($rekap)) {
        foreach ($rekap as $r) {
          $gt_tunai      += (float)$r['penjualan_tunai'];
          $gt_transfer   += (float)$r['penjualan_transfer'];
          $gt_penjualan  += (float)$r['penjualan_total'];
          $gt_pengeluaran+= (float)$r['pengeluaran'];
          $gt_pembelian  += (float)$r['pembelian'];
          $gt_keluar     += (float)$r['uang_keluar'];
          $gt_saldo      += (float)$r['saldo'];
          ?>
          <tr>
            <td class="cell-left"><?= htmlspecialchars($r['user'], ENT_QUOTES); ?></td>
            <td class="cell-right"><?= rp($r['penjualan_tunai']); ?></td>
            <td class="cell-right"><?= rp($r['penjualan_transfer']); ?></td>
            <td class="cell-right"><b><?= rp($r['penjualan_total']); ?></b></td>
            <td class="cell-right"><?= rp($r['pengeluaran']); ?></td>
            <td class="cell-right"><?= rp($r['pembelian']); ?></td>
            <td class="cell-right"><b><?= rp($r['uang_keluar']); ?></b></td>
            <td class="cell-right"><b><?= rp($r['saldo']); ?></b></td>
          </tr>
          <?php
        }
      } else {
        echo '<tr><td colspan="8" class="cell-left">Tidak ada data pada periode ini.</td></tr>';
      }
      ?>
    </tbody>
    <tfoot>
      <tr class="total-row">
        <td class="cell-left">TOTAL</td>
        <td class="cell-right"><?= rp($gt_tunai); ?></td>
        <td class="cell-right"><?= rp($gt_transfer); ?></td>
        <td class="cell-right"><?= rp($gt_penjualan); ?></td>
        <td class="cell-right"><?= rp($gt_pengeluaran); ?></td>
        <td class="cell-right"><?= rp($gt_pembelian); ?></td>
        <td class="cell-right"><?= rp($gt_keluar); ?></td>
        <td class="cell-right"><?= rp($gt_saldo); ?></td>
      </tr>
    </tfoot>
  </table>
</div>
