@page "/exam/edit/{ExamId}"
@using Entities.DTO
@using TechHelper.Client.Pages.Exam.ExamView
@using TechHelper.Client.Services
@using Entities.DTO
@using TechHelper.Client.Exam

<ExamView ParsedExam="@ExamDto"/>

@code {
	[Parameter]
	public string ExamId { get; set; }

	[Inject]
	public IExamService ExamService { get; set; }

	[Inject]
	private NavigationManager Navigation { get; set; }

	[Inject]
	private ISnackbar Snackbar { get; set; }

	private AssignmentDto ExamDto { get; set; }

	protected override async Task OnInitializedAsync()
	{

		if (Guid.TryParse(ExamId, out Guid parsedExamId))
		{
			Console.WriteLine($"ExamId 字符串成功解析为 Guid: {parsedExamId}");
			try
			{
				var result = await ExamService.GetExam(parsedExamId);
				if (result.Status) ExamDto = result.Result as AssignmentDto ?? new AssignmentDto();
			}
			catch (Exception ex)
			{
				Snackbar?.Add($"获取试卷失败: {ex.Message}", Severity.Error);
			}
		}
		else
		{
			Console.Error.WriteLine($"错误：路由参数 ExamId '{ExamId}' 不是一个有效的 GUID 格式。无法获取试卷信息。");
			Navigation.NavigateTo("/exam/manager");
			Snackbar?.Add("无效的试卷ID，无法加载。", Severity.Error);
		}
	}
}