using CMSMicroservice.Protobuf.Protos.UserRole;
using Google.Protobuf;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using MudBlazor;

namespace BackOffice.Pages.UserRole.Components
{
    public partial class UpdateDialog
    {
        private bool _isLoading = false;
        [Inject] public UserRoleContract.UserRoleContractClient UserRoleContract { get; set; }

        [Parameter]
        public UpdateUserRoleRequest Model { get; set; } = new();

        [CascadingParameter]
        IMudDialogInstance MudDialog { get; set; }
               
        public async void CallUpdateMethod()
        {
            _isLoading = true;
            StateHasChanged();
            if (Model.RoleId <= 0)
            {
                Snackbar.Add("لطفا نقش را وارد کنید!", Severity.Warning);
                _isLoading = false;
                StateHasChanged();
                return;
            }

            try
            {
                await UserRoleContract.UpdateUserRoleAsync(Model);
                Submit();

            }
            catch (Exception)
            {
                Snackbar.Add("خطای سرور", Severity.Error);
            }

            _isLoading = false;
            StateHasChanged();
        }

        void Submit() => MudDialog.Close(DialogResult.Ok(true));
        void Cancel() => MudDialog.Cancel();
    }
}