using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using DocumentFormat.OpenXml.Office2010.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForms
{
///
/// Форма для создания или редактирования счета
///
public partial class FormOrder : Form
{
///
/// Бизнес-логика для сущности "Счет"
///
private readonly IOrderLogic _orderLogic;
///
/// Бизнес-логика для сущности "Тип заказа"
///
private readonly IOrderTypeLogic _orderTypeLogic;
///
/// Идентификатор счета
///
private int? _id;
///
/// Идентификатор счета
///
public int Id { set { _id = value; } }
///
/// Конструктор
///
///
///
public FormOrder(IOrderLogic orderLogic, IOrderTypeLogic orderTypeLogic)
{
InitializeComponent();
_orderLogic = orderLogic;
_orderTypeLogic = orderTypeLogic;
}
///
/// Загрузка формы
///
///
///
private void FormOrder_Load(object sender, EventArgs e)
{
var listOrderTypes = _orderTypeLogic.ReadList(null);
if (listOrderTypes != null)
{
foreach (var type in listOrderTypes)
{
customComboBox.AddItem(type.Name);
}
}
if (!_id.HasValue)
{
return;
}
try
{
var order = _orderLogic.ReadElement(new OrderSearchModel { Id = _id.Value });
if (order == null)
{
return;
}
textBoxWaiterFullName.Text = order.WaiterFullName;
textBoxInfo.Text = order.Info;
customComboBox.SelectedItem = order.Type;
if (string.IsNullOrEmpty(order.Sum))
{
controlInputNullableDouble.Value = double.Parse(order.Sum!);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Кнопка "Сохранить"
///
///
///
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxWaiterFullName.Text))
{
MessageBox.Show("Заполните ФИО официанта!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxInfo.Text))
{
MessageBox.Show("Заполните информацию по счету!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(customComboBox.SelectedItem))
{
MessageBox.Show("Выберите тип заказа!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new OrderBindingModel
{
Id = _id ?? 0,
WaiterFullName = textBoxWaiterFullName.Text,
Info = textBoxInfo.Text,
Type = customComboBox.SelectedItem,
Sum = controlInputNullableDouble.Value,
};
var operatingResult = _id.HasValue ? _orderLogic.Update(model) : _orderLogic.Create(model);
if (!operatingResult)
{
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 buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}