<?php
// tenant-dashboard.php (styled)
// Tenant dashboard with month toggles and tier billing
// Styling upgraded to match global theme and sidebars
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
date_default_timezone_set('Africa/Johannesburg');

require_once __DIR__ . '/core/db_connect.php'; // provides $conn_rbe (PDO)
require_once __DIR__ . '/modules/billing/includes/energy_engine.php';

// ---------------------------
// Access Control
// ---------------------------
$role = null;
$tenant_id = null;

if (isset($_GET['tenant_id']) && isset($_SESSION['user_id'])) {
    $admin_id = (int)$_SESSION['user_id'];
    $stmt = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id = ? LIMIT 1");
    $stmt->execute([$admin_id]);
    $role_val = $stmt->fetchColumn();
    if ($role_val === 'admin' || $role_val === 'landlord') {
        $role = $role_val;
        $tenant_id = (int)($_GET['tenant_id']);
    } else {
        exit('Access denied.');
    }
} elseif (isset($_SESSION['user_id'])) {
    $tenant_id = (int)$_SESSION['user_id'];
    $stmt = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id = ? LIMIT 1");
    $stmt->execute([$tenant_id]);
    $role_val = $stmt->fetchColumn();
    if ($role_val !== 'tenant') {
        exit('Access denied.');
    }
    $role = 'tenant';
} else {
    header("Location: login.php");
    exit();
}

// Sidebar for impersonation views (admin/landlord viewing a tenant)
$__has_sidebar = false;
if ($role === 'admin') {
    $__has_sidebar = true;
    @include __DIR__ . '/admin-nav.php';
} elseif ($role === 'landlord') {
    $__has_sidebar = true;
    @include __DIR__ . '/landlord-nav.php';
}

// ---------------------------
// Period selection
// ---------------------------
// ?period=this  (default) -> current month 00:00 to now
// ?period=prev           -> previous month 00:00 to previous month 23:59:59
$period = strtolower($_GET['period'] ?? 'this');
$tz = new DateTimeZone('Africa/Johannesburg');
$now = new DateTime('now', $tz);

if ($period === 'prev') {
    $start_dt = new DateTime('first day of last month 00:00:00', $tz);
    $end_dt   = new DateTime('last day of last month 23:59:59', $tz);
    $period_label = $start_dt->format('F Y') . ' (full month)';
    $days_divisor = (int)$start_dt->format('t'); // days in last month
} else {
    $start_dt = new DateTime('first day of this month 00:00:00', $tz);
    $end_dt   = clone $now; // to "right now"
    $period = 'this'; // normalize
    $period_label = 'This Month (to now)';
    $days_divisor = max(1, (int)$now->format('j')); // days passed this month
}

$RANGE_START = $start_dt->format('Y-m-d H:i:s');
$RANGE_END   = $end_dt->format('Y-m-d H:i:s');

