<MudStack class="pb-20 font-iranyekan">
    <BasePartDivider Index="5" Title="تاریخچه بیماری های خانوادگی ( FH )" />
    @foreach (var question in FhQuestions)
    {
        <BaseMedicalQuestionTemplate Question="@question" Answer="@question.Answer" AnswerChanged="FhAnswerChanged" />
    }
    <MudTextField T="string"
                  Value="@FhDetail" ValueChanged="async detail => { FhDetail = detail; await FhDetailChanged.InvokeAsync(detail); }"
                  Label="توضیحاتـــ تکمیلی" Lines="5" Variant="Variant.Outlined" />


    <BasePartDivider Index="6" Title="داروهای مصرفی ( DH )" />
    <div class="grid grid-cols-2 gap-1 md:grid-cols-4 sm:grid-cols-2">
        @foreach (var question in DhQuestions)
        {
            <BaseMedicalQuestionTemplate Question="@question" OldAnswer="@question.Answer" AnswerChanged="DhAnswerChanged" />
        }
    </div>

    <MudTextField T="string"
                  Value="@DhDetail" ValueChanged="async detail => { DhDetail = detail; await DhDetailChanged.InvokeAsync(detail); }"
                  Label="توضیحاتـــ تکمیلی" Lines="2" Variant="Variant.Outlined" />

    <BasePartDivider Index="7" Title="مواد مصرفی ( HH )" />
    <div class="grid grid-cols-2 gap-1 md:grid-cols-4 sm:grid-cols-2">
        @foreach (var question in HhQuestions)
        {
            <BaseMedicalQuestionTemplate Question="@question" OldAnswer="@question.Answer" AnswerChanged="HhAnswerChanged" />
        }
    </div>

    <MudTextField T="string" 
                  Value="@HhDetail" ValueChanged="async detail => { HhDetail = detail; await HhDetailChanged.InvokeAsync(detail); }"
                  Label="توضیحاتـــ تکمیلی" Lines="2" Variant="Variant.Outlined" />


    <BasePartDivider Index="8" Title="آلرژی ( AH )" />

    <MudTextField T="string" 
                  Value="@AhDetail" ValueChanged="async ahDetail => { AhDetail = ahDetail; await AhDetailChanged.InvokeAsync(ahDetail); }" 
                  Label="توضیحاتـــ تکمیلی" Lines="2" Variant="Variant.Outlined" />



</MudStack>
@code {

    protected override void OnParametersSet()
    {
        if (FhAnswers.Count > 0)
        {
            foreach (var question in FhQuestions)
            {
                var answer = FhAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        if (DhAnswers.Count > 0)
        {
            foreach (var question in DhQuestions)
            {
                var answer = DhAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        if (HhAnswers.Count > 0)
        {
            foreach (var question in HhQuestions)
            {
                var answer = HhAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        base.OnParametersSet();
    }
    [Parameter]
    public string AhDetail { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<string> AhDetailChanged { get; set; }

    [Parameter]
    public List<MedicalHistoryAnswerSDto> FhAnswers { get; set; } = new();

    [Parameter]
    public List<MedicalHistoryQuestionSDto> FhQuestions { get; set; } = new();

    [Parameter]
    public string FhDetail { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<string> FhDetailChanged { get; set; }

    private void FhAnswerChanged(MedicalHistoryAnswerSDto dto)
    {
        var findAnswer = FhAnswers.FirstOrDefault(pi => pi.Question == dto.Question && pi.Part == dto.Part);
        if (findAnswer != null)
            findAnswer.Answer = dto.Answer;
        else
            FhAnswers.Add(dto);
    }

    [Parameter]
    public List<MedicalHistoryAnswerSDto> DhAnswers { get; set; } = new();

    [Parameter]
    public List<MedicalHistoryQuestionSDto> DhQuestions { get; set; } = new();

    [Parameter]
    public string DhDetail { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<string> DhDetailChanged { get; set; }

    private void DhAnswerChanged(MedicalHistoryAnswerSDto dto)
    {
        var findAnswer = DhAnswers.FirstOrDefault(pi => pi.Question == dto.Question && pi.Part == dto.Part);
        if (findAnswer != null)
            findAnswer.Answer = dto.Answer;
        else
            DhAnswers.Add(dto);
    }

    [Parameter]
    public List<MedicalHistoryAnswerSDto> HhAnswers { get; set; } = new();

    [Parameter]
    public List<MedicalHistoryQuestionSDto> HhQuestions { get; set; } = new();

    [Parameter]
    public string HhDetail { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<string> HhDetailChanged { get; set; }

    private void HhAnswerChanged(MedicalHistoryAnswerSDto dto)
    {
        var findAnswer = HhAnswers.FirstOrDefault(pi => pi.Question == dto.Question && pi.Part == dto.Part);
        if (findAnswer != null)
            findAnswer.Answer = dto.Answer;
        else
            HhAnswers.Add(dto);
    }
}