@page "/change-password"
@attribute [Authorize]

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components
@using Microsoft.Extensions.Options
@using WorkTracker.Configuration

@inject IOptions<AppAuthOptions> AppAuthOptions
@inject NavigationManager Navigation

<PageTitle>Change Password</PageTitle>

@if (!AppAuthOptions.Value.Enabled)
{
    <p>Redirecting...</p>
}
else
{
<h1>Change password</h1>

@if (!string.IsNullOrWhiteSpace(Error))
{
    <div class="alert alert-danger" role="alert">@Error</div>
}

<form method="post" action="/api/change-password" class="d-flex flex-column gap-3" style="max-width: 420px;">
    <div>
        <label for="newPassword" class="form-label">New password</label>
        <input id="newPassword" name="newPassword" type="password" autocomplete="new-password" class="form-control" required />
    </div>

    <div>
        <label for="confirmPassword" class="form-label">Confirm new password</label>
        <input id="confirmPassword" name="confirmPassword" type="password" autocomplete="new-password" class="form-control" required />
    </div>

    <button type="submit" class="btn btn-primary">Change password</button>
</form>
}

@code {
    [SupplyParameterFromQuery]
    public string? Error { get; set; }

    protected override void OnInitialized()
    {
        if (!AppAuthOptions.Value.Enabled)
        {
            Navigation.NavigateTo("/", forceLoad: true);
        }
    }
}
