AddEditAgent.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Shapes;
  16. using WpfApp1.SQL;
  17. namespace WpfApp1.Windows
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для AddEditAgent.xaml
  21. /// </summary>
  22. public partial class AddEditAgent : Window, INotifyPropertyChanged
  23. {
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. public Agent CurrentAgent { get; set; }
  26. public List<AgentType> AgentTypeList { get; set; }
  27. public string Name
  28. {
  29. get
  30. {
  31. return CurrentAgent.ID == 0 ? "Новый агенат" : "Редактировать агента";
  32. }
  33. }
  34. private IEnumerable<Agent> _AgentList;
  35. public IEnumerable<Agent> AgentList
  36. {
  37. get
  38. {
  39. return _AgentList;
  40. }
  41. set
  42. {
  43. _AgentList = value;
  44. }
  45. }
  46. public AddEditAgent(Agent agents)
  47. {
  48. InitializeComponent();
  49. DataContext = this;
  50. CurrentAgent = agents;
  51. AgentList = Core.DB.Agent.ToList();
  52. AgentTypeList = Core.DB.AgentType.ToList();
  53. //CurrentAgent = Core.DB.Agent.ToList();
  54. }
  55. private void AddImage_Click(object sender, RoutedEventArgs e)
  56. {
  57. OpenFileDialog GetImageDialog = new OpenFileDialog();
  58. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jng)| *.png; *,jnp";
  59. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  60. if (GetImageDialog.ShowDialog() == true)
  61. {
  62. CurrentAgent.Logo = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  63. PropertyChanged(this, new PropertyChangedEventArgs("AgentList"));
  64. }
  65. }
  66. private void AddButton_Click(object sender, RoutedEventArgs e)
  67. {
  68. if (CurrentAgent.ID == 0) Core.DB.Agent.Add(CurrentAgent);
  69. try
  70. {
  71. Core.DB.SaveChanges();
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show("Ошибка");
  76. return;
  77. }
  78. DialogResult = true;
  79. }
  80. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  81. {
  82. if (MessageBox.Show("Ты уверен?", "Внимание", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  83. {
  84. try
  85. {
  86. if (CurrentAgent.ProductSale.Count > 0)
  87. {
  88. MessageBox.Show("Нелья удалять агент, если есть продаж агентов или история изменения приоритета");
  89. return;
  90. }
  91. Core.DB.Agent.Remove(CurrentAgent);
  92. Core.DB.SaveChanges();
  93. }
  94. catch (Exception ex)
  95. {
  96. MessageBox.Show(ex.Message, "Ошибка");
  97. }
  98. DialogResult = true;
  99. }
  100. }
  101. }
  102. }