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

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

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

$property_id = (int)($_GET['id'] ?? 0);
if (!$property_id) die("Invalid property.");

// Fetch property & landlord (ensure agent owns it)
$q = $conn_rbe->prepare("
    SELECT p.id, p.property_number, p.address, p.square_meters, p.created_at,
           u.fullname AS landlord_name, u.email AS landlord_email, u.phone AS landlord_phone
    FROM properties p
    LEFT JOIN rbe_users u ON u.id = p.landlord_id
    WHERE p.id=? AND p.agent_id=?
    LIMIT 1
");
$q->execute([$property_id, $user_id]);
$property = $q->fetch(PDO::FETCH_ASSOC);

if (!$property) die("Property not found or access denied.");

// Fetch meters linked to this property
$smart = $conn_rbe->prepare("
    SELECT id, serial, meter_type, meter_units, created_at
    FROM smart_meters
    WHERE property_id=? AND agent_id=?
    ORDER BY created_at DESC
");
$smart->execute([$property_id, $user_id]);
$smart_meters = $smart->fetchAll(PDO::FETCH_ASSOC);

$flecto = $conn_rbe->prepare("
    SELECT id, serial_number, description, created_at
    FROM flecto_meters
    WHERE property_id=? AND agent_id=?
    ORDER BY created_at DESC
");
$flecto->execute([$property_id, $user_id]);
$flecto_meters = $flecto->fetchAll(PDO::FETCH_ASSOC);

$water = $conn_rbe->prepare("
    SELECT id, serial_number, description, created_at
    FROM water_meters
    WHERE property_id=? AND agent_id=?
    ORDER BY created_at DESC
");
$water->execute([$property_id, $user_id]);
$water_meters = $water->fetchAll(PDO::FETCH_ASSOC);
?>

<link rel="stylesheet" href="/agents/includes/styles.css">
<style>
.section { margin-bottom: 30px; }
.btn-action {
  display:inline-block; padding:6px 12px; margin:2px;
  font-size:0.85em; font-weight:600; border-radius:6px;
  text-decoration:none; color:#fff;
}
.btn-view { background:#3498db; }
.btn-edit { background:#e67e22; }
.btn-back { background:#7ed957; color:#000; font-weight:bold; }
</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>Property Details</h1>
    <h2><?=htmlspecialchars($property['property_number'])?></h2>
    <p><?=htmlspecialchars($property['address'])?></p>
  </div>
</header>

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

<section class="section">
  <h2>Property Info</h2>
  <table>
    <tr><th>ID</th><td><?=$property['id']?></td></tr>
    <tr><th>Property Number</th><td><?=htmlspecialchars($property['property_number'])?></td></tr>
    <tr><th>Address</th><td><?=htmlspecialchars($property['address'])?></td></tr>
    <tr><th>Square Meters</th><td><?=htmlspecialchars($property['square_meters'] ?? '—')?></td></tr>
    <tr><th>Date Added</th><td><?=date("Y-m-d", strtotime($property['created_at']))?></td></tr>
    <tr><th>Landlord</th><td><?=htmlspecialchars($property['landlord_name'] ?? '—')?></td></tr>
    <tr><th>Email</th><td><?=htmlspecialchars($property['landlord_email'] ?? '—')?></td></tr>
    <tr><th>Phone</th><td><?=htmlspecialchars($property['landlord_phone'] ?? '—')?></td></tr>
  </table>
  <br>
  <a href="/agents/manage.php" class="btn-action btn-back">⬅ Back</a>
  <a href="/edit-property.php?id=<?=$property['id']?>" class="btn-action btn-edit">✏ Edit</a>
</section>

<section class="section">
  <h2>IAMMETER Meters</h2>
  <?php if ($smart_meters): ?>
    <table>
      <tr><th>ID</th><th>Serial</th><th>Type</th><th>Units</th><th>Added</th><th>Actions</th></tr>
      <?php foreach($smart_meters as $m): ?>
      <tr>
        <td><?=$m['id']?></td>
        <td><?=htmlspecialchars($m['serial'])?></td>
        <td><?=htmlspecialchars($m['meter_type'])?></td>
        <td><?=htmlspecialchars($m['meter_units'])?></td>
        <td><?=date("Y-m-d", strtotime($m['created_at']))?></td>
        <td><a href="/meter-details.php?id=<?=$m['id']?>" class="btn-action btn-view">View</a></td>
      </tr>
      <?php endforeach; ?>
    </table>
  <?php else: ?><p>No IAMMETER meters assigned.</p><?php endif; ?>
</section>

<section class="section">
  <h2>Flecto Meters</h2>
  <?php if ($flecto_meters): ?>
    <table>
      <tr><th>ID</th><th>Serial</th><th>Description</th><th>Added</th><th>Actions</th></tr>
      <?php foreach($flecto_meters as $m): ?>
      <tr>
        <td><?=$m['id']?></td>
        <td><?=htmlspecialchars($m['serial_number'])?></td>
        <td><?=htmlspecialchars($m['description'])?></td>
        <td><?=date("Y-m-d", strtotime($m['created_at']))?></td>
        <td><a href="/meter-details.php?type=flecto&id=<?=$m['id']?>" class="btn-action btn-view">View</a></td>
      </tr>
      <?php endforeach; ?>
    </table>
  <?php else: ?><p>No Flecto meters assigned.</p><?php endif; ?>
</section>

<section class="section">
  <h2>Water Meters</h2>
  <?php if ($water_meters): ?>
    <table>
      <tr><th>ID</th><th>Serial</th><th>Description</th><th>Added</th><th>Actions</th></tr>
      <?php foreach($water_meters as $m): ?>
      <tr>
        <td><?=$m['id']?></td>
        <td><?=htmlspecialchars($m['serial_number'])?></td>
        <td><?=htmlspecialchars($m['description'])?></td>
        <td><?=date("Y-m-d", strtotime($m['created_at']))?></td>
        <td><a href="/meter-details.php?type=water&id=<?=$m['id']?>" class="btn-action btn-view">View</a></td>
      </tr>
      <?php endforeach; ?>
    </table>
  <?php else: ?><p>No water meters assigned.</p><?php endif; ?>
</section>

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