<?php
if (count(get_included_files()) === 1) { http_response_code(403); exit('Forbidden'); }
$page_title = 'Vale Admin — Vendedores';

ob_start();
?>
<div class="dashboard-container">
  <?php $adminNav = 'sellers'; require __DIR__ . '/sidebar.php'; ?>

  <main class="main-content">
    <header class="header-bar"><h2>Vendedores</h2></header>

    <div style="flex:1;overflow-y:auto;padding:1.5rem 2rem">

      <?php if (!empty($notice)): ?>
        <div class="alert" style="background:rgba(30,122,77,.1);color:var(--color-verde);border:1px solid rgba(30,122,77,.2)"><?= htmlspecialchars($notice) ?></div>
      <?php endif; ?>
      <?php if (!empty($error)): ?>
        <div class="alert alert-error"><?= htmlspecialchars($error) ?></div>
      <?php endif; ?>

      <!-- Crear vendedor -->
      <details style="background:#fff;border:1px solid var(--color-border);border-radius:10px;padding:1rem 1.25rem;margin-bottom:1.5rem" <?= !empty($openForm) ? 'open' : '' ?>>
        <summary style="cursor:pointer;font-weight:600;color:var(--color-barro)">+ Agregar vendedor</summary>
        <form method="POST" action="/admin/sellers" style="margin-top:1rem;display:grid;grid-template-columns:1fr 1fr;gap:.8rem">
          <input type="hidden" name="action" value="create_seller">
          <label style="font-size:.8rem">Nombre*
            <input type="text" name="nombre" required class="form-control">
          </label>
          <label style="font-size:.8rem">Email*
            <input type="email" name="email" required class="form-control">
          </label>
          <label style="font-size:.8rem">Teléfono
            <input type="text" name="telefono" class="form-control">
          </label>
          <label style="font-size:.8rem">Comisión setup %
            <input type="number" name="comision_setup_pct" value="35" min="0" max="100" step="0.01" class="form-control">
          </label>
          <label style="font-size:.8rem">Comisión MRR %
            <input type="number" name="comision_mrr_pct" value="15" min="0" max="100" step="0.01" class="form-control">
          </label>
          <label style="font-size:.8rem">Meses de comisión MRR
            <input type="number" name="comision_meses" value="12" min="1" max="60" class="form-control">
          </label>
          <label style="font-size:.8rem">Contraseña (login)*
            <input type="password" name="password" required class="form-control">
          </label>
          <div style="grid-column:1/3">
            <button type="submit" class="btn btn-primary">Crear vendedor</button>
          </div>
        </form>
      </details>

      <table style="width:100%;border-collapse:collapse;background:#fff;border-radius:12px;overflow:hidden;font-size:.84rem;box-shadow:0 1px 4px rgba(0,0,0,.05)">
        <thead>
          <tr style="background:var(--color-tierra);color:#FBF6EE;text-align:left">
            <th style="padding:.65rem 1rem">Vendedor</th>
            <th style="padding:.65rem 1rem">Comisión setup</th>
            <th style="padding:.65rem 1rem">Comisión MRR</th>
            <th style="padding:.65rem 1rem">Meses</th>
            <th style="padding:.65rem 1rem">Negocios</th>
            <th style="padding:.65rem 1rem">MRR total</th>
            <th style="padding:.65rem 1rem">Estado</th>
            <th style="padding:.65rem 1rem">Acciones</th>
          </tr>
        </thead>
        <tbody>
          <?php if (empty($sellers)): ?>
            <tr><td colspan="8" style="padding:2rem;text-align:center;color:var(--color-text-muted)">Sin vendedores todavía.</td></tr>
          <?php else: foreach ($sellers as $s): ?>
          <tr style="border-top:1px solid var(--color-border)">
            <td style="padding:.65rem 1rem">
              <div style="font-weight:700"><?= htmlspecialchars($s['nombre']) ?></div>
              <div style="font-size:.75rem;color:var(--color-text-muted)"><?= htmlspecialchars($s['email']) ?></div>
              <?php if ($s['telefono']): ?>
                <div style="font-size:.72rem;color:var(--color-text-muted)"><?= htmlspecialchars($s['telefono']) ?></div>
              <?php endif; ?>
            </td>
            <td style="padding:.65rem 1rem"><?= number_format($s['comision_setup_pct'], 1) ?>%</td>
            <td style="padding:.65rem 1rem"><?= number_format($s['comision_mrr_pct'], 1) ?>%</td>
            <td style="padding:.65rem 1rem"><?= (int)$s['comision_meses'] ?></td>
            <td style="padding:.65rem 1rem"><?= (int)($s['total_tenants'] ?? 0) ?></td>
            <td style="padding:.65rem 1rem;font-weight:700;color:var(--color-verde)">$<?= number_format($s['mrr'] ?? 0, 0) ?></td>
            <td style="padding:.65rem 1rem">
              <span style="background:<?= $s['activo']?'rgba(30,122,77,.1)':'rgba(192,57,43,.1)' ?>;color:<?= $s['activo']?'#1E7A4D':'#C0392B' ?>;font-size:.7rem;font-weight:700;border-radius:99px;padding:2px 9px">
                <?= $s['activo'] ? 'Activo' : 'Inactivo' ?>
              </span>
            </td>
            <td style="padding:.65rem 1rem;display:flex;gap:.5rem;align-items:center">
              <a href="/admin/seller?id=<?= (int)$s['id'] ?>" style="font-size:.78rem;color:var(--color-barro);font-weight:600;text-decoration:none">Ver →</a>
              <form method="POST" action="/admin/sellers" style="display:inline">
                <input type="hidden" name="action" value="toggle_seller">
                <input type="hidden" name="seller_id" value="<?= (int)$s['id'] ?>">
                <button type="submit" style="font-size:.72rem;background:none;border:1px solid var(--color-border);border-radius:6px;padding:.25rem .6rem;cursor:pointer;font-family:var(--font-body);color:var(--color-text-muted)">
                  <?= $s['activo'] ? 'Desactivar' : 'Activar' ?>
                </button>
              </form>
            </td>
          </tr>
          <?php endforeach; endif; ?>
        </tbody>
      </table>

    </div>
  </main>
</div>

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