131 lines
4.8 KiB
C#
Raw Normal View History

2024-10-22 19:58:21 +04:00
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;
2024-10-23 15:53:54 +04:00
private bool isEdited;
2024-10-22 19:58:21 +04:00
2024-10-23 15:53:54 +04:00
public ProviderForm(IProviderLogic plogic, IOrganisationTypeLogic ologic, int? id = null)
2024-10-22 19:58:21 +04:00
{
InitializeComponent();
_pLogic = plogic;
_oLogic = ologic;
2024-10-23 15:53:54 +04:00
_id = id;
isEdited = false;
2024-10-22 19:58:21 +04:00
LoadData();
}
private void LoadData()
{
var list = _oLogic.ReadList(null);
if (list != null)
{
List<string> strList = new List<string>();
foreach (var item in list)
{
strList.Add(item.Name);
}
controlSelectedComboBoxList.AddList(strList);
}
if (_id.HasValue)
{
var view = _pLogic.ReadElement(new ProviderSearchModel
{
Id = _id.Value,
});
2024-10-23 15:53:54 +04:00
textBoxFurnitureType.Text = view.FurnitureType;
textBoxName.Text = view.Name;
2024-10-23 16:25:39 +04:00
if (view.DateLastDelivery != "Поставок не было") {
controlInputNullableDate.Value = Convert.ToDateTime(view.DateLastDelivery);
}
else
{
controlInputNullableDate.Value = null;
}
2024-10-22 19:58:21 +04:00
controlSelectedComboBoxList.SelectedElement = view!.OrganisationType;
2024-10-23 15:53:54 +04:00
isEdited = false;
2024-10-22 19:58:21 +04:00
}
}
2024-10-23 15:53:54 +04:00
private void OnInputChange(object sender, EventArgs e)
{
isEdited = true;
}
2024-10-22 19:58:21 +04:00
private void buttonAdd_Click(object sender, EventArgs e)
{
2024-10-23 15:53:54 +04:00
if (string.IsNullOrEmpty(textBoxName.Text))
2024-10-22 19:58:21 +04:00
{
MessageBox.Show("Заполните поле Название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxFurnitureType.Text))
{
MessageBox.Show("Заполните Перечень мебели ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-10-23 15:53:54 +04:00
if (string.IsNullOrEmpty(controlSelectedComboBoxList.SelectedElement))
2024-10-22 19:58:21 +04:00
{
MessageBox.Show("Выберите Тип организации", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-10-23 15:53:54 +04:00
isEdited = false;
2024-10-22 19:58:21 +04:00
try
{
2024-10-23 15:53:54 +04:00
var model = new ProviderBindingModel
2024-10-22 19:58:21 +04:00
{
2024-10-23 15:53:54 +04:00
Id = _id ?? 0,
2024-10-22 19:58:21 +04:00
Name = textBoxName.Text,
FurnitureType = textBoxFurnitureType.Text,
2024-10-23 15:53:54 +04:00
DateLastDelivery = controlInputNullableDate.Value.ToString() == "" ? "Поставок не было" : controlInputNullableDate.Value.ToString(),
2024-10-22 19:58:21 +04:00
OrganisationType = controlSelectedComboBoxList.SelectedElement
2024-10-23 15:53:54 +04:00
};
var OperationResult = _id.HasValue ? _pLogic.Update(model) : _pLogic.Create(model);
2024-10-22 19:58:21 +04:00
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);
}
}
2024-10-23 15:53:54 +04:00
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();
}
2024-10-22 19:58:21 +04:00
}
}