LoginForm.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using TravelAgencyWinForms.Data;
  2. using TravelAgencyWinForms.Services;
  3. using TravelAgencyWinForms.Utils;
  4. namespace TravelAgencyWinForms.Forms;
  5. public partial class LoginForm : Form
  6. {
  7. public LoginForm()
  8. {
  9. InitializeComponent();
  10. ApplyStyle();
  11. LoadIllustration();
  12. loginTextBox.Text = "admin";
  13. }
  14. private void ApplyStyle()
  15. {
  16. AppTheme.ApplyForm(this, true);
  17. AppTheme.StyleTextBox(loginTextBox);
  18. AppTheme.StyleTextBox(passwordTextBox);
  19. AppTheme.StylePrimaryButton(loginButton);
  20. AppTheme.StyleSecondaryButton(exitButton);
  21. titleLabel.ForeColor = AppTheme.Text;
  22. subtitleLabel.ForeColor = AppTheme.Muted;
  23. loginLabel.ForeColor = AppTheme.Text;
  24. passwordLabel.ForeColor = AppTheme.Text;
  25. statusLabel.ForeColor = AppTheme.Muted;
  26. rootPanel.BackColor = AppTheme.Background;
  27. imagePanel.BackColor = AppTheme.Surface;
  28. authPanel.BackColor = AppTheme.Surface;
  29. }
  30. private void LoadIllustration()
  31. {
  32. var image = AppTheme.LoadImage("login_welcome.png");
  33. if (image != null)
  34. welcomePictureBox.Image = image;
  35. }
  36. private void LoginButton_Click(object? sender, EventArgs e)
  37. {
  38. TryLogin();
  39. }
  40. private void ExitButton_Click(object? sender, EventArgs e)
  41. {
  42. Close();
  43. }
  44. private void PasswordTextBox_KeyDown(object? sender, KeyEventArgs e)
  45. {
  46. if (e.KeyCode != Keys.Enter)
  47. return;
  48. e.SuppressKeyPress = true;
  49. TryLogin();
  50. }
  51. private void TryLogin()
  52. {
  53. if (string.IsNullOrWhiteSpace(loginTextBox.Text) || string.IsNullOrWhiteSpace(passwordTextBox.Text))
  54. {
  55. statusLabel.Text = "Заполните логин и пароль.";
  56. return;
  57. }
  58. Cursor = Cursors.WaitCursor;
  59. try
  60. {
  61. if (AuthService.Login(loginTextBox.Text, passwordTextBox.Text, out var error))
  62. {
  63. DialogResult = DialogResult.OK;
  64. Close();
  65. }
  66. else
  67. {
  68. statusLabel.Text = error;
  69. }
  70. }
  71. finally
  72. {
  73. Cursor = Cursors.Default;
  74. }
  75. }
  76. }