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

182 lines
6.8 KiB
C#

using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project;
public partial class AddOrUpdateAppointmentForm : Form
{
public event EventHandler UpdateAppointmentsList;
private Appointment? _currentAppointment;
private List<string> _appointmentTypes = new ()
{
"Scrum",
"Presentation",
"Other"
};
private List<Customer> _customers = new();
public AddOrUpdateAppointmentForm()
{
InitializeComponent();
RefreshCustomersList();
typeComboBox.DataSource = _appointmentTypes;
saveButton.Click += SaveButton_Click;
cancelButton.Click += (s, e) => Close();
}
private void RefreshCustomersList()
{
customerComboBox.DataSource = null;
_customers.Clear();
_customers.AddRange(DatabaseHelper.RetrieveCustomers());
customerComboBox.DataSource = _customers;
}
public void InitAdd()
{
RefreshCustomersList();
label1.Text = "Add New Appointment";
_currentAppointment = null;
titleTextBox.Text = string.Empty;
descriptionTextBox.Text = string.Empty;
locationTextBox.Text = string.Empty;
contactTextBox.Text = string.Empty;
textBoxUrl.Text = string.Empty;
typeComboBox.SelectedIndex = -1;
customerComboBox.SelectedIndex = -1;
startPickerDate.Value = DateTime.Now.Date;
startPickerTime.Value = DateTime.Now;
endPickerDate.Value = DateTime.Now.Date;
endPickerTime.Value = DateTime.Now.AddHours(1);
ShowDialog();
}
public void InitUpdate(Appointment appointment)
{
RefreshCustomersList();
label1.Text = "Update Appointment";
_currentAppointment = appointment;
titleTextBox.Text = appointment.Title;
descriptionTextBox.Text = appointment.Description;
locationTextBox.Text = appointment.Location;
contactTextBox.Text = appointment.Contact;
textBoxUrl.Text = appointment.Url;
typeComboBox.SelectedItem = appointment.AppointmentType;
customerComboBox.SelectedItem = _customers.FirstOrDefault(c => c.CustomerId == appointment.CustomerId);
startPickerDate.Value = appointment.Start.ToLocalTime().Date;
startPickerTime.Value = appointment.Start.ToLocalTime();
endPickerDate.Value = appointment.End.ToLocalTime().Date;
endPickerTime.Value = appointment.End.ToLocalTime();
ShowDialog();
}
private bool ValidateForm()
{
if (string.IsNullOrWhiteSpace(titleTextBox.Text) ||
string.IsNullOrWhiteSpace(descriptionTextBox.Text) ||
string.IsNullOrWhiteSpace(locationTextBox.Text) ||
string.IsNullOrWhiteSpace(contactTextBox.Text) ||
string.IsNullOrWhiteSpace(textBoxUrl.Text) ||
typeComboBox.SelectedIndex == -1 ||
customerComboBox.SelectedIndex == -1)
{
MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
DateTime start = startPickerDate.Value.Date.Add(startPickerTime.Value.TimeOfDay).ToUniversalTime();
DateTime end = endPickerDate.Value.Date.Add(endPickerTime.Value.TimeOfDay).ToUniversalTime();
if (start >= end)
{
MessageBox.Show("Start time must be before end time.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime startEST = TimeZoneInfo.ConvertTimeFromUtc(start, estZone);
DateTime endEST = TimeZoneInfo.ConvertTimeFromUtc(end, estZone);
if (startEST.Hour < 9 || endEST.Hour > 17 || startEST.DayOfWeek == DayOfWeek.Saturday || startEST.DayOfWeek == DayOfWeek.Sunday)
{
MessageBox.Show("Appointments must be scheduled during business hours (9:00 AM - 5:00 PM EST, Monday-Friday).", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
var appointments = DatabaseHelper.RetrieveAppointments(AppState.CurrentUser.UserId);
foreach (var appt in appointments)
{
if (_currentAppointment == null || appt.AppointmentId != _currentAppointment.AppointmentId)
{
if (start < appt.End && end > appt.Start)
{
MessageBox.Show("Appointment times cannot overlap with existing appointments.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
return true;
}
private void SaveButton_Click(object? sender, EventArgs e)
{
if (!ValidateForm())
return;
try
{
var selectedCustomer = (Customer)customerComboBox.SelectedItem!;
var appointment = new Appointment
{
AppointmentId = _currentAppointment?.AppointmentId ?? 0,
CustomerId = selectedCustomer.CustomerId,
UserId = AppState.CurrentUser.UserId,
Title = titleTextBox.Text.Trim(),
Description = descriptionTextBox.Text.Trim(),
Location = locationTextBox.Text.Trim(),
Contact = contactTextBox.Text.Trim(),
AppointmentType = typeComboBox.SelectedItem!.ToString()!,
Url = textBoxUrl.Text.Trim(),
Start = startPickerDate.Value.Date.Add(startPickerTime.Value.TimeOfDay).ToUniversalTime(),
End = endPickerDate.Value.Date.Add(endPickerTime.Value.TimeOfDay).ToUniversalTime(),
CreateDate = DateTime.UtcNow,
CreatedBy = AppState.CurrentUser.Username,
LastUpdate = DateTime.UtcNow,
LastUpdateBy = AppState.CurrentUser.Username
};
if (_currentAppointment == null)
{
DatabaseHelper.AddAppointment(appointment);
MessageBox.Show("Appointment added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DatabaseHelper.UpdateAppointment(appointment);
MessageBox.Show("Appointment updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
DialogResult = DialogResult.OK;
UpdateAppointmentsList?.Invoke(this, EventArgs.Empty);
Close();
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while saving the appointment:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}