COP-17/WinFormsProject/AppView/FormProvider.cs

187 lines
6.0 KiB
C#
Raw Normal View History

2023-11-30 00:36:01 +04:00
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using DocumentFormat.OpenXml.Vml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm;
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;
private byte[] _logo = new byte[0];
public FormProvider(IProviderStorage providerStorage, ITypeStorage typeStorage)
{
_providerStorage = providerStorage;
_typeStorage = typeStorage;
isEdited = false;
InitializeComponent();
}
private Image byteArrayToImage(byte[] byteArrayIn)
{
Image res = null;
try
{
MemoryStream ms = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length);
ms.Write(byteArrayIn, 0, byteArrayIn.Length);
res = Image.FromStream(ms, true);//Exception occurs here
}
catch { }
return res;
}
private void FormProvider_Load(object sender, EventArgs e)
{
dropDownList1.ExplicitEvent += OnInputChange;
LoadData();
if (_id.HasValue)
{
try
{
var element = _providerStorage.GetElement(new ProviderSearchModel { Id = _id!.Value });
if (element != null)
{
textBoxName.Text = element.Name;
_logo = element.Logo;
dropDownList1.Selected = element.Type;
textBoxNumber.Text = element.Number;
pictureBox1.Image = byteArrayToImage(_logo);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении записи", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
var list = _typeStorage.GetFullList().Select(item => item.Name).ToList();
dropDownList1.Clear();
foreach (var item in list)
{
dropDownList1.ComboBoxItems.Add(item);
}
if (_id != null)
{
var provider = _providerStorage.GetElement(new ProviderSearchModel() { Id = _id!.Value });
dropDownList1.Selected = provider!.Type;
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Название не указано.");
return;
}
if (_logo.Length == 0)
{
MessageBox.Show("Выберите фото", "Ошибка");
return;
}
if (string.IsNullOrEmpty(dropDownList1.Selected))
{
MessageBox.Show("Не выбран тип изделий.");
return;
}
if (!Regex.IsMatch(textBoxNumber.Text, @"^\d-\d{3}-\d{3}-\d{2}-\d{2}$"))
{
MessageBox.Show("Неправильный формат номера телефона.");
return;
}
isEdited = false;
try
{
var model = new ProviderBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Logo = _logo,
Type = dropDownList1.Selected,
Number = textBoxNumber.Text
};
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 buttonType_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;
}
private void buttonImage_Click(object sender, EventArgs e)
{
if (ImgFile.ShowDialog() != DialogResult.OK) return;
Image img = Image.FromFile(ImgFile.FileName);
using (MemoryStream mStream = new MemoryStream())
{
img.Save(mStream, img.RawFormat);
_logo = mStream.ToArray();
}
pictureBox1.Image = img;
}
private void OnInputChange(object sender, EventArgs e)
{
isEdited = true;
}
}
}