<?php
// ruleta/admin/landing_forms.php
session_start();
require_once __DIR__ . '/db.php';
if (empty($_SESSION['admin_id'])) { header('Location: ../index.php'); exit; }

$landing_id = (int)($_GET['landing_id'] ?? 0);
if ($landing_id <= 0) { die('landing_id requerido'); }

$landing = $conn->query("SELECT id,titulo FROM landing_pages WHERE id={$landing_id}")->fetch_assoc();
if (!$landing) { die('Landing no existe'); }

$from = trim($_GET['from'] ?? '');
$to   = trim($_GET['to'] ?? '');

$where = "WHERE lf.landing_id={$landing_id}";
$params = []; $types = '';
if ($from !== '') { $where .= " AND lf.created_at >= ?"; $params[] = $from.' 00:00:00'; $types.='s'; }
if ($to   !== '') { $where .= " AND lf.created_at <= ?"; $params[] = $to  .' 23:59:59'; $types.='s'; }

$sql = "SELECT lf.id, lf.created_at, lf.nombre, lf.email, lf.telefono, lf.mensaje, lf.ip_address
        FROM landing_forms lf
        {$where}
        ORDER BY lf.id DESC
        LIMIT 500";
$st = $conn->prepare($sql);
if (!empty($params)) { $st->bind_param($types, ...$params); }
$st->execute();
$list = $st->get_result();
?>
<!doctype html>
<html lang="es">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Landing Leads</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>.msg{max-width:420px;white-space:pre-wrap}</style>
</head>
<body>
<nav class="navbar navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="dashboard.php">Admin</a>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-light btn-sm" href="landing.php">Landings</a>
      <a class="btn btn-outline-light btn-sm" href="landing_crm.php?landing_id=<?= (int)$landing_id ?>">CRM Hooks</a>
      <a class="btn btn-light btn-sm" href="landing_forms.php?landing_id=<?= (int)$landing_id ?>">Leads</a>
      <a class="btn btn-outline-light btn-sm" href="../index.php?action=logout">Salir</a>
    </div>
  </div>
</nav>

<div class="container py-4">
  <div class="mb-3">
    <a class="btn btn-secondary btn-sm" href="landing.php">← Volver a Landings</a>
  </div>

  <h4 class="mb-3">Leads: <strong><?= htmlspecialchars($landing['titulo']) ?></strong></h4>

  <form class="row g-2 mb-3" method="get">
    <input type="hidden" name="landing_id" value="<?= (int)$landing_id ?>">
    <div class="col-md-3">
      <label class="form-label">Desde</label>
      <input type="date" class="form-control" name="from" value="<?= htmlspecialchars($from) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">Hasta</label>
      <input type="date" class="form-control" name="to" value="<?= htmlspecialchars($to) ?>">
    </div>
    <div class="col-md-6 d-flex align-items-end gap-2">
      <button class="btn btn-primary">Filtrar</button>
      <a class="btn btn-success" target="_blank"
         href="landing_forms_export.php?landing_id=<?= (int)$landing_id ?>&from=<?= urlencode($from) ?>&to=<?= urlencode($to) ?>">
         Exportar CSV
      </a>
    </div>
  </form>

  <div class="card shadow-sm"><div class="card-body table-responsive">
    <table class="table table-sm table-striped align-middle">
      <thead><tr><th>ID</th><th>Fecha</th><th>Nombre</th><th>Email</th><th>Teléfono</th><th>Mensaje</th><th>IP</th></tr></thead>
      <tbody>
        <?php while($r=$list->fetch_assoc()): ?>
          <tr>
            <td><?= (int)$r['id'] ?></td>
            <td><?= htmlspecialchars($r['created_at']) ?></td>
            <td><?= htmlspecialchars($r['nombre']) ?></td>
            <td><?= htmlspecialchars($r['email']) ?></td>
            <td><?= htmlspecialchars($r['telefono']) ?></td>
            <td class="msg"><?= htmlspecialchars($r['mensaje']) ?></td>
            <td><?= htmlspecialchars($r['ip_address']) ?></td>
          </tr>
        <?php endwhile; if(!$list || !$list->num_rows): ?>
          <tr><td colspan="7" class="text-center text-muted">Sin leads</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div></div>
</div>
</body>
</html>
