CreateOrUpdate.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace Golyshev43p180322
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для CreateOrUpdate.xaml
  20. /// </summary>
  21. public partial class CreateOrUpdate : Page
  22. {
  23. Agent agent = new Agent();
  24. bool flag;
  25. string path;
  26. public CreateOrUpdate()
  27. {
  28. InitializeComponent();
  29. flag = true;
  30. List<AgentType> AT = BaseClass.Base.AgentType.ToList();
  31. foreach (AgentType s in AT)
  32. {
  33. CBType.Items.Add(s.Title);
  34. }
  35. btnDelete.Visibility = Visibility.Collapsed;
  36. }
  37. public CreateOrUpdate(Agent AgentUpdate)
  38. {
  39. InitializeComponent();
  40. List<AgentType> AT = BaseClass.Base.AgentType.ToList();
  41. foreach (AgentType s in AT)
  42. {
  43. CBType.Items.Add(s.Title);
  44. }
  45. agent = AgentUpdate;
  46. TBTitle.Text = agent.Title;
  47. CBType.SelectedIndex = agent.AgentTypeID-1;
  48. TBPriority.Text = Convert.ToString(agent.Priority);
  49. TBAdress.Text = agent.Address;
  50. TBINN.Text = agent.INN;
  51. TBKPP.Text = agent.KPP;
  52. TBDirector.Text = agent.DirectorName;
  53. TBPone.Text = agent.Phone;
  54. TBEmail.Text = agent.Email;
  55. }
  56. private void btnGoBack_Click(object sender, RoutedEventArgs e)
  57. {
  58. FrameClass.MainFrame.Navigate(new ShowAgentPage());
  59. }
  60. private void btnSave_Click(object sender, RoutedEventArgs e)
  61. {
  62. try
  63. {
  64. string title = TBTitle.Text;
  65. int agenttype = CBType.SelectedIndex + 1;
  66. int priority = Convert.ToInt32(TBPriority.Text);
  67. string image = path;
  68. string adress = TBAdress.Text;
  69. string inn = TBINN.Text;
  70. string kpp = TBKPP.Text;
  71. string director = TBDirector.Text;
  72. string phone = TBPone.Text;
  73. string email = TBEmail.Text;
  74. if (priority < 0)
  75. {
  76. MessageBox.Show("Агент не добавлен!", "Ошибка");
  77. }
  78. else
  79. {
  80. agent.Title = title; agent.AgentTypeID = agenttype; agent.Priority = priority; agent.Logo = image; agent.Address = adress; agent.INN = inn;
  81. agent.KPP = kpp; agent.DirectorName = director; agent.Phone = phone; agent.Email = email;
  82. MessageBoxResult result = MessageBox.Show("Вы уверены сохранить?", "Добавление записи", MessageBoxButton.YesNo);
  83. if (result == MessageBoxResult.Yes)
  84. {
  85. if (flag == true)
  86. {
  87. BaseClass.Base.Agent.Add(agent);
  88. }
  89. BaseClass.Base.SaveChanges();
  90. MessageBox.Show("Данные записаны!", "Добавление записи");
  91. FrameClass.MainFrame.Navigate(new ShowAgentPage());
  92. }
  93. }
  94. }
  95. catch
  96. {
  97. MessageBox.Show("Агент не добавлен!", "Ошибка");
  98. }
  99. }
  100. private void btnDelete_Click(object sender, RoutedEventArgs e)
  101. {
  102. int id = agent.ID;
  103. Agent AgentDelete = BaseClass.Base.Agent.FirstOrDefault(x => x.ID == id);
  104. MessageBoxResult result = MessageBox.Show("Вы уверены что хотите удалить запись?", "Удаление записи", MessageBoxButton.YesNo);
  105. if (result == MessageBoxResult.Yes)
  106. {
  107. BaseClass.Base.Agent.Remove(AgentDelete);
  108. BaseClass.Base.SaveChanges();
  109. FrameClass.MainFrame.Navigate(new ShowAgentPage());
  110. MessageBox.Show("Запись удалена!", "Удаление записи");
  111. }
  112. }
  113. private void Button_Click(object sender, RoutedEventArgs e)
  114. {
  115. OpenFileDialog OFD = new OpenFileDialog();
  116. OFD.ShowDialog();
  117. path = OFD.FileName;
  118. int n = path.IndexOf("agent");
  119. path = path.Substring(n);
  120. }
  121. }
  122. }