<?php
// reporte_analytics.php - reporte diario de visitas y clicks
require_once __DIR__ . '/db_connect.php';
if (!isset($conn) || !($conn instanceof mysqli)) { die("No se pudo inicializar la conexión (db_connect.php)."); }
$conn->set_charset('utf8mb4');

$desde = $_GET['desde'] ?? date('Y-m-01');
$hasta = $_GET['hasta'] ?? date('Y-m-d');

function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

// Visitas diarias
$sqlVis = "SELECT DATE(fecha) dia, COUNT(*) visitas
           FROM visitas
           WHERE DATE(fecha) BETWEEN ? AND ?
           GROUP BY DATE(fecha)
           ORDER BY dia DESC";
$stmt = $conn->prepare($sqlVis);
$stmt->bind_param("ss",$desde,$hasta);
$stmt->execute();
$vis = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Eventos diarios (reservación + idiomas)
$sqlEv = "SELECT DATE(fecha) dia,
                 SUM(tipo='reservation_submit') reservaciones,
                 SUM(tipo='lang_change') cambios_idioma,
                 SUM(tipo='lang_open') aperturas_menu_idioma
          FROM eventos
          WHERE DATE(fecha) BETWEEN ? AND ?
          GROUP BY DATE(fecha)
          ORDER BY dia DESC";
$stmt = $conn->prepare($sqlEv);
$stmt->bind_param("ss",$desde,$hasta);
$stmt->execute();
$ev = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Index por día para combinar
$mapEv = [];
foreach($ev as $r){ $mapEv[$r['dia']]=$r; }

?>
<!doctype html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reporte Movitax · Visitas y Clicks</title>
<style>
  body{font-family:Arial, sans-serif;background:#0b1220;color:#e5e7eb;margin:0;padding:18px;}
  .card{background:#111a2d;border:1px solid #1f2a44;border-radius:14px;padding:14px;max-width:980px;margin:0 auto;}
  h1{margin:0 0 10px 0;font-size:18px;}
  form{display:flex;gap:10px;flex-wrap:wrap;align-items:end;margin:12px 0 14px 0;}
  label{font-size:12px;color:#cbd5e1;font-weight:800;display:block;margin-bottom:6px;}
  input{background:#0b1220;border:1px solid #233152;color:#e5e7eb;border-radius:10px;padding:9px 10px;}
  button{background:#f0b129;border:none;border-radius:10px;padding:10px 12px;font-weight:900;cursor:pointer;}
  table{width:100%;border-collapse:collapse;margin-top:10px;}
  th,td{padding:10px;border-bottom:1px solid #22304f;text-align:left;font-size:13px;}
  th{color:#cbd5e1;font-size:12px;text-transform:uppercase;letter-spacing:.04em;}
</style>
</head>
<body>
<div class="card">
  <h1>Reporte diario — Visitas y eventos</h1>
  <form method="get">
    <div>
      <label>Desde</label>
      <input type="date" name="desde" value="<?php echo h($desde); ?>">
    </div>
    <div>
      <label>Hasta</label>
      <input type="date" name="hasta" value="<?php echo h($hasta); ?>">
    </div>
    <button type="submit">Ver</button>
  </form>

  <table>
    <thead>
      <tr>
        <th>Día</th>
        <th>Visitas</th>
        <th>Clicks Reservación (submit)</th>
        <th>Menú Idiomas (open)</th>
        <th>Cambio Idioma (change)</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach($vis as $r):
        $dia = $r['dia'];
        $e = $mapEv[$dia] ?? ['reservaciones'=>0,'aperturas_menu_idioma'=>0,'cambios_idioma'=>0];
      ?>
      <tr>
        <td><?php echo h($dia); ?></td>
        <td><?php echo (int)$r['visitas']; ?></td>
        <td><?php echo (int)$e['reservaciones']; ?></td>
        <td><?php echo (int)$e['aperturas_menu_idioma']; ?></td>
        <td><?php echo (int)$e['cambios_idioma']; ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
</body>
</html>
