COP3PLUSSagirov/DeliveryApp/FormDelivery.cs
2024-11-06 15:40:33 +04:00

144 lines
5.0 KiB
C#

using BuisnessLogic;
using Contracts.BindingModels;
using Contracts.BuisnessLogicsContracts;
using Contracts.SearchModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeliveryApp
{
public partial class FormDelivery : Form
{
private readonly ITypeLogic _typeLogic;
private readonly IDeliveryLogic _deliveryLogic;
private int? _id;
private string? currentImage;
public int Id { set { _id = value; } }
public FormDelivery(ITypeLogic typeLogic, IDeliveryLogic deliveryLogic)
{
_typeLogic = typeLogic;
_deliveryLogic = deliveryLogic;
InitializeComponent();
}
private void FormDelivery_Load(object sender, EventArgs e)
{
customTextBoxNumber2.TextBoxNumber = "(0000)00-00-00";
LoadData();
}
private void LoadData()
{
var list = _typeLogic.ReadList(null);
if (list != null)
{
foreach (var item in list)
{
customCheckedListBox1.DataList.Add(item.PostType);
}
}
if (_id.HasValue)
{
try
{
var element = _deliveryLogic.ReadElement(new DeliverySearchModel
{
Id = _id.Value,
});
if (element != null)
{
textBox1.Text = element.CourierFIO;
customTextBoxNumber2.TextBoxNumber = element.Phone;
customCheckedListBox1.SelectedValue = element.Type;
pictureBox1.Image = Image.FromFile(element.Image);
currentImage = element.Image;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Заполните фио", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (customCheckedListBox1.SelectedValue == null)
{
MessageBox.Show("Выберите тип", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var ss = customTextBoxNumber2.NumPattern;
if (customTextBoxNumber2.TextBoxNumber == null)
{
MessageBox.Show("Заполните номер", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (currentImage == null)
{
MessageBox.Show("Выберите фото", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new DeliveryBindingModel
{
Id = _id ?? 0,
CourierFIO = textBox1.Text,
Image = currentImage,
Phone = customTextBoxNumber2.TextBoxNumber,
Type = customCheckedListBox1.SelectedValue,
};
var operationResult = _id.HasValue ? _deliveryLogic.Update(model) :
_deliveryLogic.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 buttonImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Выберите фото";
dlg.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
currentImage = dlg.FileName;
}
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}