174 lines
6.0 KiB
C#
174 lines
6.0 KiB
C#
|
using BusinessLogic;
|
|||
|
using DataContracts.bindingModels;
|
|||
|
using DataContracts.searchModels;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Linq.Expressions;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace ClientForms
|
|||
|
{
|
|||
|
public partial class Client : Form
|
|||
|
{
|
|||
|
private readonly ClientBL clientBL = new ClientBL();
|
|||
|
private readonly ProductBL productBL = new ProductBL();
|
|||
|
private int? _id;
|
|||
|
private bool fioFlag = false;
|
|||
|
private bool emailFlag = false;
|
|||
|
private bool photoFlag = false;
|
|||
|
private bool EditfioFlag = false;
|
|||
|
private bool EditemailFlag = false;
|
|||
|
private bool EditphotoFlag = false;
|
|||
|
private bool close = false;
|
|||
|
private byte[] image;
|
|||
|
private List<string> oldProducts = new List<string>();
|
|||
|
public int? Id { get { return _id; } set { _id = value; } }
|
|||
|
public Client()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
myEmailTextBox.ValueChanged += EmailTextBox_TextChanged;
|
|||
|
myEmailTextBox.Pattern = "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$";
|
|||
|
}
|
|||
|
|
|||
|
private void Client_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
LoadData();
|
|||
|
if (Id.HasValue)
|
|||
|
{
|
|||
|
var current_client = clientBL.ReadElement(new ClientSearchModel { Id = Id });
|
|||
|
if(current_client != null)
|
|||
|
{
|
|||
|
textBoxFIO.Text = current_client.FIO;
|
|||
|
myEmailTextBox.TextBoxValue = current_client.email;
|
|||
|
image = current_client.photo;
|
|||
|
oldProducts = current_client.products.Split(',').ToList();
|
|||
|
buttonPhoto.BackColor = Color.DarkOliveGreen;
|
|||
|
myCheckListProducts.setValues(current_client.products.Split(',').ToList());
|
|||
|
EditfioFlag = false;
|
|||
|
EditemailFlag = false;
|
|||
|
EditphotoFlag = false;
|
|||
|
fioFlag = true;
|
|||
|
emailFlag = true;
|
|||
|
photoFlag = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private bool checkProducts()
|
|||
|
{
|
|||
|
if(oldProducts.Count != myCheckListProducts.getSelectedItems().Count)
|
|||
|
return true;
|
|||
|
foreach(var oldProd in oldProducts)
|
|||
|
{
|
|||
|
if(!myCheckListProducts.getSelectedItems().Contains(oldProd))
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
myCheckListProducts.LoadValues(productBL.ReadList().Select(x => x.title).ToList());
|
|||
|
}
|
|||
|
private void textBoxFIO_TextChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
EditfioFlag = true;
|
|||
|
if (textBoxFIO.Text.Length != 0)
|
|||
|
{
|
|||
|
fioFlag = true;
|
|||
|
textBoxFIO.BackColor = Color.DarkOliveGreen;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
fioFlag = false;
|
|||
|
textBoxFIO.BackColor = Color.IndianRed;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
private void EmailTextBox_TextChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
EditemailFlag = true;
|
|||
|
if (myEmailTextBox.TextBoxValue != null)
|
|||
|
{
|
|||
|
emailFlag = true;
|
|||
|
myEmailTextBox.BackColor = Color.DarkOliveGreen;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
emailFlag = false;
|
|||
|
myEmailTextBox.BackColor = Color.IndianRed;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonPhoto_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
EditphotoFlag = true;
|
|||
|
if (openFileDialogPath.ShowDialog() == DialogResult.Cancel)
|
|||
|
return;
|
|||
|
string filename = openFileDialogPath.FileName;
|
|||
|
Image img = Image.FromFile(filename);
|
|||
|
byte[] bA;
|
|||
|
using (MemoryStream mStream = new MemoryStream())
|
|||
|
{
|
|||
|
img.Save(mStream, img.RawFormat);
|
|||
|
bA = mStream.ToArray();
|
|||
|
}
|
|||
|
image = bA;
|
|||
|
photoFlag = true;
|
|||
|
buttonPhoto.BackColor = Color.DarkOliveGreen;
|
|||
|
}
|
|||
|
|
|||
|
private void buttonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if(fioFlag & emailFlag & photoFlag & myCheckListProducts.selectedValuesCount != 0)
|
|||
|
{
|
|||
|
if(!Id.HasValue)
|
|||
|
clientBL.Create(new ClientBindingModel {
|
|||
|
FIO = textBoxFIO.Text,
|
|||
|
email = myEmailTextBox.TextBoxValue,
|
|||
|
photo = image,
|
|||
|
products = string.Join(",", myCheckListProducts.getSelectedItems().ToArray())
|
|||
|
});
|
|||
|
else
|
|||
|
clientBL.Update(new ClientBindingModel
|
|||
|
{
|
|||
|
Id = (int)Id,
|
|||
|
FIO = textBoxFIO.Text,
|
|||
|
email = myEmailTextBox.TextBoxValue,
|
|||
|
photo = image,
|
|||
|
products = string.Join(",", myCheckListProducts.getSelectedItems())
|
|||
|
});
|
|||
|
close = true;
|
|||
|
this.Close();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
MessageBox.Show("Не заполнены поля");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void Client_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
{
|
|||
|
if (close)
|
|||
|
{
|
|||
|
e.Cancel = false;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if ((EditfioFlag || EditemailFlag || EditphotoFlag || checkProducts()))
|
|||
|
{
|
|||
|
DialogResult dialogResult = MessageBox.Show("Вы забыли сохранить изменения! Желаете выйти?", "Важно", MessageBoxButtons.YesNo);
|
|||
|
if (dialogResult == DialogResult.Yes)
|
|||
|
e.Cancel = false;
|
|||
|
else
|
|||
|
e.Cancel = true;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|