<?php
/**
 * dashboard-container-stats-rows.php — <tr> rows for the live container memory
 * table. Shared by the dashboard server-render and the AJAX reload endpoint.
 * Expects in scope: $live_container_stats (array) and $show_user_column (bool).
 *
 * data-sort attributes give whp-table.js numeric/percent sort keys for the MB
 * and Usage columns (which otherwise sort as strings / aren't sortable via the
 * progress bar markup).
 */
foreach ($live_container_stats as $cname => $stat):
    $use_pct = (($stat['limit_mb'] ?? 0) > 0) ? ($stat['anon_mb'] / $stat['limit_mb']) * 100 : 0;
?>
<tr>
    <td class="whp-cell-truncate" title="<?php echo htmlspecialchars($stat['domain'] ?? '', ENT_QUOTES); ?>"><?php echo htmlspecialchars($stat['domain'] ?? ''); ?></td>
    <td><small><?php echo htmlspecialchars($cname); ?></small></td>
    <?php if (!empty($show_user_column)): ?>
    <td><?php echo htmlspecialchars($stat['username'] ?? ''); ?></td>
    <?php endif; ?>
    <td class="text-end" data-sort="<?php echo (int) $stat['anon_mb']; ?>"><strong><?php echo $stat['anon_mb']; ?> MB</strong></td>
    <td class="text-end" data-sort="<?php echo (int) $stat['cache_mb']; ?>"><?php echo $stat['cache_mb']; ?> MB</td>
    <td class="text-end" data-sort="<?php echo (int) $stat['total_mb']; ?>"><?php echo $stat['total_mb']; ?> MB</td>
    <td class="text-end" data-sort="<?php echo (int) $stat['limit_mb']; ?>"><?php echo $stat['limit_mb']; ?> MB</td>
    <td data-sort="<?php echo round($use_pct, 1); ?>" style="min-width:120px">
        <div class="progress" style="height: 16px;">
            <div class="progress-bar <?php echo $use_pct > 80 ? 'bg-danger' : ($use_pct > 60 ? 'bg-warning' : 'bg-success'); ?>"
                 role="progressbar" aria-label="<?php echo htmlspecialchars(($stat['domain'] ?? $cname) . ' memory usage', ENT_QUOTES); ?>"
                 style="width: <?php echo min($use_pct, 100); ?>%"
                 aria-valuenow="<?php echo round($use_pct); ?>" aria-valuemin="0" aria-valuemax="100">
                <small><?php echo number_format($use_pct, 0); ?>%</small>
            </div>
        </div>
    </td>
</tr>
<?php endforeach; ?>
