<?php
session_start();
ob_start();

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

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

$stmt = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id = ? LIMIT 1");
$stmt->execute([ (int)$_SESSION['user_id'] ]);
if ($stmt->fetchColumn() !== 'admin') { echo "Access denied."; exit(); }

// ---- INCLUDE NAV ----
include 'admin-nav.php';

// Helper to compute status
function compute_status($ts) {
    if (!$ts || $ts === "0000-00-00 00:00:00") return "GRAY";

    $t = strtotime($ts);
    if (!$t) return "GRAY";

    $diffH = (time() - $t) / 3600;

    if ($diffH <= 2)  return "GREEN";
    if ($diffH <= 24) return "YELLOW";
    return "RED";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Admin Meters List</title>

  <link rel="stylesheet" href="/assets/css/theme.css">
  <link rel="stylesheet" href="/assets/css/admin.css">
  <style>
      .status-dot {
          display:inline-block;
          width:10px;height:10px;border-radius:50%;
          margin-left:4px;box-shadow:0 0 4px currentColor;
      }
  </style>
</head>
<body>

<div class="wrap">
  <h1>Admin Meters</h1>

  <a href="admin-meters.php" class="btn-manage">⚙️ Meter Management</a>
  <br><br>

  <div class="filters">
    <select id="filterLandlord">
      <option value="">All Landlords</option>
    </select>

    <select id="filterSource">
      <option value="ALL">All Types</option>
      <option value="IAMMETER">IAMMETER</option>
      <option value="FLECTO">FLECTO</option>
      <option value="WATER">WATER</option>
    </select>

    <button type="button" id="resetMeterFilters" class="btn-secondary">Reset Filters</button>
  </div>

  <div class="status-legend" style="margin:15px 0; padding:12px; background:var(--bg-light); border-radius:8px;">
      <strong>Status Legend:</strong>
      <div style="margin-top:6px;display:flex;gap:20px;flex-wrap:wrap;">
        <span><span class="status-dot" style="background:#48e072;"></span> Active (< 2h)</span>
        <span><span class="status-dot" style="background:#ffcc00;"></span> Late (2–24h)</span>
        <span><span class="status-dot" style="background:#ff4d4f;"></span> Offline (> 24h)</span>
        <span><span class="status-dot" style="background:#999;"></span> Never / Invalid</span>
      </div>

      <div style="margin-top:10px;">
        <label style="margin-right:10px;font-weight:600;">Filter by Status:</label>
        <select id="filterStatus" style="padding:6px 10px;border-radius:6px;background:#222;color:#fff;">
          <option value="ALL">All</option>
          <option value="GREEN">Green (Active)</option>
          <option value="YELLOW">Yellow (Late)</option>
          <option value="RED">Red (Offline)</option>
        </select>
      </div>
  </div>

  <section id="metersTable" class="table-card">
    <div class="table-controls">
      <input type="text" id="searchMeters" placeholder="Search serial, property, tenant..." />
      <div class="pagination">
        <button id="prevPage">◀</button>
        <span id="pageInfo">Page 1</span>
        <button id="nextPage">▶</button>
      </div>
    </div>

    <table class="meters sortable">
      <thead>
        <tr>
          <th data-sort="source">Source</th>
          <th data-sort="serial_number">Serial</th>
          <th data-sort="property_number">Property</th>
          <th data-sort="landlord_name">Landlord</th>
          <th data-sort="tenant_name">Tenant</th>
          <th data-sort="last_synced_at">Last Sync</th>
          <th>Status</th>
        </tr>
      </thead>

      <tbody id="metersBody">
        <tr><td colspan="7" class="muted">Loading…</td></tr>
      </tbody>
    </table>
  </section>
</div>

<script src="/assets/js/admin.js"></script>
</body>
</html>
<?php ob_end_flush(); ?>
