<?php
session_start();
require_once 'db_connect.php';

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

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

$id = intval($_GET['id'] ?? 0);
if ($id > 0) {
    $stmt = $conn_rbe->prepare("DELETE FROM smart_meters WHERE id = ?");
    $stmt->execute([$id]);
    header("Location: admin-dashboard.php?msg=Meter+deleted");
    exit();
}
header("Location: admin-dashboard.php?msg=Invalid+ID");
exit();
?>
