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

$user_id = $_SESSION['user_id'] ?? 0;

// Ensure agent
$role = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id=?");
$role->execute([$user_id]);
if ($role->fetchColumn() !== 'agent') die("Access denied");

// Fetch agent details
$q = $conn_rbe->prepare("
  SELECT u.fullname, u.email, u.company, u.phone, cs.rate AS commission_rate
  FROM rbe_users u
  LEFT JOIN commission_settings cs ON cs.agent_id=u.id
  WHERE u.id=?
");
$q->execute([$user_id]);
$agent = $q->fetch(PDO::FETCH_ASSOC) ?: [];

$flash_ok = $flash_err = '';

// Handle update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $company = trim($_POST['company'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');

    try {
        $stmt = $conn_rbe->prepare("UPDATE rbe_users SET company=?, phone=? WHERE id=?");
        $stmt->execute([$company, $phone, $user_id]);

        $flash_ok = "Profile updated successfully.";
        // Refresh
        $q->execute([$user_id]);
        $agent = $q->fetch(PDO::FETCH_ASSOC) ?: [];
    } catch (Exception $e) {
        $flash_err = "Error updating profile: " . $e->getMessage();
    }
}
?>
<link rel="stylesheet" href="/agents/includes/styles.css">
<style>
.profile-form {
  display:flex; flex-direction:column; gap:15px; max-width:500px; margin:0 auto;
}
.profile-form label { font-weight:bold; margin-bottom:5px; }
.profile-form input {
  padding:10px; border-radius:8px; border:1px solid #ccc; font-size:1em;
}
.profile-form button {
  margin-top:15px; padding:12px; background:#7ed957; border:none;
  border-radius:10px; font-weight:bold; font-size:1em; cursor:pointer; color:#000;
}
.profile-form button:hover { background:#00cc77; }
.flash { margin:10px 0; padding:10px 14px; border-radius:8px; }
.flash.ok { background:#0f3b2a; color:#b6ffcf; border:1px solid #176546; }
.flash.err { background:#3b0f0f; color:#ffd6d6; border:1px solid #6b1a1a; }
</style>

<header>
  <img src="https://rbelectrical.enterprises/wp-content/uploads/2024/07/RBE-Electrical-Logo-clear2-250x125.png" class="logo" alt="Logo">
  <div class="title-group">
    <h1>Welcome</h1>
    <h2><?=htmlspecialchars($agent['fullname'] ?? 'Unknown Agent')?></h2>
    <?php if (!empty($agent['company'])): ?>
      <p><?=htmlspecialchars($agent['company'])?></p>
    <?php endif; ?>
    <p><?=htmlspecialchars($agent['email'] ?? '')?></p>
  </div>
</header>

<nav class="nav">
  <a href="/agents/dashboard.php">Dashboard</a>
  <a href="/agents/manage.php">My Clients</a>
  <a href="/agents/commissions.php">Commissions</a>
  <a href="/agents/profile.php" class="active">Profile</a>
  <a href="/logout.php">Logout</a>
</nav>

<section class="section">
  <h2>My Profile</h2>

  <?php if ($flash_ok): ?>
    <div class="flash ok"><?=htmlspecialchars($flash_ok)?></div>
  <?php elseif ($flash_err): ?>
    <div class="flash err"><?=htmlspecialchars($flash_err)?></div>
  <?php endif; ?>

  <form method="post" class="profile-form">
    <label>Full Name (read-only)</label>
    <input type="text" value="<?=htmlspecialchars($agent['fullname'])?>" disabled>

    <label>Email (read-only)</label>
    <input type="email" value="<?=htmlspecialchars($agent['email'])?>" disabled>

    <label>Company</label>
    <input type="text" name="company" value="<?=htmlspecialchars($agent['company'])?>">

    <label>Phone</label>
    <input type="text" name="phone" value="<?=htmlspecialchars($agent['phone'])?>">

    <label>Commission Rate (read-only)</label>
    <input type="text" value="R<?=htmlspecialchars($agent['commission_rate'] ?? 0)?> per meter" disabled>

    <button type="submit">Save Changes</button>
  </form>
</section>

<?php include __DIR__.'/includes/footer.php'; ?>
