@if (IsSelected)
{
	<MudPaper Outlined="true" Class="ma-1 pa-1">
		<MudGrid>
			<MudItem xs="12" md="8">



				<MudStack>
					<MudStack Row="true" Spacing="2">
						<MudText Typo="Typo.caption" Color="Color.Primary">（选中状态，在此编辑具体题目内容）</MudText>
					</MudStack>

					<MudTextField @bind-Value="QuestionItem.Question.QuestionText" AutoGrow="true"></MudTextField>
					<MudNumericField @bind-Value="Num" Label="选项数量" Variant="Variant.Outlined" Dense="true" Min="0" Max="byte.MaxValue" />
					<MudTextField @bind-Value="QuestionItem.Question.CorrectAnswer" AutoGrow="true"></MudTextField>
				</MudStack>
			</MudItem>
			<MudItem xs="12" md="4">


				<MudStack>

					@for (int index = 0; index < QuestionItem.Radio.Count; index++)
					{
						var tempIndex = index;

						<MudTextField @bind-Value="QuestionItem.Radio[tempIndex]"
						Label="@($"选项 {tempIndex + 1}")"
						Color="Color.Primary"
						Variant="Variant.Outlined" Dense="true">
						</MudTextField>
					}
				</MudStack>

				@if (QuestionItem.Radio.Count > 0)
				{
					<MudRadioGroup T="string" @bind-Value="QuestionItem.Question.CorrectAnswer">
						<MudText Typo="Typo.body2">设置正确选项:</MudText>
						@foreach (var optionText in QuestionItem.Radio)
						{
							<MudRadio Value="@optionText" Color="Color.Success">@optionText</MudRadio>
						}
					</MudRadioGroup>
				}

			</MudItem>

		</MudGrid>
	</MudPaper>

}

<MudStack Spacing="1">
	<MudText> @QuestionItem.Title </MudText>
	@if (QuestionItem.Radio.Count > 0)
	{
		<MudRadioGroup T="string" ReadOnly="true">
			@foreach (var optionText in QuestionItem.Radio)
			{
				<MudRadio Value="@optionText">@optionText</MudRadio>
			}
		</MudRadioGroup>
	}

	<MudText Typo="Typo.body2">
		正确答案: @(QuestionItem?.Question.CorrectAnswer ?? "(未设置)")
	</MudText>

</MudStack>


@using Microsoft.AspNetCore.Components.Web
@code {
	private bool _oldIsSelected;
	private const string OptionsDelimiter = "[OPTIONS]";
	private const string ItemDelimiter = "[SEP]";

	public string SelectedOption { get; set; }
	public int Num
	{
		get { return QuestionItem.Radio.Count; }
		set
		{
			if (QuestionItem.Radio == null)
			{
				QuestionItem.Radio = new List<string>();
			}
			int targetCount = value;
			while (QuestionItem.Radio.Count < targetCount)
			{
				QuestionItem.Radio.Add($"选项 {QuestionItem.Radio.Count + 1}");
			}

			while (QuestionItem.Radio.Count > targetCount)
			{
				QuestionItem.Radio.RemoveAt(QuestionItem.Radio.Count - 1);
			}

			StateHasChanged();
		}
	}

	[Parameter]
	public QuestionItem QuestionItem { get; set; }


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

	protected override async Task OnParametersSetAsync()
	{
		if (IsSelected && !_oldIsSelected)
		{
			ParseCombinedString(QuestionItem.Question.QuestionText);
		}
		else if (!IsSelected && _oldIsSelected)
		{
			CombineDataIntoString(QuestionItem.Question);
		}

		_oldIsSelected = IsSelected;

		await base.OnParametersSetAsync();
	}

	private void ParseCombinedString(string combinedText)
	{
		QuestionItem.Radio.Clear();

		if (!string.IsNullOrEmpty(combinedText) && combinedText.Contains(OptionsDelimiter))
		{
			var parts = combinedText.Split(new[] { OptionsDelimiter }, 2, StringSplitOptions.None);

			if (parts.Length == 2)
			{
				QuestionItem.Question.QuestionText = parts[0];

				var optionStrings = parts[1].Split(new[] { ItemDelimiter }, StringSplitOptions.None);

				foreach (var opt in optionStrings)
				{
					QuestionItem.Radio.Add(opt);
				}
			}
			else
			{
				QuestionItem.Question.QuestionText = combinedText;
			}
		}
		else
		{
		}
		StateHasChanged();
	}

	private void CombineDataIntoString(Question questionToUpdate)
	{
		QuestionItem.Title = QuestionItem.Question.QuestionText;
		string combinedText = questionToUpdate.QuestionText + OptionsDelimiter + string.Join(ItemDelimiter, QuestionItem.Radio);

		questionToUpdate.QuestionText = combinedText;
	}

}