@page "/clusters/{ClusterId}/equipment"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">Equipment &middot; <span class="mono">@ClusterId</span></h4>
    <div class="d-flex gap-2">
        <a href="/clusters/@ClusterId/equipment/import" class="btn btn-outline-primary btn-sm">Import CSV…</a>
        <a href="/clusters/@ClusterId/equipment/new" class="btn btn-primary btn-sm">New equipment</a>
    </div>
</div>

<ClusterNav ClusterId="@ClusterId" ActiveTab="equipment" />

@if (_rows is null)
{
    <p>Loading…</p>
}
else
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Equipment rows are scoped to a UNS line and bound to a single driver. EquipmentId is
        system-generated (decision #125); browse identifiers are MachineCode (operator) + ZTag
        (ERP).
    </section>

    <section class="panel rise mt-3" style="animation-delay:.08s">
        <div class="panel-head">@_rows.Count equipment row@(_rows.Count == 1 ? "" : "s")</div>
        @if (_rows.Count == 0)
        {
            <div style="padding:1rem" class="text-muted">No equipment defined for this cluster.</div>
        }
        else
        {
            <div class="table-wrap">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>EquipmentId</th>
                            <th>Name</th>
                            <th>MachineCode</th>
                            <th>ZTag</th>
                            <th>Driver</th>
                            <th>UNS line</th>
                            <th>Identification</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var e in _rows)
                        {
                            <tr>
                                <td><span class="mono small">@e.EquipmentId</span></td>
                                <td>@e.Name</td>
                                <td><span class="mono">@e.MachineCode</span></td>
                                <td>@(e.ZTag ?? "—")</td>
                                <td><span class="mono small">@e.DriverInstanceId</span></td>
                                <td><span class="mono small">@e.UnsLineId</span></td>
                                <td class="text-muted small">
                                    @if (!string.IsNullOrWhiteSpace(e.Manufacturer)) { <span>@e.Manufacturer</span> }
                                    @if (!string.IsNullOrWhiteSpace(e.Model)) { <span class="ms-1">/ @e.Model</span> }
                                </td>
                                <td><a href="/clusters/@ClusterId/equipment/@e.EquipmentId" class="btn btn-sm btn-outline-primary">Edit</a></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    private List<Equipment>? _rows;

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        var driversInCluster = db.DriverInstances.AsNoTracking()
            .Where(d => d.ClusterId == ClusterId).Select(d => d.DriverInstanceId);
        _rows = await db.Equipment.AsNoTracking()
            .Where(e => driversInCluster.Contains(e.DriverInstanceId))
            .OrderBy(e => e.Name)
            .ToListAsync();
    }
}
