@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
<MudStack class="my-auto text-center font-iranyekan pb-20">
    
    <div class="-mb-5">
        <dotlottie-player src="https://lottie.host/e7dc67f6-baa3-44c1-b136-3b6e04685fb1/pb8uFg7FnQ.json"
                          background="transparent" speed="1" class="m-auto w-64 h-64" loop autoplay />
    </div>
    <p class="text-lg font-extrabold">افزودن پیش نویس شرح حال جدید</p>
    <p class="text-center text-md">اطلاعات هر بخش شرح حال را میتوانید به صورت پیش نویس وارد کنید و در شرح حال نویسی خود را سریع و کارامد کنید </p>
    <BasePartDivider Index="1" Title="شکایت اصلی بیمار"/>
    <MudTextField T="string" Value="@ChiefComplaint" ValueChanged="async cc => {ChiefComplaint = cc; await ChiefComplaintChanged.InvokeAsync(ChiefComplaint); }"
                  Label="شکایت اصلی بیمار ( CC )" Variant="Variant.Outlined" Margin="Margin.Normal"/>
    <MudAutocomplete Value="@SelectedSection" T="SectionSDto" Label="بخش بیمار" Variant="Variant.Outlined"
                     ToStringFunc="dto => dto.Name"
                     SearchFunc="@SearchSection"
                     ValueChanged="async dto => { SelectedSection = dto; await SelectedSectionChanged.InvokeAsync(SelectedSection); }">

        <ProgressIndicatorInPopoverTemplate>
            <MudList Clickable="false">
                <MudListItem>
                    <div class="flex flex-row w-full mx-auto">
                        <MudProgressCircular class="my-auto mr-1 -ml-4" Size="Size.Small" Indeterminate="true"/>
                        <p class="font-bold my-1 mx-auto text-md">منتظر بمانید</p>
                    </div>
                </MudListItem>
            </MudList>
        </ProgressIndicatorInPopoverTemplate>
        <ItemTemplate Context="e">
            <p>@e.Name</p>
        </ItemTemplate>
    </MudAutocomplete>
    @if (IsEditing)
    {
        <BaseButtonUi Icon="@Icons.Material.TwoTone.Close"
        @onclick="@DeleteClicked" Content="حذف شرح حال" Variant="Variant.Outlined" Color="Color.Error" />
    }
</MudStack>

@code {


    [Parameter]
    public EventCallback DeleteClicked { get; set; }

    [Parameter]
    public bool IsEditing { get; set; }

    [Parameter]
    public string ChiefComplaint { get; set; } = string.Empty;
    [Parameter]
    public EventCallback<string> ChiefComplaintChanged { get; set; }

    [Parameter]
    public SectionSDto SelectedSection { get; set; } = new();
    [Parameter]
    public EventCallback<SectionSDto> SelectedSectionChanged { get; set; }

    private MedicalHistoryTemplateLDto _editingTemplate = new();

    public List<SectionSDto> Sections { get; private set; } = new List<SectionSDto>();

    public async Task<IEnumerable<SectionSDto>> SearchSection(string section)
    {
        try
        {
            var token = await UserUtility.GetBearerTokenAsync();
            var user = await UserUtility.GetUserAsync();
            Sections = await RestWrapper.SectionRestApi.GetByUniversityAsync(user.UniversityId, token);

            if (section.IsNullOrEmpty())
                return Sections;
            return Sections.Where(c => c.Name.Contains(section));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            return Sections;
        }
        catch (Exception e)
        {
            return Sections;
        }
    }

}