// ---------------------------
// Helpers
// ---------------------------
function esc($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

// ---------------------------
// Tenant & Landlord
// ---------------------------
$stmt = $conn_rbe->prepare("SELECT fullname, email, phone FROM rbe_users WHERE id = ?");
$stmt->execute([$tenant_id]);
$tenant = $stmt->fetch(PDO::FETCH_ASSOC) ?: ['fullname'=>'', 'email'=>'', 'phone'=>''];

$stmt = $conn_rbe->prepare("SELECT landlord_id FROM tenant_landlord_map WHERE tenant_id = ? LIMIT 1");
$stmt->execute([$tenant_id]);
$landlord_id = $stmt->fetchColumn();

$landlord_name = 'Unassigned';
if ($landlord_id) {
    $stmt = $conn_rbe->prepare("SELECT fullname FROM rbe_users WHERE id = ?");
    $stmt->execute([$landlord_id]);
    $landlord_name = $stmt->fetchColumn() ?: 'Unassigned';
}

$use_wallet = 0;
if ($landlord_id) {
    $stmt = $conn_rbe->prepare("
        SELECT use_wallet 
        FROM tenant_landlord_map 
        WHERE tenant_id = ? AND landlord_id = ?
        LIMIT 1
    ");
    $stmt->execute([$tenant_id, $landlord_id]);
    $use_wallet = (int)$stmt->fetchColumn();
}


// ---------------------------
// Billing Tiers (STRICTLY landlord-scoped)
// ---------------------------
$tiers_by_type = [];
if ($landlord_id) {
    $stmt = $conn_rbe->prepare("
        SELECT id, type, tier_from, tier_to, rate
        FROM billing_tiers
        WHERE landlord_id = ?
        ORDER BY type, tier_from ASC
    ");
    $stmt->execute([$landlord_id]);
    $tiers_all = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($tiers_all as $t) {
        $key = strtolower(trim($t['type'] ?? ''));
        $tiers_by_type[$key][] = $t;
    }
}

/**
 * Continuous tier billing for quantities (kWh / kL), using landlord-defined thresholds.
 * tier_from inclusive; tier_to exclusive (NULL = open-ended).
 */
function calculateTieredCost(float $amount, array $tiers): float {
    if ($amount <= 0 || empty($tiers)) return 0.0;
    $total = 0.0;
    foreach ($tiers as $tier) {
        $from = isset($tier['tier_from']) ? (float)$tier['tier_from'] : 0.0;
        $to   = (isset($tier['tier_to']) && $tier['tier_to'] !== null) ? (float)$tier['tier_to'] : INF;
        $rate = (float)($tier['rate'] ?? 0.0);
        if ($to <= $from || $amount <= $from) continue;
        $billable = min($amount, $to) - $from;   // portion inside [from, to)
        if ($billable > 0) $total += $billable * $rate;
    }
    return round($total, 2);
}

// ---------------------------
// Meters for this tenant
// ---------------------------
$stmt = $conn_rbe->prepare("SELECT id, serial FROM smart_meters WHERE tenant_id = ?");
$stmt->execute([$tenant_id]);
$iammeters = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $conn_rbe->prepare("SELECT serial_number FROM flecto_meters WHERE tenant_id = ?");
$stmt->execute([$tenant_id]);
$flectos = $stmt->fetchAll(PDO::FETCH_COLUMN);

$stmt = $conn_rbe->prepare("SELECT serial_number FROM water_meters WHERE tenant_id = ?");
$stmt->execute([$tenant_id]);
$waters = $stmt->fetchAll(PDO::FETCH_COLUMN);

// ---------------------------
// Reading helpers (boundary-aware)
// ---------------------------
// Toggle: true if IAMMETER energy_total is in Wh; false if already kWh.
$IAMMETER_STORED_IN_Wh = true;

/**
 * Get a 3-phase-complete timestamp around a pivot.
 * $dir: 'le' (<= pivot, nearest before), 'ge' (>= pivot, nearest after)
 */
function get_iammeter_complete_ts(PDO $conn, int $meter_id, string $pivot, string $dir): ?string {
    if ($dir === 'le') {
        $sql = "
          SELECT reading_timestamp
          FROM smart_meter_readings
          WHERE meter_id = ? AND reading_timestamp <= ?
          GROUP BY reading_timestamp
          HAVING COUNT(DISTINCT phase) = 3
          ORDER BY reading_timestamp DESC
          LIMIT 1
        ";
    } else {
        $sql = "
          SELECT reading_timestamp
          FROM smart_meter_readings
          WHERE meter_id = ? AND reading_timestamp >= ?
          GROUP BY reading_timestamp
          HAVING COUNT(DISTINCT phase) = 3
          ORDER BY reading_timestamp ASC
          LIMIT 1
        ";
    }
    $st = $conn->prepare($sql);
    $st->execute([$meter_id, $pivot]);
    $ts = $st->fetchColumn();
    return $ts ?: null;
}

/**
 * Sum energy_total across phases at a specific timestamp
 */
function get_iammeter_sum_at(PDO $conn, int $meter_id, string $ts): ?float {
    $st = $conn->prepare("
      SELECT SUM(energy_total) AS total_energy
      FROM smart_meter_readings
      WHERE meter_id = ? AND reading_timestamp = ?
    ");
    $st->execute([$meter_id, $ts]);
    $val = $st->fetchColumn();
    return ($val === false || $val === null) ? null : (float)$val;
}

/**
 * IAMMETER kWh used in [start..end], boundary-aware:
 * - start anchor: nearest complete reading <= start, else >= start
 * - end anchor:   nearest complete reading <= end,   else >= end
 */
function get_iammeter_energy_range(PDO $conn, int $meter_id, string $start, string $end, bool $wh_to_kwh = true): float {
    // Find anchors
    $start_ts = get_iammeter_complete_ts($conn, $meter_id, $start, 'le');
    if (!$start_ts) $start_ts = get_iammeter_complete_ts($conn, $meter_id, $start, 'ge');

    $end_ts = get_iammeter_complete_ts($conn, $meter_id, $end, 'le');
    if (!$end_ts) $end_ts = get_iammeter_complete_ts($conn, $meter_id, $end, 'ge');

    if (!$start_ts || !$end_ts) return 0.0;

    // Ensure chronological order
    if ($end_ts < $start_ts) return 0.0;

    $start_sum = get_iammeter_sum_at($conn, $meter_id, $start_ts);
    $end_sum   = get_iammeter_sum_at($conn, $meter_id, $end_ts);

    if ($start_sum === null || $end_sum === null) return 0.0;

    $diff = max($end_sum - $start_sum, 0.0);
    if ($wh_to_kwh) $diff /= 1000.0;

    return round($diff, 2);
}

/**
 * Generic boundary finders for monotonic registers (Flecto, Water):
 * - anchor_start: last reading <= start, else first reading >= start
 * - anchor_end:   last reading <= end,   else first reading >= end
 */
function get_scalar_at_anchor(PDO $conn, string $sql_le, string $sql_ge, array $params_le, array $params_ge): ?float {
    $st = $conn->prepare($sql_le);
    $st->execute($params_le);
    $val = $st->fetchColumn();
    if ($val !== false && $val !== null) return (float)$val;

    $st = $conn->prepare($sql_ge);
    $st->execute($params_ge);
    $val = $st->fetchColumn();
    if ($val !== false && $val !== null) return (float)$val;

    return null;
}

/**
 * Flecto kWh difference in [start..end], using import registers (reg_a + reg_h + reg_l).
 */
function get_flecto_energy_range(PDO $conn, string $serial, string $start, string $end): float {
    $sum_expr = "(reg_a + reg_h + reg_l)";

    $sql_le = "SELECT $sum_expr AS total FROM flecto_meter_readings
               WHERE serial = ? AND created_at <= ?
               ORDER BY created_at DESC LIMIT 1";
    $sql_ge = "SELECT $sum_expr AS total FROM flecto_meter_readings
               WHERE serial = ? AND created_at >= ?
               ORDER BY created_at ASC LIMIT 1";

    $start_val = get_scalar_at_anchor($conn, $sql_le, $sql_ge, [$serial, $start], [$serial, $start]);
    $end_val   = get_scalar_at_anchor($conn, $sql_le, $sql_ge, [$serial, $end],   [$serial, $end]);

    if ($start_val === null || $end_val === null) return 0.0;
    return round(max($end_val - $start_val, 0.0), 2);
}

/**
 * Water kL difference in [start..end], using volume_liters
 * Adjust timestamp column name if yours differs from `created_at`.
 */
function get_water_usage_range(PDO $conn, string $serial, string $start, string $end): float {
    $sql_le = "SELECT volume_liters FROM water_meter_readings
               WHERE serial_number = ? AND reading_datetime <= ?
               ORDER BY created_at DESC LIMIT 1";
    $sql_ge = "SELECT volume_liters FROM water_meter_readings
               WHERE serial_number = ? AND reading_datetime >= ?
               ORDER BY created_at ASC LIMIT 1";

    $start_val = get_scalar_at_anchor($conn, $sql_le, $sql_ge, [$serial, $start], [$serial, $start]);
    $end_val   = get_scalar_at_anchor($conn, $sql_le, $sql_ge, [$serial, $end],   [$serial, $end]);

    if ($start_val === null || $end_val === null) return 0.0;

    $liters = max($end_val - $start_val, 0.0);
    return round($liters / 1000.0, 2); // liters → kL
}

// ---------------------------
// Build per-meter usage & costs for selected window
// ---------------------------
$report_rows = [];
$total_estimate = 0.0;
$total_energy_sum = 0.0; // electricity only (for average daily)
$elec_tiers  = $tiers_by_type['electricity'] ?? [];
$water_tiers = $tiers_by_type['water'] ?? [];
$gen_tiers   = $tiers_by_type['generator'] ?? [];
$solar_tiers = $tiers_by_type['solar'] ?? [];

// IAMMETER
foreach ($iammeters as $m) {
    $usage = get_iammeter_energy_range($conn_rbe, (int)$m['id'], $RANGE_START, $RANGE_END, $IAMMETER_STORED_IN_Wh);
    $total_energy_sum += $usage;
    $cost = calculateTieredCost($usage, $elec_tiers);
    $total_estimate += $cost;

    $report_rows[] = [
        'type'  => 'Electricity (IAMMETER)',
        'meter' => $m['serial'],
        'usage' => $usage,
        'unit'  => 'kWh',
        'cost'  => $cost
    ];
}

// FLECTO (grid vs generator vs solar export using TL300 registers)
if ($flectos) {
    $tou_enabled = false; // TODO: wire real TOU flag when available
    $energy = get_energy_breakdown($conn_rbe, $tenant_id, null, null, null, $RANGE_START, $RANGE_END, $tou_enabled);
    $t = $energy['tenant'] ?? [];
    if ($t) {
        $grid_kwh = (float)($t['import_t1'] ?? 0);
        $gen_kwh  = (float)($t['import_t2'] ?? 0);
        $solar_kwh = (float)($t['solar_export'] ?? 0);

        if ($grid_kwh > 0) {
            $cost = calculateTieredCost($grid_kwh, $elec_tiers);
            $total_energy_sum += $grid_kwh;
            $total_estimate   += $cost;
            $report_rows[] = [
                'type'  => 'Electricity (Grid Flecto)',
                'meter' => 'Flecto group',
                'usage' => $grid_kwh,
                'unit'  => 'kWh',
                'cost'  => $cost,
            ];
        }

        if ($gen_kwh > 0) {
            $cost = calculateTieredCost($gen_kwh, $gen_tiers);
            $total_energy_sum += $gen_kwh;
            $total_estimate   += $cost;
            $report_rows[] = [
                'type'  => 'Electricity (Generator)',
                'meter' => 'Flecto group',
                'usage' => $gen_kwh,
                'unit'  => 'kWh',
                'cost'  => $cost,
            ];
        }

        if ($solar_kwh > 0) {
            $report_rows[] = [
                'type'  => 'Solar Export',
                'meter' => 'Flecto group',
                'usage' => $solar_kwh,
                'unit'  => 'kWh',
                'cost'  => 0.00,
            ];
        }
    }
}

// WATER
foreach ($waters as $serial) {
    $usage = get_water_usage_range($conn_rbe, $serial, $RANGE_START, $RANGE_END);
    $cost = calculateTieredCost($usage, $water_tiers);
    $total_estimate += $cost;

    $report_rows[] = [
        'type'  => 'Water',
        'meter' => $serial,
        'usage' => $usage,
        'unit'  => 'kL',
        'cost'  => $cost
    ];
}

// ---------------------------
// Fixed fees for this tenant (sewage/refuse/rates + Other 1..5)
// Pull ONLY tenant-specific rows.
// ---------------------------
$fixed_rows = [];
if ($landlord_id) {
    $stmt = $conn_rbe->prepare("
        SELECT *
        FROM billing_settings
        WHERE landlord_id = ? AND tenant_id = ?
        ORDER BY id DESC
        LIMIT 1
    ");
    $stmt->execute([$landlord_id, $tenant_id]);
    $tenant_fee_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($tenant_fee_rows as $feeRow) {
        $map = [
            ['label' => 'Sewage Fee',          'name_col' => null,                'amt_col' => 'sewage_fee'],
            ['label' => 'Refuse Removal Fee',  'name_col' => null,                'amt_col' => 'refuse_removal_fee'],
            ['label' => 'Rates & Taxes',       'name_col' => null,                'amt_col' => 'rates_and_taxes'],
            ['label' => null, 'name_col' => 'other_fee_name',   'amt_col' => 'other_fee'],
            ['label' => null, 'name_col' => 'other_fee_name_2', 'amt_col' => 'other_fee_2'],
            ['label' => null, 'name_col' => 'other_fee_name_3', 'amt_col' => 'other_fee_3'],
            ['label' => null, 'name_col' => 'other_fee_name_4', 'amt_col' => 'other_fee_4'],
            ['label' => null, 'name_col' => 'other_fee_name_5', 'amt_col' => 'other_fee_5'],
        ];

        foreach ($map as $def) {
            $amt = isset($feeRow[$def['amt_col']]) ? (float)$feeRow[$def['amt_col']] : 0.0;
            if ($amt <= 0) continue;

            if ($def['name_col']) {
                $nm = trim((string)($feeRow[$def['name_col']] ?? ''));
                $label = ($nm !== '') ? $nm : 'Other Fee';
            } else {
                $label = $def['label'];
            }

            $fixed_rows[] = [
                'type'  => $label,
                'meter' => '—',
                'usage' => '—',
                'unit'  => '—',
                'cost'  => round($amt, 2),
            ];
            $total_estimate += $amt;
        }
    }
}

// Per-tenant extra_fees rows (active only)
if ($landlord_id) {
    $stEf = $conn_rbe->prepare("
      SELECT fee_type, custom_label, amount
      FROM extra_fees
      WHERE tenant_id = ? AND landlord_id = ? AND active = 1
    ");
    $stEf->execute([$tenant_id, $landlord_id]);
    foreach ($stEf->fetchAll(PDO::FETCH_ASSOC) as $ef) {
      $baseLabel = trim((string)($ef['custom_label'] ?? ''));
      if ($baseLabel === '') {
        switch (strtolower((string)($ef['fee_type'] ?? ''))) {
          case 'aircon_fee':      $label = 'Aircon Fee'; break;
          case 'generator_fee':   $label = 'Generator Fee'; break;
          case 'elec_avail_fee':  $label = 'Electricity Availability Fee'; break;
          case 'elec_basic_fee':  $label = 'Electricity Basic Fee'; break;
          case 'water_avail_fee': $label = 'Water Availability Fee'; break;
          case 'water_basic_fee': $label = 'Water Basic Fee'; break;
          case 'custom':
          default:                $label = 'Extra Fee'; break;
        }
      } else {
        $label = $baseLabel;
      }
      $amtEf = (float)($ef['amount'] ?? 0);
      if ($amtEf <= 0) continue;
      $fixed_rows[] = [
        'type'  => $label,
        'meter' => '-',
        'usage' => '-',
        'unit'  => '-',
        'cost'  => round($amtEf, 2),
      ];
      $total_estimate += $amtEf;
    }
}

// Merge fixed rows into main report rows (after usage rows)
$report_rows = array_merge($report_rows, $fixed_rows);

// Derived insights
$average_daily_use = $days_divisor > 0 ? round($total_energy_sum / $days_divisor, 2) : 0.0;
$kva_month = '—'; // wire up if/when you track it

// ---------------------------
// Render (Styled)
// ---------------------------
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Tenant Dashboard | RBE</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="/assets/css/theme.css">
  <style>
    body { margin:0; }
    <?php if ($__has_sidebar): ?>
    body { margin-left: 240px; }
    body.sidebar-collapsed { margin-left: 72px; }
    @media (max-width:900px){ body{ margin-left:0; } body.sidebar-collapsed{ margin-left:0; } }
    <?php endif; ?>
    .container { max-width:1100px; margin:0 auto; padding:24px; }
    .row { display:flex; gap:16px; flex-wrap:wrap; }
    .col { flex:1 1 320px; }
    .toggle { display:flex; gap:8px; margin:10px 0 16px; flex-wrap:wrap; }
    .toggle a { background:rgba(0,204,255,.12); border:1px solid rgba(126,217,87,.25); color:var(--accent); padding:8px 12px; border-radius:999px; text-decoration:none; }
    .toggle a.active { background:var(--accent); color:#0a1b25; border-color:transparent; }
    table { width:100%; border-collapse:collapse; background:var(--surface); border-radius:12px; overflow:hidden; }
    th,td { padding:12px 14px; border-bottom:1px solid rgba(255,255,255,.07); text-align:center; }
    th { background:#0f2e44; color:var(--accent); }
    tfoot td { background:#0f2e44; font-weight:700; }
    .card { opacity:0; transform:translateY(6px); transition:opacity .25s ease, transform .25s ease; }
    .card.visible { opacity:1; transform:translateY(0); }
  </style>
  <script>
    document.addEventListener('DOMContentLoaded', () => {
      document.querySelectorAll('.card').forEach((c,i)=> setTimeout(()=> c.classList.add('visible'), i*80));
    });
  </script>
</head>
<body>

  <div class="container">
    <div class="card" style="display:flex; align-items:center; justify-content:space-between; gap:12px;">
      <h1 class="mb-0">Tenant Dashboard</h1>
      <div style="display:flex; gap:10px; flex-wrap:wrap;">
        <?php if ($role !== 'tenant'): ?>
          <a class="btn secondary" href="<?= $role === 'admin' ? 'admin-dashboard.php' : 'landlord-dashboard.php' ?>">Back to Dashboard</a>
        <?php endif; ?>
        <a class="btn secondary" href="logout.php">Logout</a>
      </div>
    </div>

    <div class="card flat">
      <div class="toggle">
        <a href="?period=this<?= $role!=='tenant' ? '&tenant_id='.urlencode((string)$tenant_id) : '' ?>" class="<?= $period==='this' ? 'active' : '' ?>">This Month</a>
        <a href="?period=prev<?= $role!=='tenant' ? '&tenant_id='.urlencode((string)$tenant_id) : '' ?>" class="<?= $period==='prev' ? 'active' : '' ?>">Previous Month</a>
      </div>
      <p class="muted">
        Window: <span class="tag"><?= esc($RANGE_START) ?> → <?= esc($RANGE_END) ?></span>
        &nbsp;•&nbsp; Period: <span class="tag"><?= esc($period_label) ?></span>
        &nbsp;•&nbsp; Billing tiers: landlord-specific
      </p>
    </div>

    <div class="row">
      <div class="col">
        <div class="card">
          <h2>Your Profile</h2>
          <p><strong>Name:</strong> <?= esc($tenant['fullname']) ?></p>
          <p><strong>Email:</strong> <?= esc($tenant['email']) ?></p>
          <p><strong>Phone:</strong> <?= esc($tenant['phone']) ?></p>
          <p><strong>Landlord:</strong> <?= esc($landlord_name) ?></p>
        </div>
      </div>

      <div class="col">
        <div class="card">
          <h2>Usage Insights</h2>
          <p><strong>Average Daily Use:</strong> <?= number_format($average_daily_use, 2) ?> kWh/day</p>
          <p><strong>kVA for the Period:</strong> <?= esc($kva_month) ?></p>
        </div>
      </div>
    </div>

    <?php if ($use_wallet): ?>
      <div class="card">
        <h2>Your Wallet</h2>
        <?php
          require_once __DIR__ . "/wallet/includes/wallet_db.php";
          $balance = getTenantBalance($tenant_id, $conn_rbe);
          $deducted_balance = $balance;
          if ($period === 'this') { $deducted_balance = $balance - $total_estimate; }
        ?>
        <p><strong>Current Balance:</strong> R <?= number_format($balance, 2) ?></p>
        <?php if ($period === 'this'): ?>
          <p><strong>Balance After Deduction:</strong>
             R <?= number_format($deducted_balance, 2) ?>
             <?= $deducted_balance < 0 ? '<span style="color: var(--danger);">(In Arrears)</span>' : '' ?>
          </p>
        <?php endif; ?>
        <a class="btn" href="wallet/tenant/deposit.php?tenant_id=<?= $tenant_id ?>">Top Up Wallet</a>
        <a class="btn secondary" href="wallet/tenant/history.php?tenant_id=<?= $tenant_id ?>">View Transactions</a>
      </div>
    <?php endif; ?>

    <div class="card">
      <h2><?= $period==='prev' ? 'Last Month' : 'This Month' ?> Usage &amp; Estimated Costs</h2>
      <table>
        <thead>
          <tr>
            <th>Type</th>
            <th>Meter</th>
            <th>Usage</th>
            <th>Unit</th>
            <th>Estimated Cost (R)</th>
          </tr>
        </thead>
        <tbody>
        <?php foreach ($report_rows as $r): ?>
          <tr>
            <td><?= esc($r['type']) ?></td>
            <td><?= esc($r['meter']) ?></td>
            <td><?= is_numeric($r['usage']) ? number_format((float)$r['usage'], 2) : esc($r['usage']) ?></td>
            <td><?= esc($r['unit']) ?></td>
            <td><?= number_format((float)$r['cost'], 2) ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="4" class="right">Total Estimated Bill:</td>
            <td>R <?= number_format($total_estimate, 2) ?></td>
          </tr>
        </tfoot>
      </table>
    </div>

    <!-- OPTIONAL: Global fees display (for information only) -->
    <?php /*
    if (!empty($global_fee_rows)) { ?>
    <div class="card">
      <h2>Global Fees (for your information)</h2>
      <p class="muted">These fees are set at the landlord level and may be distributed separately.</p>
      <table>
        <tr><th>Label</th><th>Amount (R)</th></tr>
        <?php
          $gmap = ['sewage_fee'=>'Sewage Fee','refuse_removal_fee'=>'Refuse Removal Fee','rates_and_taxes'=>'Rates & Taxes'];
          foreach ($global_fee_rows as $g) {
            foreach ($gmap as $col=>$lbl) {
              $v = (float)($g[$col] ?? 0);
              if ($v > 0) echo "<tr><td>".esc($lbl)."</td><td>".number_format($v,2)."</td></tr>";
            }
            $others = [
              ['n'=>$g['other_fee_name'] ?? 'Other','v'=>$g['other_fee'] ?? 0],
              ['n'=>$g['other_fee_name_2'] ?? 'Other','v'=>$g['other_fee_2'] ?? 0],
              ['n'=>$g['other_fee_name_3'] ?? 'Other','v'=>$g['other_fee_3'] ?? 0],
              ['n'=>$g['other_fee_name_4'] ?? 'Other','v'=>$g['other_fee_4'] ?? 0],
              ['n'=>$g['other_fee_name_5'] ?? 'Other','v'=>$g['other_fee_5'] ?? 0],
            ];
            foreach ($others as $o) {
              $v = (float)$o['v'];
              if ($v > 0) echo "<tr><td>".esc($o['n'] ?: 'Other')."</td><td>".number_format($v,2)."</td></tr>";
            }
          }
        ?>
      </table>
    </div>
    <?php } */ ?>

  </div>

</body>
</html>
