2024-11-07 14:19:38 +04:00

182 lines
5.1 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 Microsoft.Extensions.Logging;
using System.ComponentModel;
using ShopContracts.BindingModels;
using ShopContracts.BusinessLogicsContracts;
using Lab1;
using System;
using Lab1.VisualComponents.Exceptions;
namespace ShopView
{
public partial class FormCustomer : Form
{
private readonly ILogger _logger;
private readonly ICustomerLogic _customerLogic;
private readonly IProductCategoryLogic _productCategoryLogic;
private string imagePath;
BindingList<ProductCategoryBindingModel> _list;
private int? _id;
public int Id { set { _id = value; } }
private string? _item;
List<string> productCategory = new();
public FormCustomer(ILogger<FormCustomer> logger, ICustomerLogic customerLogic, IProductCategoryLogic productCategoryLogic)
{
InitializeComponent();
_logger = logger;
_customerLogic = customerLogic;
_productCategoryLogic = productCategoryLogic;
imagePath = "";
_list = new BindingList<ProductCategoryBindingModel>();
emailField1.EmailRegex = "^\\S+@\\S+\\.\\S+$";
emailField1.EmailExample = "example@mail.ru";
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFIO.Text))
{
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
if (string.IsNullOrEmpty(emailField1.Value))
{
MessageBox.Show("Введите почту!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch (IsNotMatchRegexException)
{
MessageBox.Show("Не соответствует шаблону", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (pictureBox.Image == null)
{
MessageBox.Show("Загрузите фото", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение студента");
try
{
_customerLogic.CreateOrUpdate(new CustomerBindingModel
{
Id = _id,
FIO = textBoxFIO.Text,
PhotoFilePath = imagePath,
Email = emailField1.Value,
ProductCategoryName = myListBox1.SelectedItem
});
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения студента");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void FormCustomer_Load(object sender, EventArgs e)
{
LoadData();
if (_id.HasValue)
{
try
{
var view = _customerLogic.Read(new CustomerBindingModel { Id = _id.Value })?.FirstOrDefault();
if (view != null)
{
textBoxFIO.Text = view.FIO;
imagePath = view.PhotoFilePath ?? string.Empty;
// Используем рефлексию для доступа к textBox1 в emailField1
var textBoxField = typeof(EmailField).GetField("textBox1",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
if (textBoxField?.GetValue(emailField1) is TextBox innerTextBox)
{
innerTextBox.Text = view.Email ?? string.Empty; // Устанавливаем значение напрямую
}
// Устанавливаем другие значения, как и раньше
string[] dirs = view.ProductCategoryName.Split(";");
foreach (var dir in dirs)
{
myListBox1.SelectedItem = dir;
}
if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
{
pictureBox.Image = Image.FromFile(imagePath);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
_logger.LogInformation("Загрузка категории товаров");
try
{
var list = _productCategoryLogic.Read(null);
myListBox1.Clear();
productCategory.Clear();
_list.Clear();
if (list != null)
{
foreach (var item in list)
{
productCategory.Add(item.Name);
_list.Add(new ProductCategoryBindingModel
{
Id = item.Id,
Name = item.Name,
});
}
}
myListBox1.AddItems(productCategory);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки категории товаров");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonAddPhoto_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new()
{
Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
imagePath = openFileDialog.FileName;
pictureBox.Image = Image.FromFile(imagePath);
}
}
}
}