<?php
session_start();
require_once __DIR__ . '/db.php';


if (isset($_GET['action']) && $_GET['action'] === 'logout') {
  session_destroy();
  header('Location: index.php');
  exit;
}

if (empty($_SESSION['admin_id'])) {
  header('Location: index.php');
  exit;
}

$total_ruletas = (int)$conn->query("SELECT COUNT(*) c FROM ruletas")->fetch_assoc()['c'];
$total_premios  = (int)$conn->query("SELECT COUNT(*) c FROM premios")->fetch_assoc()['c'];
$total_jugadas  = (int)$conn->query("SELECT COUNT(*) c FROM jugadas")->fetch_assoc()['c'];

$ultimas = $conn->query("
  SELECT j.id, j.fecha_juego, r.nombre AS ruleta, p.nombre AS premio, j.ip_address
  FROM jugadas j
  LEFT JOIN ruletas r ON r.id = j.ruleta_id
  LEFT JOIN premios p ON p.id = j.premio_id
  ORDER BY j.id DESC
  LIMIT 10
");

// --- contadores para Memoria ---
$total_mem_juegos  = (int)$conn->query("SELECT COUNT(*) c FROM juegos_config")->fetch_assoc()['c'];
$total_mem_cartas  = (int)$conn->query("SELECT COUNT(*) c FROM cartas WHERE activo=1")->fetch_assoc()['c'];
$total_mem_jugadas = (int)$conn->query("SELECT COUNT(*) c FROM jugadas_memoria")->fetch_assoc()['c'];

// últimas 10 jugadas de memoria
$ult_mem = $conn->query("
  SELECT jm.id, jm.fecha_juego, jm.juego_id, jm.tiempo_ms, jm.aciertos, gc.nombre AS juego
  FROM jugadas_memoria jm
  LEFT JOIN juegos_config gc ON gc.juego_id = jm.juego_id
  ORDER BY jm.id DESC
  LIMIT 10
");

// --- contadores para Rasca y Gana ---
$total_rasca_juegos   = (int)$conn->query("SELECT COUNT(*) c FROM rasca_config")->fetch_assoc()['c'];
$total_rasca_premios  = (int)$conn->query("SELECT COUNT(*) c FROM rasca_premios WHERE activo=1")->fetch_assoc()['c'];
$total_rasca_jugadas  = (int)$conn->query("SELECT COUNT(*) c FROM jugadas_rasca")->fetch_assoc()['c'];

// últimas 10 jugadas de Rasca
$ult_rasca = $conn->query("
  SELECT jr.id,
         jr.fecha_juego,
         rc.nombre       AS rasca,
         COALESCE(rp.nombre, '—') AS premio,
         jr.ip_address   AS ip
  FROM jugadas_rasca jr
  LEFT JOIN rasca_config  rc ON rc.rasca_id = jr.rasca_id
  LEFT JOIN rasca_premios rp ON rp.id       = jr.premio_id
  ORDER BY jr.id DESC
  LIMIT 10
");


?>
<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <title>Dashboard | Admin Ruletas</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Home</a>
    <div class="d-flex">
      <span class="navbar-text me-3">
        <?= htmlspecialchars($_SESSION['admin_nombre'] ?? $_SESSION['admin_email']) ?>
      </span>
      <a href="?action=logout" class="btn btn-outline-light btn-sm">Salir</a>
    </div>
  </div>
</nav>

<div class="container py-4">
  <?php include __DIR__ . '/dashboard_landing_widgets_adv.php'; ?>
  <h5 class="mt-4">Juegos Disponibles</h5>

  <div class="row g-3">
    <div class="col-md-4"><div class="card shadow-sm"><div class="card-body">
      <h6 class="text-muted mb-1">Ruletas</h6>
      <div class="display-6"><?= $total_ruletas ?></div>
      <a href="ruletas.php" class="btn btn-sm btn-primary mt-2">Gestionar ruletas</a>
    </div></div></div>

    <div class="col-md-4"><div class="card shadow-sm"><div class="card-body">
      <h6 class="text-muted mb-1">Premios</h6>
      <div class="display-6"><?= $total_premios ?></div>
      <a href="premios.php" class="btn btn-sm btn-primary mt-2">Gestionar premios</a>
    </div></div></div>

    <div class="col-md-4"><div class="card shadow-sm"><div class="card-body">
      <h6 class="text-muted mb-1">Jugadas</h6>
      <div class="display-6"><?= $total_jugadas ?></div>
      <a href="jugadas.php" class="btn btn-sm btn-primary mt-2">Ver jugadas</a>
    </div></div></div>
    
    <div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Juegos de Memoria</h6>
    <div class="display-6"><?= $total_mem_juegos ?></div>
    <a href="juegos_config.php" class="btn btn-sm btn-primary mt-2">Configurar juegos</a>
  </div></div>
</div>

<div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Cartas activas</h6>
    <div class="display-6"><?= $total_mem_cartas ?></div>
    <a href="cartas.php" class="btn btn-sm btn-primary mt-2">Gestionar cartas</a>
  </div></div>
</div>

<div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Jugadas Memoria</h6>
    <div class="display-6"><?= $total_mem_jugadas ?></div>
    <a href="jugadas_memoria.php" class="btn btn-sm btn-primary mt-2">Ver jugadas</a>
  </div></div>
</div>

   <div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Juegos de Rasca y Gana</h6>
    <div class="display-6"><?= $total_rasca_juegos ?></div>
    <a href="rasca_config.php" class="btn btn-sm btn-primary mt-2">Configurar juegos</a>
  </div></div>
</div>

<div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Premios de Rasca y Gana</h6>
    <div class="display-6"><?= $total_rasca_premios ?></div>
    <a href="rasca_premios.php" class="btn btn-sm btn-primary mt-2">Gestionar premios</a>
  </div></div>
</div>

<div class="col-md-4">
  <div class="card shadow-sm"><div class="card-body">
    <h6 class="text-muted mb-1">Jugadas de Rasca y Gana</h6>
    <div class="display-6"><?= $total_rasca_jugadas ?></div>
    <a href="rasca_jugadas.php" class="btn btn-sm btn-primary mt-2">Ver jugadas</a>
  </div></div>
</div>
  </div>
  <h5 class="mt-4">Últimas 10 jugadas</h5>
  <div class="card shadow-sm">
    <div class="card-body table-responsive">
      <table class="table table-sm align-middle">
        <thead><tr><th>ID</th><th>Fecha</th><th>Ruleta</th><th>Premio</th><th>IP</th></tr></thead>
        <tbody>
          <?php while($row = $ultimas->fetch_assoc()): ?>
          <tr>
            <td><?= (int)$row['id'] ?></td>
            <td><?= htmlspecialchars($row['fecha_juego']) ?></td>
            <td><?= htmlspecialchars($row['ruleta'] ?? '—') ?></td>
            <td><?= htmlspecialchars($row['premio'] ?? '—') ?></td>
            <td><?= htmlspecialchars($row['ip_address'] ?? '—') ?></td>
          </tr>
          <?php endwhile; ?>
          <?php if ($ultimas->num_rows === 0): ?>
          <tr><td colspan="5" class="text-center text-muted">Sin registros</td></tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <h5 class="mt-4">Últimas 10 jugadas — Memoria</h5>
<div class="card shadow-sm">
  <div class="card-body table-responsive">
    <table class="table table-sm align-middle">
      <thead>
        <tr><th>ID</th><th>Fecha</th><th>Juego</th><th>Juego ID</th><th>Aciertos</th><th>Tiempo (s)</th></tr>
      </thead>
      <tbody>
        <?php if ($ult_mem && $ult_mem->num_rows): ?>
          <?php while($m = $ult_mem->fetch_assoc()): ?>
          <tr>
            <td><?= (int)$m['id'] ?></td>
            <td><?= htmlspecialchars($m['fecha_juego']) ?></td>
            <td><?= htmlspecialchars($m['juego'] ?? '—') ?></td>
            <td><?= (int)$m['juego_id'] ?></td>
            <td><?= (int)$m['aciertos'] ?></td>
            <td><?= number_format(($m['tiempo_ms'] ?? 0)/1000, 2) ?></td>
          </tr>
          <?php endwhile; ?>
        <?php else: ?>
          <tr><td colspan="6" class="text-center text-muted">Sin registros</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<div class="card shadow-sm mt-4">
  <div class="card-body table-responsive">
    <h5 class="mb-3">Últimas 10 jugadas (Rasca)</h5>
    <table class="table table-sm table-striped align-middle">
      <thead>
        <tr>
          <th>ID</th>
          <th>Fecha</th>
          <th>Rasca</th>
          <th>Premio</th>
          <th>IP</th>
        </tr>
      </thead>
      <tbody>
        <?php while($row = $ult_rasca->fetch_assoc()): ?>
          <tr>
            <td><?= (int)$row['id'] ?></td>
            <td><?= htmlspecialchars($row['fecha_juego']) ?></td>
            <td><?= htmlspecialchars($row['rasca'] ?? '—') ?></td>
            <td><?= htmlspecialchars($row['premio'] ?? '—') ?></td>
            <td><?= htmlspecialchars($row['ip'] ?? '') ?></td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  </div>
</div>


</div>
</body>
</html>
