@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors

<div class="row g-2">
    <div class="col-md-12"><label class="form-label">Upstream node reference (NodeId)</label>
        <input type="text" class="form-control form-control-sm mono" value="@_m.NodeId"
               placeholder="nsu=urn:server:ns;s=Line3.Temp"
               @onchange="@(e => Update(() => _m.NodeId = e.Value?.ToString() ?? string.Empty))" />
        <div class="form-text">The remote OPC UA NodeId the driver reads/writes/subscribes against. Use the browse picker on the driver page to find it.</div></div>
</div>

@code {
    [Parameter] public string? ConfigJson { get; set; }
    [Parameter] public EventCallback<string> ConfigJsonChanged { get; set; }
    /// <summary>DriverType of the selected driver — lets browse-capable pickers open a universal session.</summary>
    [Parameter] public string DriverType { get; set; } = "";
    /// <summary>Live accessor for the selected driver's DriverConfig JSON (browse connect config).</summary>
    [Parameter] public Func<string> GetDriverConfigJson { get; set; } = () => "{}";

    private OpcUaClientTagConfigModel _m = new();
    private string? _lastConfigJson;

    // Re-parse only when the incoming JSON actually changes, so an unrelated parent re-render
    // (Blazor Server live-status pushes do this) can't reset the user's in-progress edits.
    protected override void OnParametersSet()
    {
        if (ConfigJson == _lastConfigJson) { return; }
        _lastConfigJson = ConfigJson;
        _m = OpcUaClientTagConfigModel.FromJson(ConfigJson);
    }

    private async Task Update(Action apply)
    {
        apply();
        await ConfigJsonChanged.InvokeAsync(_m.ToJson());
    }
}
