@using Common.Dtos.Exper
@using HushianWebApp.Service
@inject GroupService groupService;
@inject UserService userService;
@inject HushianWebApp.Services.ILocalStorageService localStorageService;
@inject ChatService chatService;
@inject ToastService toastService
@if (!Spinnervisible)
{
    <div class="row">
        <div class="col-md-12 col-sm-12" style="margin-bottom:15px">
            <AutoComplete @bind-Value="ExperName"
            TItem="Read_ExperDto"
            DataProvider="DataProvider"
            PropertyName="FullName"
            Placeholder="جستجو در کاربران..."
            OnChanged="(Read_ExperDto exper) => OnAutoCompleteChanged(exper)"/>
        </div>
    </div>
}
@code {
    private string? ExperName;
    private bool Spinnervisible = false;
    [Parameter]  public int chatID { get; set; }
    [Parameter] public EventCallback OnMultipleOfThree { get; set; }
    public List<Read_ExperDto> CoExpers { get; set; }
    = new();
    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
    }
}
    @functions {
    private async Task<AutoCompleteDataProviderResult<Read_ExperDto>> DataProvider(AutoCompleteDataProviderRequest<Read_ExperDto> request)
    {
        CoExpers = await userService.GetExpersCompany();
        return await Task.FromResult(new AutoCompleteDataProviderResult<Read_ExperDto> { Data = CoExpers.Where(w => w.FullName.Contains(request.Filter.Value)), TotalCount = CoExpers.Count() });
    }

    private async Task OnAutoCompleteChanged(Read_ExperDto exper)
    {
        Spinnervisible = true;
        if(await chatService.Attachedto(exper.ID, chatID))
        {
           await OnMultipleOfThree.InvokeAsync();
        }
        else toastService.Notify(new ToastMessage(ToastType.Danger, "خطا در پیوست"));
        Spinnervisible = false;
    }
   }
