@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
<MudStack class="my-auto text-center font-iranyekan">
    <p class="text-lg font-extrabold">افزودن شرح حال جدید</p>
    <p class="text-center text-md">
        لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از
        طراحان گرافیک است چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی
        مورد ده
    </p>
    <BasePartDivider Index="1" Title="شکایت اصلی بیمار" />
    
    <MudTextField Value="@PatientFirstName" ValueChanged="async detail => { PatientFirstName = detail; await PatientFirstNameChanged.InvokeAsync(detail); }"
        T="string" Label="نام بیمار" Variant="Variant.Outlined" />
    <MudTextField Value="@PatientLastName" ValueChanged="async detail => { PatientLastName = detail; await PatientLastNameChanged.InvokeAsync(detail); }"
        T="string" Label="نام خانوادگی بیمار" Variant="Variant.Outlined" />
    <MudTextField Value="@PatientAge" ValueChanged="async detail => { PatientAge = detail; await PatientAgeChanged.InvokeAsync(detail); }"
        T="int" Label="سن بیمار" Variant="Variant.Outlined" />
    <MudAutocomplete Value="@SelectedTemplate" 
                     ToStringFunc="dto => dto.ChiefComplaint"
                     SearchFunc="@SearchTemplates"
                     TextChanged="async str => { ChiefComplaint = str;await ChiefComplaintChanged.InvokeAsync(str); }"
                     ValueChanged="async dto => { SelectedTemplate = dto; await SelectedTemplateChanged.InvokeAsync(SelectedTemplate); }"
                     T="MedicalHistoryTemplateSDto" Label="شکایت اصلی بیمار ( CC )" 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>

    <MudAutocomplete Value="@SelectedSection" ToStringFunc="dto => dto.Name"
                     SearchFunc="@SearchSection"
                     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>
</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; }

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