WindowEditAgent.xaml.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using DemoexamUser11.classes;
  15. namespace DemoexamUser11.windows
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для WindowEditAgent.xaml
  19. /// </summary>
  20. public partial class WindowEditAgent : Window
  21. {
  22. bool isNewAgent = false;
  23. Agent agent;
  24. List<AgentType> types = BaseConnector.BaseConnect.AgentType.ToList();
  25. public WindowEditAgent()
  26. {
  27. InitializeComponent();
  28. Title = "Добавление нового агента";
  29. isNewAgent = true;
  30. comboBoxAgentType.ItemsSource = types;
  31. comboBoxAgentType.SelectedValuePath = "ID";
  32. comboBoxAgentType.DisplayMemberPath = "Title";
  33. textBlockImagePath.Text = "/images/picture.png";
  34. }
  35. public WindowEditAgent(Agent agent)
  36. {
  37. InitializeComponent();
  38. Title = "Редактирование агента";
  39. this.agent = agent;
  40. comboBoxAgentType.ItemsSource = types;
  41. comboBoxAgentType.SelectedValuePath = "ID";
  42. comboBoxAgentType.DisplayMemberPath = "Title";
  43. comboBoxAgentType.SelectedValue = agent.AgentTypeID;
  44. }
  45. private void buttonSave_Click(object sender, RoutedEventArgs e)
  46. {
  47. try
  48. {
  49. if (isNewAgent == true)
  50. {
  51. int lastID = BaseConnector.BaseConnect.Agent.ToList().Last().ID + 1;
  52. BaseConnector.BaseConnect.Agent.Add(new Agent()
  53. {
  54. ID = lastID,
  55. Address = textBoxAddres.Text,
  56. Title = textBoxTitle.Text,
  57. AgentTypeID = Convert.ToInt32(comboBoxAgentType.SelectedValue),
  58. Priority = Convert.ToInt32(textBoxPriority.Text),
  59. Logo = textBlockImagePath.Text,
  60. INN = textBoxINN.Text,
  61. KPP = textBoxKPP.Text,
  62. DirectorName = textBoxNameDirector.Text,
  63. Phone = textBoxPhone.Text,
  64. Email = textBoxEmail.Text
  65. });
  66. BaseConnector.BaseConnect.SaveChanges();
  67. MessageBox.Show("Успешно добавлено!");
  68. }
  69. }
  70. catch
  71. {
  72. MessageBox.Show("Не удалось добавить агента!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  73. }
  74. }
  75. private void buttonDelete_Click(object sender, RoutedEventArgs e)
  76. {
  77. if (isNewAgent == false)
  78. {
  79. try
  80. {
  81. BaseConnector.BaseConnect.Agent.Remove(agent);
  82. BaseConnector.BaseConnect.SaveChanges();
  83. MessageBox.Show("Успех!");
  84. }
  85. catch
  86. {
  87. MessageBox.Show("Не удалось удалить агента!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  88. }
  89. }
  90. }
  91. }
  92. }