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

namespace BackOffice.Pages.Blog.Components;

public partial class BlogCategoryEditDialog
{
    [CascadingParameter] IMudDialogInstance DialogInstance { get; set; } = default!;
    [Inject] public IBlogCategoryService BlogCategoryService { get; set; } = default!;

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

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

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

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

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

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