@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
<MudStack class="my-auto text-center font-iranyekan pb-20">
    <p class="text-lg font-extrabold">افزودن شرح حال جدید</p>
    <p class="text-center text-md">شرح حال بیمار را در مراحل مختلف و اطلاعات را با دقت وارد نمایید ، در مرحله اخر میتوانید شرحال کامل را مشاهده کنید
    </p>
    <BasePartDivider Index="1" Title="شکایت اصلی بیمار" />
    
    <MudForm>
        <MudFocusTrap>
            <MudTextField Required="true" RequiredError="نام بیمار خود را وارد کنید" Value="@PatientFirstName" ValueChanged="async detail => { PatientFirstName = detail; await PatientFirstNameChanged.InvokeAsync(detail); }"
                          T="string" Label="نام بیمار" Variant="Variant.Outlined"  class="text-sm my-5" />
            <MudTextField Required="true" RequiredError="نام خانوادگی بیمار خود را وارد کنید" Value="@PatientLastName" ValueChanged="async detail => { PatientLastName = detail; await PatientLastNameChanged.InvokeAsync(detail); }"
                          T="string" Label="نام خانوادگی بیمار" Variant="Variant.Outlined" class="text-sm my-5" />
            <MudNumericField Required="true" RequiredError="سن بیمار خود را وارد کنید" Value="@PatientAge"
                             ValueChanged="async detail => { PatientAge = detail; await PatientAgeChanged.InvokeAsync(detail); }"
                             Min="0"
                             T="int" Label="سن بیمار" Variant="Variant.Outlined" />



            <MudAutocomplete Required="true" RequiredError="بخش بیمار خود را وارد کنید" Value="@SelectedSection" ToStringFunc="dto => dto.Name"
                             SearchFunc="@SearchSection"
                             class="text-sm my-5"
                             ValueChanged="async dto => { SelectedSection = dto; await SelectedSectionChanged.InvokeAsync(SelectedSection); }"
                             T="SectionSDto" Label="بخش بیمار" Variant="Variant.Outlined">
                <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>

            <MudTextField Required="true" RequiredError="شکایت اصلی بیمار را وارد کنید" Value="@ChiefComplaint" ValueChanged="async detail => { ChiefComplaint = detail; await ChiefComplaintChanged.InvokeAsync(detail); }"
                          T="string" Label="شکایت اصلی بیمار" Variant="Variant.Outlined" class="text-sm my-5" />
            
            <MudAutocomplete Value="@SelectedTemplate"
                             CoerceValue="true"
                             class="text-sm my-5"
                             ToStringFunc="dto => dto.ChiefComplaint"
                             SearchFunc="@SearchTemplates"
                             ValueChanged="async dto => { SelectedTemplate = dto; await SelectedTemplateChanged.InvokeAsync(SelectedTemplate); }"
                             T="MedicalHistoryTemplateSDto" Label="انتخاب از پیش نویس ها" Variant="Variant.Outlined">
                <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.ChiefComplaint</p>
                </ItemTemplate>
            </MudAutocomplete>

        </MudFocusTrap>
        
        

    </MudForm>
    @if (IsEditing)
    {
        <BaseButtonUi Icon="@Icons.Material.TwoTone.Close"
                      @onclick="@DeleteClicked" Content="حذف شرح حال" Variant="Variant.Outlined" Color="Color.Error" />
    }
</MudStack>

@code
{
    
    [Parameter]
    public string PatientFirstName { get; set; } = string.Empty;
    [Parameter]
    public EventCallback<string> PatientFirstNameChanged { get; set; }


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


    [Parameter]
    public int PatientAge { get; set; }
    [Parameter]
    public EventCallback<int> PatientAgeChanged { 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; }


    [Parameter]
    public MedicalHistoryTemplateSDto SelectedTemplate { get; set; } = new();
    [Parameter]
    public EventCallback<MedicalHistoryTemplateSDto> SelectedTemplateChanged { get; set; }

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

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

    private MedicalHistoryTemplateLDto _editingTemplate = new();

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

    public List<MedicalHistoryTemplateSDto> Templates { get; private set; } = new List<MedicalHistoryTemplateSDto>();


    public async Task<IEnumerable<MedicalHistoryTemplateSDto>> SearchTemplates(string template)
    {
        try
        {
            var token = await UserUtility.GetBearerTokenAsync();
            var list = await RestWrapper
                .CrudDtoApiRest<MedicalHistoryTemplateLDto, MedicalHistoryTemplateSDto, Guid>(Address.MedicalHistoryTemplateController)
                .ReadAll(0, token);
            Templates = list;
            if (template.IsNullOrEmpty())
                return Templates;
            return Templates.Where(c => c.ChiefComplaint.Contains(template));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            return Templates;
        }
        catch (Exception e)
        {
            return Templates;
        }
    }

    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;
        }
    }
}