using Blazorise;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.BaseClasses;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Dialogs
{
///
/// The add prospect modal class (a. beging, 31.05.2022)
///
public partial class EditProspectDialog : FsBase
{
#region Dependencies
///
/// Gets or sets the value of the prospect service (ab)
///
[Inject]
public ProspectService ProspectService { get; set; } = null!;
#endregion
#region Parameters
///
/// Gets or sets the value of the is update mode (ab)
///
[Parameter]
public bool IsUpdateMode { get; set; }
[Parameter]
public Func? OnSuccess { get; set; }
///
/// Gets or sets the value of the on add (ab)
///
[Parameter]
public EventCallback OnAdd { get; set; }
///
/// Gets or sets the value of the on update (ab)
///
[Parameter]
public EventCallback OnUpdate { get; set; }
///
/// Gets or sets the value of the prospect (ab)
///
[Parameter]
public Prospect Prospect { get; set; } = new();
///
/// Gets or sets the value of the save button text (ab)
///
[Parameter]
public string? SaveButtonText { get; set; }
#endregion
#region Public Method ShowAsync
///
/// Displays the AddProspectModal dialog asynchronously, allowing for the creation or editing of a prospect.
///
/// The modal service used to display the dialog.
/// Callback to be invoked upon successful addition or update of a prospect.
/// The prospect to be edited, or null for creating a new prospect.
/// A task representing the asynchronous operation.
public static async Task ShowAsync(IModalService modalService, Func onSuccess, Prospect? prospect = null)
{
await modalService.Show(prospect == null ? "Neuling hinzufügen" : $"{prospect.Name} bearbeiten", p =>
{
p.Add(nameof(Prospect), prospect ?? new Prospect());
p.Add(nameof(IsUpdateMode), prospect != null);
p.Add(nameof(SaveButtonText), prospect == null ? "Hinzufügen" : "Speichern");
p.Add(nameof(OnSuccess), onSuccess);
});
}
#endregion
#region Private Method SaveClick
///
/// Saves the click (a. beging, 31.05.2022)
///
private async Task SaveClick()
{
if (IsUpdateMode)
{
var updateR = await ProspectService.UpdateAsync(Prospect);
if (updateR.Success && OnSuccess != null) await OnSuccess.Invoke();
}
else
{
var addR = await ProspectService.AddProspectAsync(Prospect);
if (addR.Success && OnSuccess != null) await OnSuccess.Invoke();
}
await ModalService.Hide();
}
#endregion
}
}