Files
demo-24.11/Программа/Login form/Forms/MainForm.cs
T
2026-11-24 13:33:22 +03:00

172 lines
6.5 KiB
C#

using RentalApp.Data;
using RentalApp.Helpers;
using RentalApp.Models;
using System.IO;
namespace RentalApp.Forms;
public class MainForm : Form
{
private readonly Employee _user;
private readonly System.Windows.Forms.Timer _sessionTimer = new();
private DateTime _sessionEnd;
private bool _warned = false;
// --- контролы ---
private readonly PictureBox picEmployee = new();
private readonly Label lblFullName = new();
private readonly Label lblRole = new();
private readonly Label lblSessionLabel = new();
private readonly Label lblSessionTime = new();
private readonly Button btnCreateOrder = new();
private readonly Button btnAcceptReturn = new();
private readonly Button btnReports = new();
private readonly Button btnLoginHistory = new();
private readonly Button btnConsumables = new();
private readonly Button btnLogout = new();
public MainForm(Employee user)
{
_user = user;
BuildUi();
lblFullName.Text = user.FullName;
lblRole.Text = user.Role;
if (!string.IsNullOrEmpty(user.PhotoPath) && File.Exists(user.PhotoPath))
picEmployee.Image = Image.FromFile(user.PhotoPath);
else
picEmployee.Image = SystemIcons.Information.ToBitmap();
btnCreateOrder.Visible = true;
btnAcceptReturn.Visible = user.Role == "Старший смены";
btnReports.Visible = user.Role == "Администратор";
btnLoginHistory.Visible = user.Role == "Администратор";
btnConsumables.Visible = user.Role == "Администратор";
_sessionTimer.Interval = 1000;
_sessionTimer.Tick += SessionTimer_Tick;
_sessionEnd = DateTime.Now.AddMinutes(10);
_sessionTimer.Start();
UpdateTimerLabel();
btnLogout.Click += (s, e) => Logout();
// заглушки на кнопки — чтобы не падало, если нажать
btnCreateOrder.Click += (s, e) => MessageBox.Show("Окно оформления заказа (в разработке)");
btnAcceptReturn.Click += (s, e) => MessageBox.Show("Окно приёма оборудования (в разработке)");
btnReports.Click += (s, e) => MessageBox.Show("Окно отчётов (в разработке)");
btnLoginHistory.Click += (s, e) => ShowLoginHistory();
btnConsumables.Click += (s, e) => MessageBox.Show("Окно расходных материалов (в разработке)");
}
private void BuildUi()
{
picEmployee.BorderStyle = BorderStyle.FixedSingle;
picEmployee.Location = new Point(20, 20);
picEmployee.Size = new Size(120, 140);
picEmployee.SizeMode = PictureBoxSizeMode.Zoom;
lblFullName.AutoSize = true;
lblFullName.Font = new Font("Segoe UI", 12F, FontStyle.Bold);
lblFullName.Location = new Point(160, 25);
lblRole.AutoSize = true;
lblRole.Font = new Font("Segoe UI", 10F);
lblRole.ForeColor = Color.DarkSlateGray;
lblRole.Location = new Point(160, 60);
lblSessionLabel.AutoSize = true;
lblSessionLabel.Location = new Point(160, 100);
lblSessionLabel.Text = "Сеанс:";
lblSessionTime.AutoSize = true;
lblSessionTime.Font = new Font("Consolas", 14F, FontStyle.Bold);
lblSessionTime.ForeColor = Color.DarkGreen;
lblSessionTime.Location = new Point(215, 97);
btnCreateOrder.Location = new Point(20, 190);
btnCreateOrder.Size = new Size(260, 35);
btnCreateOrder.Text = "Оформить заказ";
btnAcceptReturn.Location = new Point(20, 235);
btnAcceptReturn.Size = new Size(260, 35);
btnAcceptReturn.Text = "Принять оборудование";
btnReports.Location = new Point(20, 280);
btnReports.Size = new Size(260, 35);
btnReports.Text = "Отчёты";
btnLoginHistory.Location = new Point(20, 325);
btnLoginHistory.Size = new Size(260, 35);
btnLoginHistory.Text = "История входа";
btnConsumables.Location = new Point(20, 370);
btnConsumables.Size = new Size(260, 35);
btnConsumables.Text = "Расходные материалы";
btnLogout.Location = new Point(20, 420);
btnLogout.Size = new Size(260, 35);
btnLogout.Text = "Выйти";
ClientSize = new Size(480, 480);
Controls.AddRange(new Control[] {
picEmployee, lblFullName, lblRole, lblSessionLabel, lblSessionTime,
btnCreateOrder, btnAcceptReturn, btnReports,
btnLoginHistory, btnConsumables, btnLogout
});
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Главное окно";
}
private void SessionTimer_Tick(object? sender, EventArgs e)
{
var left = _sessionEnd - DateTime.Now;
if (left.TotalSeconds <= 0)
{
_sessionTimer.Stop();
MessageBox.Show("Сеанс завершён. Вход заблокирован на 3 минуты.",
"Сеанс", MessageBoxButtons.OK, MessageBoxIcon.Information);
LockHelper.SetLock(DateTime.Now.AddMinutes(3));
Logout();
return;
}
UpdateTimerLabel();
if (left.TotalSeconds <= 5 * 60 && !_warned)
{
_warned = true;
MessageBox.Show("До окончания сеанса менее 5 минут. Проверьте кассу!",
"Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void UpdateTimerLabel()
{
var left = _sessionEnd - DateTime.Now;
if (left.TotalSeconds < 0) left = TimeSpan.Zero;
lblSessionTime.Text = $"{left.Minutes:00}:{left.Seconds:00}";
}
private void ShowLoginHistory()
{
var text = string.Join(Environment.NewLine,
AppData.LoginHistory
.OrderByDescending(h => h.Time)
.Select(h => $"{h.Time:yyyy-MM-dd HH:mm:ss} | {h.Login} | {(h.Success ? "Успешно" : "Неуспешно")}"));
MessageBox.Show(text, "История входа",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Logout()
{
_sessionTimer.Stop();
var login = new LoginForm();
Hide();
login.ShowDialog();
Close();
}
}