147 lines
4.0 KiB
C#
147 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WguApp.Controls;
|
|
using WguApp.Models;
|
|
using WguApp.Services;
|
|
|
|
namespace WguApp.Views;
|
|
|
|
public partial class TermPage : ContentPage
|
|
{
|
|
|
|
private Term _term;
|
|
|
|
private List<Course> _courses = [];
|
|
|
|
public TermPage(Term term)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_term = term;
|
|
|
|
Title = string.IsNullOrWhiteSpace(_term.Name) ? "Term" : _term.Name;
|
|
TitleEntry.Text = _term.Name;
|
|
|
|
StartDatePicker.Date = _term.StartDate;
|
|
EndDatePicker.Date = _term.EndDate;
|
|
}
|
|
|
|
private async Task RefreshCourseList()
|
|
{
|
|
_courses.Clear();
|
|
_courses = await DatabaseService.GetCoursesByTermId(_term.Id);
|
|
|
|
CourseButtonStack.Children.Clear();
|
|
|
|
foreach (var course in _courses)
|
|
{
|
|
var btn = new CustomButton()
|
|
{
|
|
Text = course.Name,
|
|
DateText = $"{DateOnly.FromDateTime(course.StartDate).ToString()} - {DateOnly.FromDateTime(course.EndDate).ToString()}"
|
|
};
|
|
|
|
CourseButtonStack.Add(btn);
|
|
|
|
btn.Clicked += (sender, e) =>
|
|
{
|
|
HandleCourseButtonClick(course);
|
|
};
|
|
}
|
|
}
|
|
|
|
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
|
|
{
|
|
try
|
|
{
|
|
await RefreshCourseList();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
await DisplayAlert("Error", e.Message, "Ok");
|
|
}
|
|
}
|
|
|
|
private async void HandleCourseButtonClick(Course course)
|
|
{
|
|
await Navigation.PushAsync(new CoursePage(course));
|
|
}
|
|
|
|
private async void SaveButton_OnClicked(object? sender, EventArgs e)
|
|
{
|
|
var validationResult = ValidateFields();
|
|
|
|
if (!validationResult.result)
|
|
{
|
|
await DisplayAlert("Validation Error", validationResult.msg, "Ok");
|
|
return;
|
|
}
|
|
|
|
_term.Name = TitleEntry.Text;
|
|
_term.StartDate = StartDatePicker.Date;
|
|
_term.EndDate = EndDatePicker.Date;
|
|
|
|
var updateResult = await DatabaseService.UpdateTerm(_term);
|
|
|
|
if (updateResult)
|
|
{
|
|
await DisplayAlert("Info", $"{_term.Name} Saved!", "Ok");
|
|
}
|
|
else
|
|
{
|
|
await DisplayAlert("Error", "Could not save term", "Ok");
|
|
}
|
|
|
|
Title = _term.Name;
|
|
}
|
|
|
|
private (string msg, bool result) ValidateFields()
|
|
{
|
|
var message = string.Empty;
|
|
|
|
if (string.IsNullOrWhiteSpace(TitleEntry.Text)) message += "Title cannot be blank\n";
|
|
if (StartDatePicker.Date > EndDatePicker.Date) message += "Start Date cannot be after the End Date\n";
|
|
|
|
return string.IsNullOrWhiteSpace(message) ? (message, true) : (message, false);
|
|
}
|
|
|
|
private async void DeleteButton_OnClicked(object? sender, EventArgs e)
|
|
{
|
|
var result = await DisplayAlert("Delete Term", $"Do you really want to delete '{_term.Name}'? This action cannot be undone.", "Delete", "Cancel");
|
|
if (!result) return;
|
|
|
|
await DatabaseService.DeleteTerm(_term);
|
|
await Navigation.PopAsync();
|
|
}
|
|
|
|
private async void AddCourseButton_OnClicked(object? sender, EventArgs e)
|
|
{
|
|
if (_courses.Count >= 6)
|
|
{
|
|
await DisplayAlert("Info", $"Cannot add more than 6 courses per term", "Ok");
|
|
return;
|
|
}
|
|
|
|
var course = new Course()
|
|
{
|
|
TermId = _term.Id,
|
|
Name = "New Course",
|
|
StartDate = DateTime.Today,
|
|
EndDate = DateTime.Today.AddDays(30),
|
|
InstructorName = "",
|
|
InstructorEmail = "",
|
|
InstructorPhone = "",
|
|
StartNotifCheck = false,
|
|
EndNotifCheck = false,
|
|
Status = CourseStatus.Planned,
|
|
Notes = ""
|
|
};
|
|
|
|
await DatabaseService.AddCourse(course);
|
|
|
|
await RefreshCourseList();
|
|
}
|
|
} |