137 lines
4.8 KiB
C#
137 lines
4.8 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();
|
|
var orderList = _orderStatusLogic.ReadList(null);
|
|
List<string> list = new List<string>();
|
|
foreach (var val in orderList)
|
|
{
|
|
list.Add(val.Name);
|
|
}
|
|
userControlCheckedList1.SetCheckedListBoxValues(list);
|
|
userControlCheckedList1.ChangeTypeOfMaxChecked(Components.Types.Exception);
|
|
userControlCheckedList1.MaxCheckedItemsCount = 6;
|
|
userControlDatePicker1.MinDate = DateTime.Today.AddDays(1);
|
|
userControlDatePicker1.MaxDate = DateTime.Today.AddDays(4);
|
|
selectComponentOrderStatus.FillList(list);
|
|
textBoxFIO.TextChanged += valueChanged;
|
|
selectComponentOrderStatus.SelectComponentChanged += valueChanged;
|
|
userControlDatePicker1.DataContextChanged += valueChanged;
|
|
userControlCheckedList1.CheckedItemChanged += valueChanged;
|
|
|
|
}
|
|
|
|
private void FormEdit_Add_Load(object sender, EventArgs e)
|
|
{
|
|
LoadData();
|
|
|
|
|
|
}
|
|
|
|
private void LoadData()
|
|
{
|
|
try
|
|
{
|
|
if (_id != null)
|
|
{
|
|
var element = _orderLogic.ReadElement(new OrderSearchModel { Id = _id.Value });
|
|
if (element != null)
|
|
{
|
|
textBoxFIO.Text = element.FIO;
|
|
userControlCheckedList1.CheckedItemss = element.OrderPath.ToList();
|
|
selectComponentOrderStatus.SelectedValue = element.OrderDestination;
|
|
userControlDatePicker1.Date = element.OrderDeliveryTime.ToDateTime(TimeOnly.MinValue);
|
|
userControlCheckedList1.CheckedItemss = element.OrderPath.ToList();
|
|
}
|
|
}
|
|
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 = userControlCheckedList1.CheckedItemss.ToArray(),
|
|
OrderDestination = selectComponentOrderStatus.SelectedValue.ToString(),
|
|
OrderDeliveryTime = DateOnly.FromDateTime(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 + "Тут");
|
|
}
|
|
}
|
|
|
|
private void userControlCheckedList1_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|