c969-project/C969Project/Forms/AppointmentsForm.cs
2025-06-26 21:10:19 -05:00

93 lines
3.5 KiB
C#

using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project;
public partial class AppointmentsForm : Form
{
private List<Appointment> _appointments = new();
private AddOrUpdateAppointmentForm _addOrUpdateAppointmentForm = new();
public AppointmentsForm()
{
InitializeComponent();
UpdateAppointmentsList(null, EventArgs.Empty);
Shown += UpdateAppointmentsList;
_addOrUpdateAppointmentForm.UpdateAppointmentsList += UpdateAppointmentsList;
calendar.DateSelected += ShowAppointmentsFromSelectedDay;
showAllButton.Click += UpdateAppointmentsList;
deleteButton.Click += DeleteButtonOnClick;
addButton.Click += AddButtonOnClick;
modifyButton.Click += ModifyButtonOnClick;
appointmentDataView.SelectionChanged += UpdateAppointmentDetails;
}
private void ModifyButtonOnClick(object? sender, EventArgs e)
{
if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment selectedAppointment)
{
_addOrUpdateAppointmentForm.InitUpdate(selectedAppointment);
}
else
{
MessageBox.Show("Error while trying to modify appointment");
}
}
private void AddButtonOnClick(object? sender, EventArgs e)
{
_addOrUpdateAppointmentForm.InitAdd();
}
private void DeleteButtonOnClick(object? sender, EventArgs e)
{
Appointment? selectedAppointment;
if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment appointment)
{
selectedAppointment = appointment;
}
else
{
MessageBox.Show("Error while trying to delete appointment");
return;
}
var result = MessageBox.Show("Are you sure you want to delete this appointment?", "Delete Appointment?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
DatabaseHelper.DeleteAppointment(selectedAppointment);
UpdateAppointmentsList(null, EventArgs.Empty);
}
}
private void UpdateAppointmentsList(object? sender, EventArgs e)
{
appointmentDataView.DataSource = null;
_appointments.Clear();
_appointments.AddRange(DatabaseHelper.RetrieveAppointments(AppState.CurrentUser.UserId));
appointmentDataView.DataSource = _appointments;
}
private void ShowAppointmentsFromSelectedDay(object? sender, EventArgs e)
{
var startDate = calendar.SelectionStart.ToUniversalTime();
var selectedDate = startDate.Date;
List<Appointment>? selectedAppointments = _appointments.Where(a => a.Start.Date == selectedDate).ToList();
appointmentDataView.DataSource = selectedAppointments?.Count > 0 ? selectedAppointments : null;
}
private void UpdateAppointmentDetails(object? sender, EventArgs e)
{
if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment appointment)
{
apptTitleLabel.Text = $"Title: {appointment.Title}";
apptTypeLabel.Text = $"Type: {appointment.AppointmentType}";
withLabel.Text = $"With: {DatabaseHelper.RetrieveCustomer(appointment.CustomerId)?.CustomerName ?? "Unknown"}";
startTimeLabel.Text = $"Start: {appointment.Start.ToLocalTime():MM/dd/yyyy hh:mm tt} {TimeZoneInfo.Local.Id}";
endTimeLabel.Text = $"End: {appointment.End.ToLocalTime():MM/dd/yyyy hh:mm tt} {TimeZoneInfo.Local.Id}";
}
}
}