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

$admin_id = $_SESSION['user_id'] ?? 0;
$role = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id=?");
$role->execute([$admin_id]);
if ($role->fetchColumn()!=='admin') die("Admins only");

$agent_id = (int)($_GET['id'] ?? 0);
if ($_SERVER['REQUEST_METHOD']==='POST') {
    $rate = $_POST['commission_rate'];
    $stmt = $conn_rbe->prepare("UPDATE rbe_agents SET commission_rate=? WHERE id=?");
    $stmt->execute([$rate,$agent_id]);
    echo "Updated.";
}

$q = $conn_rbe->prepare("SELECT * FROM rbe_agents WHERE id=?");
$q->execute([$agent_id]);
$agent = $q->fetch(PDO::FETCH_ASSOC);
?>
<h2>Edit Agent</h2>
<form method="post">
  <label>Commission Rate <input name="commission_rate" value="<?=$agent['commission_rate']?>"></label>
  <button type="submit">Update</button>
</form>
