@* Static S7 address builder: area + db-number + offset + type → DB10.DBD20:REAL / M0.0:X *@
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers

<div class="row g-3">
    <div class="col-md-2">
        <label class="form-label">Area</label>
        <select class="form-select form-select-sm" @bind="_area" @bind:after="OnChangedAsync">
            <option value="DB">DB — Data Block</option>
            <option value="M">M — Merker</option>
            <option value="I">I — Input</option>
            <option value="Q">Q — Output</option>
        </select>
    </div>
    @if (_area == "DB")
    {
        <div class="col-md-2">
            <label class="form-label">DB number</label>
            <input type="number" class="form-control form-control-sm" min="1" max="65535"
                   @bind="_dbNumber" @bind:after="OnChangedAsync" />
        </div>
    }
    <div class="col-md-2">
        <label class="form-label">Offset (bytes)</label>
        <input type="number" class="form-control form-control-sm" min="0" max="65535"
               @bind="_offset" @bind:after="OnChangedAsync" />
    </div>
    <div class="col-md-2">
        <label class="form-label">S7 type</label>
        <select class="form-select form-select-sm" @bind="_s7Type" @bind:after="OnChangedAsync">
            <option value="X">X — Bit</option>
            <option value="B">B — Byte</option>
            <option value="W">W — Word</option>
            <option value="D">D — DWord</option>
            <option value="REAL">REAL — Float</option>
        </select>
    </div>
</div>

<div class="mt-3">
    <span class="text-muted small">Result:</span>
    <code class="mono ms-2">@_built</code>
</div>

@code {
    [Parameter] public string CurrentAddress { get; set; } = "";
    [Parameter] public EventCallback<string> CurrentAddressChanged { get; set; }

    private string _area = "DB";
    private int _dbNumber = 1;
    private int _offset = 0;
    private string _s7Type = "REAL";
    private string _built = "";

    protected override void OnInitialized()
    {
        _built = S7AddressBuilder.Build(_area, _dbNumber, _offset, _s7Type);
        _ = CurrentAddressChanged.InvokeAsync(_built);
    }

    private async Task OnChangedAsync()
    {
        _built = S7AddressBuilder.Build(_area, _dbNumber, _offset, _s7Type);
        await CurrentAddressChanged.InvokeAsync(_built);
    }
}
