@using System.Collections.ObjectModel

<MudStack class="font-iranyekan">
    <BasePartDivider Index="2" Title="تاریخچه بیماری فعلی ( PI )" />
    
    @foreach (var question in PiQuestions)
    {
        <BaseMedicalQuestionTemplate Question="@question" Answer="@question.Answer" AnswerChanged="AnswerChanged" />
    }


    
    <MudTextField T="string" Value="@PiDetail" ValueChanged="async piDetail => { PiDetail = piDetail; await PiDetailChanged.InvokeAsync(piDetail); }" Label="توضیحاتـــ تکمیلی" Lines="5" Variant="Variant.Outlined" />
</MudStack>


@code 
{
    protected override void OnParametersSet()
    {
        if (PiAnswers.Count > 0)
        {
            foreach (var question in PiQuestions)
            {
                var answer = PiAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        base.OnParametersSet();
    }

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

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

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

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

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

    }
}