ManagerEditForm.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows.Forms;
  4. namespace WindowsFormsApp4
  5. {
  6. public partial class ManagerEditForm : Form
  7. {
  8. private int? managerId;
  9. private bool isEditMode;
  10. public ManagerEditForm()
  11. {
  12. InitializeComponent();
  13. isEditMode = false;
  14. lblTitle.Text = "Добавление менеджера";
  15. AttachEventHandlers();
  16. }
  17. public ManagerEditForm(int id, string lastName, string firstName, string middleName, string phone)
  18. {
  19. InitializeComponent();
  20. isEditMode = true;
  21. managerId = id;
  22. lblTitle.Text = "Редактирование менеджера";
  23. txtLastName.Text = lastName;
  24. txtFirstName.Text = firstName;
  25. txtMiddleName.Text = middleName;
  26. txtPhone.Text = phone;
  27. AttachEventHandlers();
  28. }
  29. private void BtnCancel_Click(object sender, EventArgs e)
  30. {
  31. this.DialogResult = DialogResult.Cancel;
  32. this.Close();
  33. }
  34. private void AttachEventHandlers()
  35. {
  36. btnSave.Click += BtnSave_Click;
  37. btnCancel.Click += BtnCancel_Click;
  38. }
  39. private bool ValidateFields()
  40. {
  41. if (string.IsNullOrWhiteSpace(txtLastName.Text))
  42. {
  43. MessageBox.Show("Введите фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  44. txtLastName.Focus();
  45. return false;
  46. }
  47. if (string.IsNullOrWhiteSpace(txtFirstName.Text))
  48. {
  49. MessageBox.Show("Введите имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  50. txtFirstName.Focus();
  51. return false;
  52. }
  53. string phone = txtPhone.Text.Trim();
  54. if (string.IsNullOrWhiteSpace(phone))
  55. {
  56. MessageBox.Show("Введите номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  57. txtPhone.Focus();
  58. return false;
  59. }
  60. Regex phoneRegex = new Regex(@"^(\+7|8)[0-9]{10}$");
  61. if (!phoneRegex.IsMatch(phone))
  62. {
  63. MessageBox.Show("Некорректный номер телефона. Формат: +7XXXXXXXXXX или 8XXXXXXXXXX",
  64. "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  65. txtPhone.Focus();
  66. return false;
  67. }
  68. return true;
  69. }
  70. private void BtnSave_Click(object sender, EventArgs e)
  71. {
  72. if (!ValidateFields())
  73. return;
  74. bool result;
  75. if (isEditMode && managerId.HasValue)
  76. {
  77. result = DatabaseHelper.UpdateManager(
  78. managerId.Value,
  79. txtLastName.Text.Trim(),
  80. txtFirstName.Text.Trim(),
  81. txtMiddleName.Text.Trim(),
  82. txtPhone.Text.Trim()
  83. );
  84. }
  85. else
  86. {
  87. int newId = DatabaseHelper.AddManager(
  88. txtLastName.Text.Trim(),
  89. txtFirstName.Text.Trim(),
  90. txtMiddleName.Text.Trim(),
  91. txtPhone.Text.Trim()
  92. );
  93. result = newId > 0;
  94. }
  95. if (result)
  96. {
  97. this.DialogResult = DialogResult.OK;
  98. this.Close();
  99. }
  100. else
  101. {
  102. MessageBox.Show("Ошибка при сохранении", "Ошибка",
  103. MessageBoxButtons.OK, MessageBoxIcon.Error);
  104. }
  105. }
  106. }
  107. }