2024-11-27 23:43:53 +04:00

125 lines
4.2 KiB
C#

using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
namespace COP3_
{
public partial class FormEdit : Form
{
private readonly IOrderLogic _orderLogic;
private readonly ICityStatusLogic _orderStatusLogic;
private int? _id;
public int Id { set { _id = value; } }
private bool isChanged = false;
public FormEdit(IOrderLogic orderLogic, ICityStatusLogic orderStatusLogic)
{
_orderLogic = orderLogic;
_orderStatusLogic = orderStatusLogic;
InitializeComponent();
selectComponentOrderStatus.FillList(_orderStatusLogic.ReadList(null), "Name");
textBoxFIO.TextChanged += valueChanged;
selectComponentOrderStatus.SelectComponentChanged += valueChanged;
userControlDatePicker1.MinDate = DateTime.Today.AddDays(1);
userControlDatePicker1.MaxDate = DateTime.Today.AddDays(3);
}
private void FormEdit_Add_Load(object sender, EventArgs e)
{
LoadData();
List<string> list = new List<string>();
foreach(var val in _orderStatusLogic.ReadList(null))
{
list.Add(val.Name);
}
}
private void LoadData()
{
try
{
if (_id != null)
{
var element = _orderLogic.ReadElement(new OrderSearchModel { Id = _id.Value });
if (element != null)
{
textBoxFIO.Text = element.FIO;
textBoxGoodDescription.Text = element.OrderPath.ToString();
selectComponentOrderStatus.SelectedValue = element.OrderDestination;
userControlDatePicker1.Date = element.OrderDeliveryTime;
}
}
isChanged = false;
}
catch (Exception)
{
throw;
}
}
private void valueChanged(object sender, EventArgs e)
{
isChanged = true;
}
private void buttonClose_Click(object sender, EventArgs e)
{
if (isChanged == true)
{
var result = MessageBox.Show(
"Есть несохраненные изменения. Вы уверены, что хотите закрыть?",
"Подтверждение",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (result == DialogResult.Yes)
{
Close();
}
else if (result == DialogResult.No)
{
MessageBox.Show("Окей, тогда делайте дальше)", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
Close();
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
var element = new OrderBindingModel
{
Id = _id ?? 0,
FIO = textBoxFIO.Text,
OrderPath = textBoxGoodDescription.Text.Split(", "),
OrderDestination = selectComponentOrderStatus.SelectedValue.ToString(),
OrderDeliveryTime = userControlDatePicker1.Date,
};
var operatingResult = _id.HasValue ? _orderLogic.Update(element) : _orderLogic.Create(element);
if (!operatingResult)
{
throw new Exception("Ошибка при создании сущности");
}
MessageBox.Show("Создание сущности прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}