<?php
if (count(get_included_files()) === 1) { http_response_code(403); exit('Forbidden'); }
$page_title = 'Mis comisiones';
$sellerNav  = 'commissions';

ob_start();
?>

<div class="kpi-row">
  <div class="kpi"><div class="kv">$<?= number_format($totalPend, 2) ?></div><div class="kl">Pendiente de cobro</div></div>
  <div class="kpi"><div class="kv">$<?= number_format($totalPagado, 2) ?></div><div class="kl">Total cobrado</div></div>
  <div class="kpi"><div class="kv"><?= count($commissions) ?></div><div class="kl">Movimientos</div></div>
</div>

<?php if (!empty($totalPend)): ?>
<div style="background:rgba(224,163,62,.1);border:1px solid rgba(224,163,62,.35);border-radius:10px;padding:.85rem 1.2rem;margin-bottom:1.25rem;font-size:.85rem;color:#8A5E00">
  Tenés <strong>$<?= number_format($totalPend, 2) ?></strong> pendiente de cobro. Contactá al administrador para gestionar el pago.
</div>
<?php endif; ?>

<h3 style="font-family:var(--font-headings);margin:0 0 1rem;font-size:1.05rem">Historial de comisiones</h3>

<table class="tbl">
  <thead>
    <tr>
      <th>Negocio</th>
      <th>Tipo</th>
      <th>Período</th>
      <th>Monto</th>
      <th>Estado</th>
      <th>Fecha</th>
    </tr>
  </thead>
  <tbody>
    <?php if (empty($commissions)): ?>
      <tr><td colspan="6" style="text-align:center;padding:2rem;color:var(--color-text-muted)">Sin comisiones todavía. Activá tu primer negocio.</td></tr>
    <?php else: foreach ($commissions as $c): ?>
      <tr>
        <td style="font-weight:600"><?= htmlspecialchars($c['tenant_name'] ?? '#'.$c['tenant_id']) ?></td>
        <td><span class="badge badge-<?= $c['tipo'] ?>"><?= strtoupper($c['tipo']) ?></span></td>
        <td style="font-size:.8rem"><?= $c['periodo'] ? htmlspecialchars($c['periodo']) : '—' ?></td>
        <td style="font-weight:700;color:var(--color-verde)">$<?= number_format($c['monto'], 2) ?></td>
        <td><span class="badge badge-<?= $c['pagada'] ? 'paid' : 'pending' ?>"><?= $c['pagada'] ? 'Pagada' : 'Pendiente' ?></span></td>
        <td style="font-size:.78rem;color:var(--color-text-muted)"><?= date('d/m/Y', strtotime($c['created_at'])) ?></td>
      </tr>
    <?php endforeach; endif; ?>
  </tbody>
</table>

<?php
$content = ob_get_clean();
require __DIR__ . '/layout.php';
