<MudStack class="pb-20 font-iranyekan">
    <BasePartDivider Index="9" Title="ظاهر کلی بیمار ( GA )" />

    <div class="grid grid-cols-2 gap-1 md:grid-cols-4 sm:grid-cols-2">
        @foreach (var question in GaQuestions)
        {
            <BaseMedicalQuestionTemplate Question="@question" OldAnswer="@question.Answer" AnswerChanged="GaAnswerChanged" />
        }

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


    <BasePartDivider Index="10" Title="علائم حیاتی ( VS )" />
    <MudFocusTrap>
        <div class="flex flex-row">
            <p class="my-auto mr-5 font-extrabold text-md grow">فشــــار خون</p>
            <MudNumericField Value="@SystolicBloodPressure" ValueChanged="async detail => { SystolicBloodPressure = detail; await SystolicBloodPressureChanged.InvokeAsync(detail); }"
                             Label="سیستولیک"
                             Step=".5"
                             Min="0.0"
                             Margin="Margin.Dense"
                             class="mx-3 my-3 basis-1/12" T="double" Variant="Variant.Outlined" />
            <MudNumericField Value="@DiastolicBloodPressure" ValueChanged="async detail => { DiastolicBloodPressure = detail; await DiastolicBloodPressureChanged.InvokeAsync(detail); }"
                             Label="دیاستولیک"
                             Step=".5"
                             Min="0.0"
                             Margin="Margin.Dense"
                             class="my-3 basis-1/12" T="double" Variant="Variant.Outlined" />


        </div>
        <div class="grid grid-cols-3">


            <MudNumericField Min="0"
                             Value="@PulseRate" ValueChanged="async detail => { PulseRate = detail; await PulseRateChanged.InvokeAsync(detail); }"
                             Margin="Margin.Dense" class="my-3" Label="نبض" T="double" Variant="Variant.Outlined" />

            <MudNumericField Min="0"
                             Value="@SPO2" ValueChanged="async detail => { SPO2 = detail; await SPO2Changed.InvokeAsync(detail); }"
                             class="mx-2 my-3" Margin="Margin.Dense" Max="100" Label="اکسیژن" T="double" Variant="Variant.Outlined" />

            <MudNumericField Value="@Temperature"
                             Step=".1"
                             ValueChanged="async detail => { Temperature = detail; await TemperatureChanged.InvokeAsync(detail); }"
                             Margin="Margin.Dense" class="my-3" Format="F1" Min="0.0" Label="دمای بدن" T="double" Variant="Variant.Outlined" />
        </div>
    </MudFocusTrap>


    <BasePartDivider Index="11" Title="بررسی سیستماتیک ( ROS )" />
    <div class="grid gap-1 md:grid-cols-2">
        @foreach (var question in RosQuestions)
        {
            <BaseMedicalQuestionTemplate Question="@question" OldAnswer="@question.Answer" AnswerChanged="RosAnswerChanged" />
        }

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


</MudStack>
@code {

    protected override void OnParametersSet()
    {
        if (GaAnswers.Count > 0)
        {
            foreach (var question in GaQuestions)
            {
                var answer = GaAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        if (RosAnswers.Count > 0)
        {
            foreach (var question in RosQuestions)
            {
                var answer = RosAnswers.FirstOrDefault(a => a.Question == question.Question && a.Part == question.Part);
                if (answer != null)
                    question.Answer = answer;
            }
        }
        base.OnParametersSet();
    }
    [Parameter]
    public string RosDetail { get; set; } = string.Empty;

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

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

    [Parameter]
    public List<MedicalHistoryQuestionSDto> GaQuestions { get; set; } = new();
    private void GaAnswerChanged(MedicalHistoryAnswerSDto dto)
    {
        var findAnswer = GaAnswers.FirstOrDefault(pi => pi.Question == dto.Question && pi.Part == dto.Part);
        if (findAnswer != null)
            findAnswer.Answer = dto.Answer;
        else
            GaAnswers.Add(dto);
    }

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

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

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

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

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


    [Parameter]
    public double SystolicBloodPressure { get; set; }

    [Parameter]
    public EventCallback<double> SystolicBloodPressureChanged { get; set; }


    [Parameter]
    public double DiastolicBloodPressure { get; set; }

    [Parameter]
    public EventCallback<double> DiastolicBloodPressureChanged { get; set; }


    [Parameter]
    public double PulseRate { get; set; }

    [Parameter]
    public EventCallback<double> PulseRateChanged { get; set; }


    [Parameter]
    public double SPO2 { get; set; }

    [Parameter]
    public EventCallback<double> SPO2Changed { get; set; }


    [Parameter]
    public double Temperature { get; set; }

    [Parameter]
    public EventCallback<double> TemperatureChanged { get; set; }
}