PIbd-32_Bondarenko_M.S._COP_16/WinForm/AppView/FormProvider.cs

159 lines
5.2 KiB
C#
Raw Normal View History

using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
namespace AppView
{
public partial class FormProvider : Form
{
private int? _id;
public int Id { set { _id = value; } }
public readonly IProviderStorage _providerStorage;
public readonly ITypeStorage _typeStorage;
private bool isEdited;
public FormProvider(IProviderStorage providerStorage, ITypeStorage typeStorage)
{
_providerStorage = providerStorage;
_typeStorage = typeStorage;
isEdited = false;
InitializeComponent();
}
private void FormProvider_Load(object sender, EventArgs e)
{
2023-11-02 11:34:21 +04:00
customTextBox1.ValueChanged += OnInputChange;
dropDownList.ExplicitEvent += OnInputChange;
LoadData();
if (_id.HasValue)
{
try
{
var element = _providerStorage.GetElement(new ProviderSearchModel
{
Id = _id.Value,
});
if (element != null)
{
textBoxName.Text = element.Name;
textBoxFurniture.Text = element.Furniture;
dropDownList.Selected = element.Type;
2023-11-02 11:34:21 +04:00
customTextBox1.DateValue = element.SupplyDate;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении записи", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
try
{
var strings = _typeStorage.GetFullList().Select(item => item.Name).ToList();
dropDownList.Clear();
foreach (var item in strings)
{
dropDownList.ComboBoxItems.Add(item);
}
if (_id.HasValue)
{
var view = _providerStorage.GetElement(new ProviderSearchModel
{
Id = _id!.Value
});
dropDownList.Selected = view!.Type;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка загрузки формы", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnInputChange(object sender, EventArgs e)
{
isEdited = true;
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Название не указано");
return;
}
if (string.IsNullOrEmpty(textBoxFurniture.Text))
{
MessageBox.Show("Перечень мебели не указан");
return;
}
if (string.IsNullOrEmpty(dropDownList.Selected))
{
MessageBox.Show("Тип предприятия не выбран");
return;
}
isEdited = false;
try
{
var model = new ProviderBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Furniture = textBoxFurniture.Text,
Type = dropDownList.Selected,
2023-11-02 11:34:21 +04:00
SupplyDate = (customTextBox1.DateValue != null) ?
DateTime.SpecifyKind((DateTime)customTextBox1.DateValue, DateTimeKind.Utc)
: customTextBox1.DateValue,
};
var action = _id.HasValue ? _providerStorage.Update(model) : _providerStorage.Insert(model);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void ButtonTypeForm_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormType));
if (service is FormType form)
{
form.ShowDialog();
LoadData();
}
}
private void FormProvider_FormClosing(object sender, FormClosingEventArgs e)
{
if (!isEdited) return;
var confirmResult = MessageBox.Show(
"Имеются незафиксированные изменения." +
"\n" +
"Вы действительно хотите закрыть форму?",
"Подтвердите действие",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (confirmResult == DialogResult.No) e.Cancel = true;
}
}
}