You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.4 KiB

2 years ago
using AppointmentsLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppointmentsUi.Views
{
public partial class ContactView : Form
{
Contact? contact;
IEnumerable<Salutation> salutations;
IEnumerable<Postal> postals;
IEnumerable<PhoneType> phoneTypes;
IEnumerable<Company> companies;
public ContactView(Contact? contact)
{
InitializeComponent();
this.contact = contact;
salutations = Salutation.GetSalutations();
postals = Postal.GetPostals();
phoneTypes = PhoneType.GetPhoneTypes();
companies = Company.GetCompanies();
comboBoxSalutation.Items.AddRange(salutations.Select(sal => $"{sal.SalutationId}: {sal.Label}").ToArray());
comboBoxPostal.Items.AddRange(postals.Select(sal => $"{sal.PostalId}: {sal.Label}").ToArray());
comboBoxPhoneType.Items.AddRange(phoneTypes.Select(sal => $"{sal.PhoneTypeId}: {sal.Label}").ToArray());
comboBoxCompany.Items.AddRange(companies.Select(sal => $"{sal.CompanyId}: {sal.Label}").ToArray());
if (contact == null)
{
// create new contact
buttonApplyCreate.Text = "Create";
buttonApplyCreate.Click += new EventHandler(delegate (object sender, EventArgs e)
{
var salutationId = Int32.Parse(comboBoxSalutation.SelectedItem.ToString().Split(":").First());
var postalId = Int32.Parse(comboBoxPostal.SelectedItem.ToString().Split(":").First());
var phoneTypeId = Int32.Parse(comboBoxPhoneType.SelectedItem.ToString().Split(":").First());
var companyId = Int32.Parse(comboBoxCompany.SelectedItem.ToString().Split(":").First());
Contact.Create(salutationId, textBoxFirstName.Text, textBoxLastName.Text, textBoxStreet.Text, postalId, textBoxPhone.Text, phoneTypeId, textBoxMobil.Text, companyId, textBoxDepartment.Text);
this.Close();
});
buttonDelete.Enabled = false;
} else
{
textBoxFirstName.Text = contact.FirstName;
textBoxLastName.Text = contact.LastName;
textBoxStreet.Text = contact.Street;
textBoxDepartment.Text = contact.Department;
textBoxPhone.Text = contact.Phone;
textBoxMobil.Text = contact.Mobil;
buttonApplyCreate.Click += new EventHandler(delegate (object sender, EventArgs e)
{
var salutationId = Int32.Parse(comboBoxSalutation.SelectedItem.ToString().Split(":").First());
var postalId = Int32.Parse(comboBoxPostal.SelectedItem.ToString().Split(":").First());
var phoneTypeId = Int32.Parse(comboBoxPhoneType.SelectedItem.ToString().Split(":").First());
var companyId = Int32.Parse(comboBoxCompany.SelectedItem.ToString().Split(":").First());
contact.FirstName = textBoxFirstName.Text;
contact.LastName = textBoxLastName.Text;
contact.Street = textBoxStreet.Text;
contact.Phone = textBoxPhone.Text;
contact.Mobil = textBoxMobil.Text;
contact.Department = textBoxDepartment.Text;
contact.Salutation = Salutation.GetSalutationById(salutationId);
contact.Postal = Postal.GetPostalById(postalId);
contact.PhoneType = PhoneType.GetPhoneTypeById(phoneTypeId);
contact.Company = Company.GetCompanyById(companyId);
contact.Save();
this.Close();
});
comboBoxSalutation.SelectedItem = $"{contact.Salutation.SalutationId}: {contact.Salutation.Label}";
comboBoxPostal.SelectedItem = $"{contact.Postal.PostalId}: {contact.Postal.Label}";
comboBoxPhoneType.SelectedItem = $"{contact.PhoneType.PhoneTypeId}: {contact.PhoneType.Label}";
comboBoxCompany.SelectedItem = $"{contact.Company.CompanyId}: {contact.Company.Label}";
}
}
}
}