@page "/clusters"
@using ZB.MOM.WW.OtOpcUa.Admin.Services
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject ClusterService ClusterSvc

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>Clusters</h1>
    <a href="/clusters/new" class="btn btn-primary">New cluster</a>
</div>

@if (_clusters is null)
{
    <p>Loading…</p>
}
else if (_clusters.Count == 0)
{
    <p class="text-muted">No clusters yet. Create the first one.</p>
}
else
{
    <table class="table table-hover">
        <thead>
            <tr>
                <th>ClusterId</th><th>Name</th><th>Enterprise</th><th>Site</th>
                <th>RedundancyMode</th><th>NodeCount</th><th>Enabled</th><th></th>
            </tr>
        </thead>
        <tbody>
        @foreach (var c in _clusters)
        {
            <tr>
                <td><code>@c.ClusterId</code></td>
                <td>@c.Name</td>
                <td>@c.Enterprise</td>
                <td>@c.Site</td>
                <td>@c.RedundancyMode</td>
                <td>@c.NodeCount</td>
                <td>
                    @if (c.Enabled) { <span class="badge bg-success">Active</span> }
                    else { <span class="badge bg-secondary">Disabled</span> }
                </td>
                <td><a href="/clusters/@c.ClusterId" class="btn btn-sm btn-outline-primary">Open</a></td>
            </tr>
        }
        </tbody>
    </table>
}

@code {
    private List<ServerCluster>? _clusters;

    protected override async Task OnInitializedAsync()
    {
        _clusters = await ClusterSvc.ListAsync(CancellationToken.None);
    }
}
