<?php
// paneladm.jatpluss.com/admin/dashboard.php
session_start();
if (empty($_SESSION['admin_id'])) { header('Location: login_admin.php'); exit; }

require_once __DIR__ . '/conexion.php';

/* --- m¨¦tricas --- */
$totCorreos     = $pdo->query("SELECT COUNT(*) FROM usuarios_correos")->fetchColumn();
$totPlataformas = $pdo->query("SELECT COUNT(DISTINCT platform) FROM email_subjects WHERE active=1")->fetchColumn();
$totAdmins      = $pdo->query("SELECT COUNT(*) FROM administradores")->fetchColumn();

/* --- mensajes flash --- */
$msg  = $_SESSION['exito_message'] ?? $_SESSION['error_message'] ?? '';
$type = isset($_SESSION['error_message']) ? 'danger' : 'success';
unset($_SESSION['exito_message'], $_SESSION['error_message']);
?>
<!DOCTYPE html>
<html lang="es"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Panel Admin Dashboard</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet">
  <link rel="icon" href="/images/logo/favicon.ico" type="image/x-icon">

<style>
body{background:#343a40;color:#fff}
.card-link{color:inherit;text-decoration:none}
.quick-card{background:linear-gradient(135deg,#e50914,#b20710);}
.quick-card:hover{transform:translateY(-3px);box-shadow:0 0.5rem 1rem rgba(0,0,0,.5);}
.stat-card{background:#212529;border:1px solid #464646}
.stat-card h2{font-size:2rem;margin:0}
.toast-container{position:fixed;top:1rem;right:1rem;z-index:1080}
body{background:#343a40;color:#fff}
.card-link{color:inherit;text-decoration:none}
.quick-card{background:linear-gradient(135deg,#e50914,#b20710);}
.quick-card:hover{transform:translateY(-3px);box-shadow:0 0.5rem 1rem rgba(0,0,0,.5);}
.stat-card{
  background:#212529;
  border:1px solid #464646;
  color:#fff;                 /* 👈 forzamos texto blanco */
}
.stat-card h2{font-size:2rem;margin:0}
.toast-container{position:fixed;top:1rem;right:1rem;z-index:1080}
</style>
</head><body>

<?php include 'navbar.php'; ?>

<div class="container py-4">

  <!-- toasts -->
  <div class="toast-container" id="toastBox"></div>

  <h1 class="h4 mb-4"><i class="fas fa-tachometer-alt me-1"></i> Dashboard</h1>

  <!-- accesos r¨¢pidos -->
  <div class="row g-4 mb-4">
    <div class="col-12 col-md-4">
      <a href="admin_list_correo.php" class="card-link">
        <div class="card quick-card text-center p-4 text-white">
          <i class="fas fa-envelope fa-2x mb-3"></i>
          <h5>Listar correos</h5>
        </div>
      </a>
    </div>
    <div class="col-12 col-md-4">
      <a href="admin_asignar_contrasena.php" class="card-link">
        <div class="card quick-card text-center p-4 text-white">
          <i class="fas fa-key fa-2x mb-3"></i>
          <h5>Registro de accesos</h5>
        </div>
      </a>
    </div>
    <div class="col-12 col-md-4">
      <a href="admin_asignar_masivo.php" class="card-link">
        <div class="card quick-card text-center p-4 text-white">
          <i class="fas fa-users fa-2x mb-3"></i>
          <h5>Registro masiva</h5>
        </div>
      </a>
    </div>
    <div class="col-12 col-md-4">
      <a href="subjects_admin.php" class="card-link">
        <div class="card quick-card text-center p-4 text-white">
          <i class="fas fa-users fa-2x mb-3"></i>
          <h5>Registro de Asuntos</h5>
        </div>
      </a>
    </div>
  </div>
  <!-- estad¨ªsticas -->
  <div class="row g-4">
    <div class="col-12 col-md-4">
      <div class="card stat-card text-center p-3">
        <div class="card-body">
          <i class="fas fa-database fa-lg mb-2 text-info"></i>
          <h2><?= $totCorreos ?></h2>
          <p class="mb-0">Correos registrados</p>
        </div>
      </div>
    </div>
    <div class="col-12 col-md-4">
      <div class="card stat-card text-center p-3">
        <div class="card-body">
          <i class="fas fa-tv fa-lg mb-2 text-warning"></i>
          <h2><?= $totPlataformas ?></h2>
          <p class="mb-0">Plataformas activas</p>
        </div>
      </div>
    </div>
    <div class="col-12 col-md-4">
      <div class="card stat-card text-center p-3">
        <div class="card-body">
          <i class="fas fa-user-shield fa-lg mb-2 text-success"></i>
          <h2><?= $totAdmins ?></h2>
          <p class="mb-0">Administradores</p>
        </div>
      </div>
    </div>
  </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
<?php if($msg): ?>
// mostrar toast inicial
const t=document.createElement('div');
t.className='toast text-bg-<?= $type ?>';
t.innerHTML=`<div class="d-flex">
  <div class="toast-body"><?= addslashes($msg) ?></div>
  <button class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
</div>`;
document.getElementById('toastBox').appendChild(t);
new bootstrap.Toast(t,{delay:3000,autohide:true}).show();
<?php endif;?>
</script>
<?php include __DIR__ . '/footer.php'; ?>

</body></html>
