@page "/einstellungen"
@attribute [Authorize]
@inject NetworkInfoService NetInfo

<PageTitle>ScannerPilot - Einstellungen</PageTitle>

<h2>Einstellungen</h2>

@if (loading)
{
    <p>Lade aktuelle Einstellungen …</p>
}
else
{
    <dl class="row">
        <dt class="col-sm-3">Aktuelle Filial‑ID</dt>
        <dd class="col-sm-9">@currentFilialId (@currentFilialName)</dd>

        <dt class="col-sm-3">Angemeldeter Benutzer</dt>
        <dd class="col-sm-9">@currentUser</dd>

        <dt class="col-sm-3">Client‑IP</dt>
        <dd class="col-sm-9">@currentIp</dd>

        <dt class="col-sm-3">MAC-Adresse</dt>
        <dd class="col-sm-9">@macAdresse</dd>


    </dl>
}

<div class="alert alert-light border mt-4">
    <strong>Support:</strong>
    Bei Fragen oder Problemen wenden Sie sich bitte an <br /><br />
    <strong>Herrn Meinhold</strong> (📞 0175 / 35 31 462, ✉️ <a href="mailto:christopher.meinhold@lambertz.com">christopher.meinhold@lambertz.com</a>)<br />
    oder<br />
    <strong>Herrn Vossel</strong> (📞 02405 / 40804 188, ✉️ <a href="mailto:ruediger.vossel@lambertz.com">ruediger.vossel@lambertz.com</a>)
</div>

@code {
    [Inject] private FilialService FilialService { get; set; } = default!;
    [Inject] private AuthenticationStateProvider AuthProvider { get; set; } = default!;
    [Inject] private IJSRuntime JS { get; set; } = default!;
    [Inject] private IHttpContextAccessor HttpContextAccessor { get; set; } = default!;
    @inject NetworkInfoService NetInfo

    private string? currentUser;
    private string? currentIp;
    private string? macAdresse;
    private int? currentFilialId;
    private string? currentFilialName;
    private bool loading = true;

    protected override async Task OnInitializedAsync()
    {
        // Angemeldeten Benutzer ermitteln
        var authState = await AuthProvider.GetAuthenticationStateAsync();
        currentUser = authState.User.Identity?.Name;

        // Filial‑ID aus localStorage lesen
        var filialStr = await JS.InvokeAsync<string?>("localStorage.getItem", "scannerpilot.filialId");
        if (!string.IsNullOrWhiteSpace(filialStr) && int.TryParse(filialStr.Trim('"'), out var filialId))
        {
            currentFilialId = filialId;

            // Filialname via DB laden
            var filiale = await FilialService.GetFilialeAsync(filialId);
            currentFilialName = filiale?.Bezeichnung;
        }
                
        // IP aus HttpContext/ForwardedHeaders
        var ip = HttpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
        currentIp = ip;

        macAdresse = NetInfo.GetMacFromIp(currentIp);
        
        loading = false;
    }
}
