using BackOffice.Services.Tag;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BackOffice.Pages.Tag.Components;

public partial class TagEditDialog
{
    [CascadingParameter] IMudDialogInstance DialogInstance { get; set; } = default!;
    [Inject] public ITagService TagService { get; set; } = default!;

    [Parameter] public TagEditDto Model { get; set; } = new();
    [Parameter] public long? TagId { get; set; }
    [Parameter] public bool IsEditMode { get; set; }

    private bool _isEditMode => IsEditMode && TagId.HasValue;

    private async Task HandleValidSubmit()
    {
        await SubmitAsync();
    }

    private async Task SubmitAsync()
    {
        if (_isEditMode && TagId.HasValue)
        {
            await TagService.UpdateAsync(TagId.Value, Model);
        }
        else
        {
            await TagService.CreateAsync(Model);
        }

        DialogInstance.Close(DialogResult.Ok(true));
    }

    private void Cancel()
    {
        DialogInstance.Cancel();
    }
}

