<?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);
$success = $error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $id > 0) {
    $new_property = intval($_POST['property_id'] ?? 0);
    $new_landlord = intval($_POST['landlord_id'] ?? 0);
    $new_tenant = intval($_POST['tenant_id'] ?? 0);

    if ($new_property && $new_landlord && $new_tenant) {
        $stmt = $conn_rbe->prepare("
            UPDATE smart_meters 
            SET property_id = ?, landlord_id = ?, tenant_id = ? 
            WHERE id = ?
        ");
        $stmt->execute([$new_property, $new_landlord, $new_tenant, $id]);

        header("Location: admin-dashboard.php?msg=Meter+reassigned");
        exit();
    } else {
        $error = "Please select property, landlord, and tenant.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reassign IAMMETER Meter</title>
    <style>
        body {
            background: #0a1a2f;
            color: white;
            font-family: Arial, sans-serif;
            padding: 30px;
        }
        .card {
            background: #122c4c;
            padding: 25px;
            border-radius: 12px;
            max-width: 500px;
            margin: auto;
            box-shadow: 0 0 15px rgba(0,0,0,0.5);
        }
        label, select {
            display: block;
            width: 100%;
            margin-bottom: 15px;
            font-weight: bold;
        }
        select {
            padding: 12px;
            border-radius: 8px;
            background: #1b3a4d;
            color: white;
            border: none;
        }
        button {
            background: #7ed957;
            color: black;
            padding: 12px;
            border-radius: 8px;
            font-weight: bold;
            border: none;
            cursor: pointer;
            width: 100%;
        }
        button:hover {
            background: #00cc77;
        }
        h2 {
            color: #7ed957;
            text-align: center;
        }
        .success { color: #7ed957; font-weight: bold; margin-bottom: 15px; }
        .error { color: #ff6666; font-weight: bold; margin-bottom: 15px; }
        .back-link {
            text-align: center;
            display: block;
            margin-top: 20px;
            color: #7ed957;
            text-decoration: none;
        }
        .back-link:hover {
            color: #00cc77;
        }
    </style>
</head>
<body>

<div class="card">
    <h2>🔄 Reassign IAMMETER Meter</h2>

    <?php if ($error): ?>
        <p class="error"><?= $error ?></p>
    <?php endif; ?>

    <?php if ($id > 0): ?>
        <form method="post">
            <label for="property_id">🏘️ Assign to Property:</label>
            <select name="property_id" required>
                <option value="">-- Select Property --</option>
                <?php
                $stmt = $conn_rbe->query("SELECT id, property_number FROM properties ORDER BY property_number ASC");
                while ($prop = $stmt->fetch()) {
                    echo "<option value='{$prop['id']}'>{$prop['property_number']}</option>";
                }
                ?>
            </select>

            <label for="landlord_id">👤 Assign to Landlord:</label>
            <select name="landlord_id" required>
                <option value="">-- Select Landlord --</option>
                <?php
                $stmt = $conn_rbe->query("
                    SELECT u.id, u.fullname 
                    FROM rbe_users u 
                    JOIN rbe_roles r ON u.id = r.user_id 
                    WHERE r.role = 'landlord' 
                    ORDER BY u.fullname ASC
                ");
                while ($ll = $stmt->fetch()) {
                    echo "<option value='{$ll['id']}'>{$ll['fullname']}</option>";
                }
                ?>
            </select>

            <label for="tenant_id">👥 Assign to Tenant:</label>
            <select name="tenant_id" required>
                <option value="">-- Select Tenant --</option>
                <?php
                $stmt = $conn_rbe->query("
                    SELECT u.id, u.fullname 
                    FROM rbe_users u 
                    JOIN rbe_roles r ON u.id = r.user_id 
                    WHERE r.role = 'tenant' 
                    ORDER BY u.fullname ASC
                ");
                while ($t = $stmt->fetch()) {
                    echo "<option value='{$t['id']}'>{$t['fullname']}</option>";
                }
                ?>
            </select>

            <button type="submit">💾 Reassign Meter</button>
        </form>
    <?php else: ?>
        <p class="error">❌ Invalid meter ID.</p>
    <?php endif; ?>

    <a class="back-link" href="admin-dashboard.php">⬅ Back to Dashboard</a>
</div>

</body>
</html>
