@using FoodsharingSiegen.Contracts.Entity
@using FoodsharingSiegen.Shared.Helper
@using Microsoft.AspNetCore.Components

@code {
    private Modal ModalReference { get; set; } = null!;

    private User User { get; set; } = new();

    private string? Password { get; set; }
    private string? ConfirmPassword { get; set; }

    [Parameter]
    public EventCallback<User> OnPasswortSet { get; set; }

    public async Task Show(User user)
    {
        User = user;
        await ModalReference.Show();
    }

    private ValidationStatus IsValidPassword { get; set; }
    private ValidationStatus IsValidConfirm { get; set; }

    private bool SaveDisabled
    {
        get
        {
            if (IsValidPassword != ValidationStatus.Success) return true;
            if (IsValidConfirm != ValidationStatus.Success) return true;
            return Password != ConfirmPassword;
        }
    }

    private async Task SaveClick(object arg)
    {
        User.Password = Password ?? string.Empty;
        await OnPasswortSet.InvokeAsync(User);
        await ModalReference.Hide();
    }

}

<Modal @ref="@ModalReference">
    <ModalContent Centered Size="ModalSize.Small">
        <ModalHeader>
            <ModalTitle>Passwort von @User.Name setzen</ModalTitle>
            <CloseButton/>
        </ModalHeader>
        <ModalBody>
            <Field>
                <FieldLabel>Passwort</FieldLabel>
                <Validation Validator="ValidationHelper.ValidatePassword" @bind-Status="@IsValidPassword">
                    <TextEdit @bind-Text="Password" Placeholder="Passwort" Role="TextRole.Password"></TextEdit>
                </Validation>
            </Field>
            <Field>
                <FieldLabel>Passwort wiederholen</FieldLabel>
                <Validation Validator="ValidationHelper.ValidatePassword" @bind-Status="@IsValidConfirm">
                    <TextEdit @bind-Text="ConfirmPassword" Placeholder="Passwort" Role="TextRole.Password"></TextEdit>
                </Validation>
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Clicked="ModalReference.Hide" Color="Color.Secondary">Abbrechen</Button>
            <Button Clicked="SaveClick" Color="Color.Primary" Disabled="@SaveDisabled">Speichern</Button>
        </ModalFooter>
    </ModalContent>
</Modal>