<?php
// ============================================================
// admin-stats.php — RBE Utilities Admin Quick Stats (modular)
// ============================================================

require_once __DIR__ . '/core/db_connect.php';

// Protect via session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// ------------------------------------------------------------
// Count Data
// ------------------------------------------------------------
$landlords     = $conn_rbe->query("SELECT COUNT(*) FROM rbe_roles WHERE role = 'landlord'")->fetchColumn();
$tenants       = $conn_rbe->query("SELECT COUNT(*) FROM rbe_roles WHERE role = 'tenant'")->fetchColumn();
$agents        = $conn_rbe->query("SELECT COUNT(*) FROM rbe_roles WHERE role = 'agent'")->fetchColumn();
$properties    = $conn_rbe->query("SELECT COUNT(*) FROM properties")->fetchColumn();
$flecto_meters = $conn_rbe->query("SELECT COUNT(*) FROM flecto_meters")->fetchColumn();
$iammeters     = $conn_rbe->query("SELECT COUNT(*) FROM smart_meters")->fetchColumn();
$water_meters  = $conn_rbe->query("SELECT COUNT(*) FROM water_meters")->fetchColumn();
$total_meters  = $flecto_meters + $iammeters + $water_meters;
?>

<!-- ==========================================================
     QUICK STATS MODULE
     ========================================================== -->
<main class="dashboard-content">
<div class="section quick-stats-section">
  <h2>📊 Quick Stats Overview</h2>

  <div class="quick-stats-grid">
    <div class="stat-card">
      <div class="icon">👔</div>
      <h4>Agents</h4>
      <p><?= $agents ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">🏠</div>
      <h4>Landlords</h4>
      <p><?= $landlords ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">👥</div>
      <h4>Tenants</h4>
      <p><?= $tenants ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">🏢</div>
      <h4>Properties</h4>
      <p><?= $properties ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">⚡</div>
      <h4>IAMMETER Meters</h4>
      <p><?= $iammeters ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">🔌</div>
      <h4>FLECTO Meters</h4>
      <p><?= $flecto_meters ?></p>
    </div>

    <div class="stat-card">
      <div class="icon">💧</div>
      <h4>Water Meters</h4>
      <p><?= $water_meters ?></p>
    </div>

    <div class="stat-card highlight">
      <div class="icon">🧮</div>
      <h4>Total Meters</h4>
      <p><?= $total_meters ?></p>
    </div>
  </div>
</div>
</main>

<!-- ==========================================================
     STYLING — ensures correct position next to sidebar
     ========================================================== -->
<style>
/* ---------- Base Layout Alignment ---------- */
.dashboard-content {
  margin-left: 240px; /* same as sidebar width */
  padding: 0px 30px;
  color: #e6f5ff;
  box-sizing: border-box;
}

.section.quick-stats-section {
  background: #0b2333;
  border-radius: 16px;
  padding: 25px;
  margin: -85px auto;
  box-shadow: 0 0 18px rgba(0, 0, 0, 0.4);
  max-width: 1200px;
}

.quick-stats-section h2 {
  color: var(--accent2, #00ccff);
  text-align: center;
  margin-bottom: 25px;
}

/* ---------- Grid Layout ---------- */
.quick-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 20px;
  justify-items: center;
}

/* ---------- Stat Card ---------- */
.stat-card {
  background: linear-gradient(180deg, #09263a, #041622);
  border-radius: 12px;
  text-align: center;
  padding: 20px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.4);
  transition: transform 0.25s ease, box-shadow 0.25s ease;
  width: 100%;
  max-width: 200px;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 0 18px rgba(0,255,255,0.3), inset 0 0 10px rgba(0,255,255,0.05);
}

.stat-card .icon {
  font-size: 28px;
  margin-bottom: 8px;
}

.stat-card h4 {
  color: var(--accent2, #00ccff);
  margin: 5px 0;
  font-size: 1.05em;
}

.stat-card p {
  font-size: 1.6em;
  font-weight: 700;
  margin: 0;
  color: var(--accent, #7ed957);
}

/* ---------- Highlight Card ---------- */
.stat-card.highlight {
  background: linear-gradient(180deg, #7ed957, #5ac13a);
  color: #041622;
  box-shadow: 0 0 18px rgba(126, 217, 87, 0.5);
  animation: pulse 2s infinite alternate;
}
.stat-card.highlight h4 { color: #041622; }
.stat-card.highlight p { color: #000; }

@keyframes pulse {
  from { box-shadow: 0 0 15px rgba(126,217,87,0.4); }
  to { box-shadow: 0 0 25px rgba(126,217,87,0.8); }
}

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .dashboard-content { margin-left: 80px; padding: 25px 15px; }
}
@media (max-width: 600px) {
  .quick-stats-grid { grid-template-columns: repeat(2, 1fr); }
  .stat-card { padding: 15px; }
  .stat-card h4 { font-size: 0.95em; }
  .stat-card p { font-size: 1.3em; }
}
</style>

<!-- ==========================================================
     COUNT-UP ANIMATION
     ========================================================== -->
<script>
document.addEventListener("DOMContentLoaded", () => {
  document.querySelectorAll(".stat-card p").forEach(el => {
    const target = parseInt(el.textContent.trim()) || 0;
    el.textContent = "0";
    let n = 0;
    const step = Math.ceil(target / 40);
    const interval = setInterval(() => {
      n += step;
      el.textContent = n > target ? target : n;
      if (n >= target) clearInterval(interval);
    }, 25);
  });
});
</script>
