106 lines
3.0 KiB
C#

using LawFirm;
using LawFirmContracts.BusinessLogicContracts;
using LawFirmContracts.DI;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
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 LawFirmView
{
public partial class FormMails : Form
{
private readonly ILogger _logger;
private readonly IMessageInfoLogic _logic;
private int currentPage = 1;
public int pageSize = 2;
public FormMails(ILogger<FormMails> logger, IMessageInfoLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
buttonPrevPage.Enabled = false;
}
private void FormMails_Load(object sender, EventArgs e)
{
LoadMail();
}
private bool LoadMail()
{
try
{
dataGridView.FillAndConfigGrid(_logic.ReadList(new()
{
Page = currentPage,
PageSize = pageSize,
}));
_logger.LogInformation("Загрузка писем");
_logger.LogInformation("Загрузка писем");
labelPage.Text = $"Страница: {currentPage}";
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки писем");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
}
private void buttonPrevPage_Click(object sender, EventArgs e)
{
if (currentPage == 1)
{
return;
}
currentPage--;
if (LoadMail())
{
buttonNextPage.Enabled = true;
if (currentPage == 1)
{
buttonPrevPage.Enabled = false;
}
}
}
private void buttonNextPage_Click(object sender, EventArgs e)
{
currentPage++;
if (!LoadMail() || ((List<MessageInfoViewModel>)dataGridView.DataSource).Count == 0)
{
currentPage--;
LoadMail();
buttonNextPage.Enabled = false;
}
else
{
buttonPrevPage.Enabled = true;
}
}
private void buttonReply_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var form = DependencyManager.Instance.Resolve<FormMail>();
form.MessageId = (string)(dataGridView.SelectedRows[0].Cells["MessageId"].Value);
form.ShowDialog();
LoadMail();
}
}
}
}