using System; using System.Windows.Forms; using C969Project.Data; using C969Project.Data.Models; namespace C969Project { public partial class AddUpdateCustomerForm : Form { private Customer? _currentCustomer; public AddUpdateCustomerForm() { InitializeComponent(); saveButton.Click += SaveButton_Click; cancelButton.Click += CancelButton_Click; } public void InitAdd() { Text = "Add Customer"; label1.Text = "Add Customer"; nameTextBox.Text = string.Empty; addressTextBox.Text = string.Empty; cityTextBox.Text = string.Empty; zipTextBox.Text = string.Empty; phoneTextBox.Text = string.Empty; _currentCustomer = null; ShowDialog(); } public void InitUpdate(Customer customer) { Text = "Update Customer"; label1.Text = "Update Customer"; _currentCustomer = customer; nameTextBox.Text = customer.CustomerName; var addr = DatabaseHelper.RetrieveAddress(customer.AddressId); if (addr != null) { addressTextBox.Text = addr.Address1; var city = DatabaseHelper.RetrieveCity(addr.CityId); if (city != null) { cityTextBox.Text = city.CityName; } zipTextBox.Text = addr.PostalCode; phoneTextBox.Text = addr.Phone; } ShowDialog(); } private void SaveButton_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(nameTextBox.Text) || string.IsNullOrWhiteSpace(addressTextBox.Text) || string.IsNullOrWhiteSpace(cityTextBox.Text) || string.IsNullOrWhiteSpace(zipTextBox.Text) || string.IsNullOrWhiteSpace(phoneTextBox.Text)) { MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!System.Text.RegularExpressions.Regex.IsMatch(phoneTextBox.Text.Trim(), @"^[\d-]+$")) { MessageBox.Show("Phone number can only contain digits and dashes.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string currentUser = AppState.CurrentUser.Username; try { string countryName = "US"; int currentCountryId = DatabaseHelper.GetCountryId(countryName); if (currentCountryId == 0) { Country newCountry = new Country { CountryName = countryName, CreateDate = DateTime.UtcNow, CreatedBy = currentUser, LastUpdate = DateTime.UtcNow, LastUpdateBy = currentUser }; currentCountryId = DatabaseHelper.AddCountry(newCountry); } int currentCityId = DatabaseHelper.GetCityId(cityTextBox.Text.Trim(), currentCountryId); if (currentCityId == 0) { City newCity = new City { CityName = cityTextBox.Text.Trim(), CountryID = currentCountryId, CreateDate = DateTime.UtcNow, CreatedBy = currentUser, LastUpdate = DateTime.UtcNow, LastUpdateBy = currentUser }; currentCityId = DatabaseHelper.AddCity(newCity); } Address addressToSave; int finalAddressId; if (Text == "Add Customer") { addressToSave = new Address { Address1 = addressTextBox.Text.Trim(), Address2 = "", CityId = currentCityId, PostalCode = zipTextBox.Text.Trim(), Phone = phoneTextBox.Text.Trim(), CreateDate = DateTime.UtcNow, CreatedBy = currentUser, LastUpdate = DateTime.UtcNow, LastUpdateBy = currentUser }; finalAddressId = DatabaseHelper.AddAddress(addressToSave); } else { Address? existingAddress = DatabaseHelper.RetrieveAddress(_currentCustomer.AddressId); if (existingAddress == null) { MessageBox.Show("Could not find existing address for update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } addressToSave = existingAddress; addressToSave.Address1 = addressTextBox.Text.Trim(); addressToSave.Address2 = ""; addressToSave.CityId = currentCityId; addressToSave.PostalCode = zipTextBox.Text.Trim(); addressToSave.Phone = phoneTextBox.Text.Trim(); addressToSave.LastUpdateBy = currentUser; DatabaseHelper.UpdateAddress(addressToSave); finalAddressId = addressToSave.Id; } Customer customerToSave; if (Text == "Add Customer") { customerToSave = new Customer { CustomerName = nameTextBox.Text.Trim(), AddressId = finalAddressId, Active = 1, CreateDate = DateTime.UtcNow, CreatedBy = currentUser, LastUpdate = DateTime.UtcNow, LastUpdateBy = currentUser }; DatabaseHelper.AddCustomer(customerToSave); } else { customerToSave = _currentCustomer; customerToSave.CustomerName = nameTextBox.Text.Trim(); customerToSave.AddressId = finalAddressId; customerToSave.LastUpdateBy = currentUser; DatabaseHelper.UpdateCustomer(customerToSave); } MessageBox.Show("Customer saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { // TODO: Implement robust exception handling, logging, and user notification as per assignment A2b MessageBox.Show($"An error occurred while saving the customer: {ex.Message}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void CancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }