PIbd-31_Malafeev.L.S._COP_25/Cop_25/Forms/DeliveryForm.cs

130 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Contracts.BindlingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using ControlsLibraryNet60.Input;
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 DeliveryForm : Form
{
private int? _id;
private readonly IDeliveryLogic _dLogic;
private readonly IDeliveryTypeLogic _dtLogic;
private bool isEdited;
public DeliveryForm(IDeliveryLogic dLogic, IDeliveryTypeLogic dtLogic, int? id = null)
{
InitializeComponent();
_id = id;
_dLogic = dLogic;
_dtLogic = dtLogic;
isEdited = false;
LoadData();
}
private void LoadData()
{
var list = _dtLogic.ReadList(null);
if (list != null)
{
foreach (var item in list)
{
comboBoxType.Items.Add(item.Name);
}
}
if (_id.HasValue)
{
var view = _dLogic.ReadElement(new DeliverySearchModel
{
Id = _id.Value,
});
textBoxWishes.Text = view.Wishes;
textBoxFCs.Text = view.FCs;
if (view.DeliveryDate != "Даты доставки нет")
{
inputComponentDate.Value = Convert.ToDateTime(view.DeliveryDate);
}
else
{
inputComponentDate.Value = null;
}
comboBoxType.SelectedItem = view.DeliveryType;
isEdited = false;
}
}
private void OnInputChange(object sender, EventArgs e)
{
isEdited = true;
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFCs.Text))
{
MessageBox.Show("Заполните поле ФИО курьера", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxWishes.Text))
{
MessageBox.Show("Заполните Пожелания ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if ((comboBoxType.SelectedItem == null))
{
MessageBox.Show("Выберите Тип доставки", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
isEdited = false;
try
{
var model = new DeliveryBindingModel
{
Id = _id ?? 0,
FCs = textBoxFCs.Text,
Wishes = textBoxWishes.Text,
DeliveryDate = inputComponentDate.Value.ToString().Length >= 10 ? inputComponentDate.Value.ToString().Substring(0, 10) : "Даты доставки нет",
DeliveryType = comboBoxType.SelectedItem.ToString(),
};
var OperationResult = _id.HasValue ? _dLogic.Update(model) : _dLogic.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();
}
}
}