@using TechHelper.Client.Services
@inject IStudentSubmissionService StudentSubmissionService

<MudPaper Class="ma-2 pa-2 rounded-xl d-flex flex-column flex-grow-1 overflow-auto" MaxHeight="100%">

	<StudentSubmissionPreviewCard />
	@if (_isLoading)
	{
		<div class="d-flex justify-content-center align-items-center" style="height: 200px;">
			<MudProgressCircular Color="Color.Primary" Size="Size.Large" />
		</div>
	}
	else if (_studentSubmissions == null || _studentSubmissions.Count == 0)
	{
		<div class="d-flex justify-content-center align-items-center" style="height: 200px;">
			<MudText TextColor="Color.TextSecondary" Align="Align.Center">暂无提交记录</MudText>
		</div>
	}
	else
	{
		@foreach (var submission in _studentSubmissions)
		{
			<StudentSubmissionPreviewCard StudentSubmission="@submission" />
		}
	}
</MudPaper>

@code {
	// 学生提交数据模型
	public class StudentSubmission
	{
		public string StudentName { get; set; }
		public int TotalProblems { get; set; }
		public int ErrorCount { get; set; }
		public DateTime CreatedDate { get; set; }
		public float Score { get; set; }
		public string AssignmentName { get; set; }
		public string Status { get; set; }
		public TimeSpan TimeSpent { get; set; }
	}

	// 学生提交列表
	private List<StudentSubmission> _studentSubmissions = new();
	private bool _isLoading = true;

	protected override async Task OnInitializedAsync()
	{
		await LoadStudentSubmissions();
	}

	private async Task LoadStudentSubmissions()
	{
		try
		{
			_isLoading = true;
			StateHasChanged();

			var result = await StudentSubmissionService.GetMySubmissionsAsync();
			
			if (result.Status && result.Result != null)
			{
				// 从服务器获取的数据映射到我们的模型
				var submissions = result.Result as List<Entities.DTO.StudentSubmissionSummaryDto>;
				
				if (submissions != null)
				{
					_studentSubmissions = submissions.Select(submission => new StudentSubmission
					{
						AssignmentName = submission.AssignmentName,
						CreatedDate = submission.CreatedDate,
						ErrorCount = submission.ErrorCount,
						Score = submission.Score,
						StudentName = submission.StudentName,
						Status = submission.Status,
						TotalProblems = submission.TotalQuestions,
						TimeSpent = TimeSpan.FromMinutes(30) // 默认值，实际应用中可以从服务器获取
					}).ToList();
				}
			}
			else
			{
				// 如果API调用失败，使用空列表
				_studentSubmissions = new List<StudentSubmission>();
			}
		}
		catch (Exception ex)
		{
			// 处理异常，可以记录日志
			_studentSubmissions = new List<StudentSubmission>();
		}
		finally
		{
			_isLoading = false;
			StateHasChanged();
		}
	}
}
