<?php
session_start();
require __DIR__ . '/../../config/auth.php';
require __DIR__ . '/../../config/db2.php';
require __DIR__ . '/../../lib/helpers.php';

// ── Acciones ──────────────────────────────────────────────────────────────────
if (isset($_GET['eliminar'])) {
    $pdo->prepare("UPDATE roles_juez SET deleted_at=NOW() WHERE id=?")->execute([(int)$_GET['eliminar']]);
    header('Location: listarRolesJuez.php');
    exit;
}

// ── Filtros ───────────────────────────────────────────────────────────────────
$fNombre = trim($_GET['f_nombre'] ?? '');
$fActivo = $_GET['f_activo'] ?? '';

$where = ["deleted_at IS NULL"];
$params = [];
if ($fNombre !== '') { $where[] = "nombre LIKE ?"; $params[] = "%$fNombre%"; }
if ($fActivo !== '') { $where[] = "activo = ?"; $params[] = (int)$fActivo; }

$sql = "SELECT * FROM roles_juez WHERE " . implode(' AND ', $where) . " ORDER BY nombre";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$paginaActual = 'configuracion';
require __DIR__ . '/../../config/menu.php';
?>
<div class="ph">
  <div>
    <h1>Roles de Juez</h1>
    <div class="bc"><a href="listarRolesJuez.php">Configuración</a> › Roles de Juez</div>
  </div>
  <a href="crearRolJuez.php" class="btn btn-p">+ Nuevo Rol</a>
</div>

<?php if (isset($_GET['ok'])): ?>
<div class="msg msg-ok">Guardado correctamente.</div>
<?php endif; ?>

<form id="fForm" method="get" class="fb">
  <div class="fi"><label>Nombre</label>
    <input type="text" name="f_nombre" value="<?=htmlspecialchars($fNombre)?>">
  </div>
  <div class="fi"><label>Activo</label>
    <select name="f_activo">
      <option value="">Todos</option>
      <option value="1" <?=$fActivo==='1'?'selected':''?>>Sí</option>
      <option value="0" <?=$fActivo==='0'?'selected':''?>>No</option>
    </select>
  </div>
  <div class="fi" style="align-self:flex-end">
    <button type="submit" class="btn btn-p">Buscar</button>
    <a href="listarRolesJuez.php" class="btn btn-g">Limpiar</a>
  </div>
</form>

<p style="font-size:12px;color:#64748b;margin-bottom:8px"><?=count($rows)?> resultado(s)</p>

<div class="tw"><table>
  <thead><tr>
    <th>Nombre</th>
    <th>Descripción</th>
    <th>Activo</th>
    <th style="width:100px">Acciones</th>
  </tr></thead>
  <tbody>
    <?php foreach ($rows as $r): ?>
    <tr>
      <td><?=htmlspecialchars($r['nombre'])?></td>
      <td><?=htmlspecialchars($r['descripcion'] ?? '')?></td>
      <td><?=$r['activo'] ? '<span style="color:#16a34a;font-weight:600">Sí</span>' : '<span style="color:#dc2626">No</span>'?></td>
      <td class="ac">
        <a href="crearRolJuez.php?id=<?=$r['id']?>" class="btn btn-i btn-sm">Editar</a>
        <a href="?eliminar=<?=$r['id']?>" class="btn btn-r btn-sm"
           onclick="return confirm('¿Eliminar rol?')">Eliminar</a>
      </td>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table></div>

<script>
(function(){
  const f = document.getElementById('fForm');
  f.querySelectorAll('select').forEach(el => el.addEventListener('change', () => f.submit()));
  f.querySelectorAll('input[type=text]').forEach(el => {
    el.addEventListener('keydown', e => { if (e.key === 'Enter') { e.preventDefault(); f.submit(); } });
    el.addEventListener('blur', () => f.submit());
  });
})();
</script>
</div></body></html>
