AddWindow.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  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. namespace Eight
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для AddWindow.xaml
  20. /// </summary>
  21. public partial class AddWindow : Window
  22. {
  23. public AddWindow()
  24. {
  25. InitializeComponent();
  26. Load();
  27. }
  28. private void Load()
  29. {
  30. CmbType.ItemsSource = Helper.GetContext().ProductType.ToList();
  31. ProductsBox.ItemsSource = Helper.GetContext().Material.ToList();
  32. }
  33. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  34. {
  35. if (ProductsBox.SelectedItem is Material material)
  36. {
  37. var materialDuplicate = BasketBox.Items.Cast<Record>().ToList().SingleOrDefault(x => x.Material == material);
  38. if (materialDuplicate == null)
  39. {
  40. BasketBox.Items.Add(new Record() { Material = material, Count = 1 });
  41. }
  42. else
  43. {
  44. materialDuplicate.Count++;
  45. }
  46. BasketBox.Items.Refresh();
  47. }
  48. }
  49. private void BtnRemove_Click(object sender, RoutedEventArgs e)
  50. {
  51. if (BasketBox.SelectedItem is Record record)
  52. {
  53. BasketBox.Items.Remove(record);
  54. }
  55. }
  56. private void BtnSubmit_Click(object sender, RoutedEventArgs e)
  57. {
  58. if (CmbType.SelectedItem is ProductType productType
  59. && decimal.TryParse(TbMinCost.Text, out decimal cost)
  60. && int.TryParse(TbPerson.Text, out int personCount)
  61. && int.TryParse(TbWorkshop.Text, out int workshop))
  62. {
  63. Product product = new Product()
  64. {
  65. Title = TbName.Text,
  66. ProductType = productType,
  67. Description = TbDescription.Text,
  68. ArticleNumber = TbArticle.Text,
  69. Image = path,
  70. MinCostForAgent = cost,
  71. ProductionPersonCount = personCount,
  72. ProductionWorkshopNumber = workshop,
  73. ProductMaterial = BasketBox.Items.Cast<Record>().ToList().Select(x => new ProductMaterial() { Material = x.Material, Count = x.Count }).ToList(),
  74. };
  75. Helper.GetContext().Product.Add(product);
  76. Helper.GetContext().SaveChanges();
  77. }
  78. }
  79. class Record
  80. {
  81. public Material Material { get; set; }
  82. public int Count { get; set; }
  83. }
  84. public string path;
  85. private void BtnChoose_Click(object sender, RoutedEventArgs e)
  86. {
  87. try
  88. {
  89. OpenFileDialog openFileDialog = new OpenFileDialog();
  90. if (openFileDialog.ShowDialog() == true)
  91. {
  92. var fileName = openFileDialog.FileName;
  93. path = "\\products\\" + openFileDialog.SafeFileName;
  94. File.Copy(fileName, Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName, true);
  95. BitmapImage bitmapImage = new BitmapImage();
  96. bitmapImage.BeginInit();
  97. bitmapImage.UriSource = new Uri(Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName);
  98. bitmapImage.EndInit();
  99. Img.Source = bitmapImage;
  100. }
  101. }
  102. catch (Exception)
  103. {
  104. return;
  105. }
  106. }
  107. }
  108. }