PIbd-32_Turner_I.A._COP_10/COP/WinForms/FormClient.cs
2023-10-25 23:06:54 +04:00

109 lines
2.7 KiB
C#

using ClientsContracts.BindingModels;
using ClientsContracts.BusinessLogicContracts;
using ClientsContracts.ViewModels;
using ControlsLibraryNet60.Input;
using System.Windows.Forms;
namespace WinForms
{
public partial class FormClient : Form
{
public int Id { set { id = value; } }
private readonly IClientLogic _logic;
private readonly IStatusLogic _logicS;
private int? id;
public FormClient(IClientLogic logic, IStatusLogic logicS)
{
InitializeComponent();
_logic = logic;
_logicS = logicS;
}
private void FormClient_Load(object sender, EventArgs e)
{
List<StatusViewModel> viewS = _logicS.Read(null);
if (viewS != null)
{
List<string> list = new List<string>();
foreach (StatusViewModel s in viewS)
{
list.Add(s.Name);
}
myDropDownList1.LoadValues(list);
}
if (id.HasValue)
{
try
{
ClientViewModel view = _logic.Read(new ClientBindingModel { Id = id.Value })?[0];
if (view != null)
{
textBoxReviews.Text = view.Reviews;
textBoxName.Text = view.Name;
myDropDownList1.SelectedValue = view.Status;
if (view.Amount != null)
{
controlInputNullableInt1.Value = view.Amount;
}
else
{
controlInputNullableInt1.Value = 0;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(myDropDownList1.SelectedValue))
{
MessageBox.Show("Выберите статус", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (textBoxReviews.Text == null)
{
MessageBox.Show("Заполните отзывы", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int? amount = controlInputNullableInt1.Value;
try
{
_logic.CreateOrUpdate(new ClientBindingModel
{
Id = id,
Reviews = textBoxReviews.Text,
Name = textBoxName.Text,
Status = myDropDownList1.SelectedValue.ToString(),
Amount = amount
});
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}