using Contracts.BindingModels; using Contracts.BusinessLogicsContracts; using Contracts.SearchModels; 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 Forms { public partial class ProviderForm : Form { private int? _id; private readonly IProviderLogic _pLogic; private readonly IOrganisationTypeLogic _oLogic; private bool isEdited; public ProviderForm(IProviderLogic plogic, IOrganisationTypeLogic ologic, int? id = null) { InitializeComponent(); _pLogic = plogic; _oLogic = ologic; _id = id; isEdited = false; LoadData(); } private void LoadData() { var list = _oLogic.ReadList(null); if (list != null) { List strList = new List(); foreach (var item in list) { strList.Add(item.Name); } controlSelectedComboBoxList.AddList(strList); } if (_id.HasValue) { var view = _pLogic.ReadElement(new ProviderSearchModel { Id = _id.Value, }); textBoxFurnitureType.Text = view.FurnitureType; textBoxName.Text = view.Name; if (view.DateLastDelivery != "Поставок не было") controlInputNullableDate.Value = Convert.ToDateTime(view.DateLastDelivery); controlSelectedComboBoxList.SelectedElement = view!.OrganisationType; isEdited = false; } } private void OnInputChange(object sender, EventArgs e) { isEdited = true; } private void buttonAdd_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните поле Название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxFurnitureType.Text)) { MessageBox.Show("Заполните Перечень мебели ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(controlSelectedComboBoxList.SelectedElement)) { MessageBox.Show("Выберите Тип организации", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } isEdited = false; try { var model = new ProviderBindingModel { Id = _id ?? 0, Name = textBoxName.Text, FurnitureType = textBoxFurnitureType.Text, DateLastDelivery = controlInputNullableDate.Value.ToString() == "" ? "Поставок не было" : controlInputNullableDate.Value.ToString(), OrganisationType = controlSelectedComboBoxList.SelectedElement }; var OperationResult = _id.HasValue ? _pLogic.Update(model) : _pLogic.Create(model); if (!OperationResult) { throw new Exception("Ошибка при создании поставщика."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ProviderForm_FormClosing(object sender, FormClosingEventArgs e) { if (!isEdited) return; var confirmResult = MessageBox.Show("Вы не сохранили изменения.\nВы действительно хотите выйти?", "Подтвердите действие", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (confirmResult == DialogResult.No) e.Cancel = true; } private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }