@page "/AdminSettings/CouchLogSettings"
@rendermode InteractiveServer

@using CouchLog.Data
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.Forms

@inject ApplicationDbContext CouchLogDB

<h3>CouchLog Settings</h3>

@if (accountsSettings is null)
{
    <p>Lade Einstellungen…</p>
}
else
{
    <EditForm Model="accountsSettings">
        <div class="form-check form-switch">
            <InputCheckbox class="form-check-input"
                           role="switch"
                           id="IsRegistrationallowedInput"
                           Value="accountsSettings.IsRegistrationAllowed"
                           ValueChanged="OnRegistrationChanged"
                           ValueExpression="() => accountsSettings.IsRegistrationAllowed" />



            <label class="form-check-label" for="IsRegistrationallowedInput">
                Is Registration allowed
            </label>
        </div>
    </EditForm>
}

@code {
    private AccountsSettings? accountsSettings;

    protected override async Task OnInitializedAsync()
    {
        accountsSettings = await CouchLogDB.AccountsSettings.FirstAsync();
    }

    private async Task OnRegistrationChanged(bool value)
    {
        accountsSettings!.IsRegistrationAllowed = value;

        CouchLogDB.AccountsSettings.Update(accountsSettings);
        await CouchLogDB.SaveChangesAsync();
    }
}
