PageHotel.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Navigation;
  14. using System.Windows.Shapes;
  15. namespace WpfAppHotel
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для PageHotel.xaml
  19. /// </summary>
  20. public partial class PageHotel : Page
  21. {
  22. private readonly Pagination Pagin = new Pagination();
  23. private List<Hotel> HotelList = new List<Hotel>();
  24. public PageHotel()
  25. {
  26. InitializeComponent();
  27. DataContext = Pagin;
  28. HotelList = DataBase.DB.Hotel.ToList();
  29. ListHotel.ItemsSource = HotelList;
  30. tboxPageCount.Text = "10";
  31. }
  32. private void EditingCurrentPage_MouseDown(object sender, MouseButtonEventArgs e)
  33. {
  34. TextBlock tb = (TextBlock)sender;
  35. switch (tb.Uid)
  36. {
  37. case "first":
  38. Pagin.CurrentPage = 1;
  39. break;
  40. case "last":
  41. Pagin.CurrentPage = HotelList.Count;
  42. break;
  43. case "back":
  44. Pagin.CurrentPage--;
  45. break;
  46. case "next":
  47. Pagin.CurrentPage++;
  48. break;
  49. default:
  50. Pagin.CurrentPage = Convert.ToInt32(tb.Text);
  51. break;
  52. }
  53. ListHotel.ItemsSource = HotelList.Skip((Pagin.CurrentPage * Pagin.CountPage) - Pagin.CountPage).Take(Pagin.CountPage).ToList();
  54. }
  55. private void tboxPageCount_TextChanged(object sender, TextChangedEventArgs e)
  56. {
  57. SetPagination();
  58. }
  59. private void SetPagination()
  60. {
  61. try
  62. {
  63. if (Convert.ToInt32(tboxPageCount.Text) > 0)
  64. {
  65. Pagin.CountPage = Convert.ToInt32(tboxPageCount.Text);
  66. }
  67. }
  68. catch
  69. {
  70. Pagin.CountPage = HotelList.Count;
  71. }
  72. Pagin.CountList = HotelList.Count;
  73. ListHotel.ItemsSource = HotelList.Skip(0).Take(Pagin.CountPage).ToList();
  74. }
  75. private void btnExit_Click(object sender, RoutedEventArgs e)
  76. {
  77. _ = MainFrame.MFrame.Navigate(new PageMain());
  78. }
  79. private void btnAdd_Click(object sender, RoutedEventArgs e)
  80. {
  81. WindowAddHotel addHotel = new WindowAddHotel();
  82. _ = addHotel.ShowDialog();
  83. HotelList = DataBase.DB.Hotel.ToList();
  84. SetPagination();
  85. }
  86. private void btnDelete_Click(object sender, RoutedEventArgs e)
  87. {
  88. Button button = (Button)sender;
  89. int id = Convert.ToInt32(button.Uid);
  90. Hotel hotel = DataBase.DB.Hotel.FirstOrDefault(x => x.Id == id);
  91. foreach (Tour item1 in DataBase.DB.Tour)
  92. {
  93. if (hotel.Tour == item1 && item1.IsActual == true)
  94. {
  95. _ = MessageBox.Show("Вы пытаетесь удалить отель для актуального тура");
  96. return;
  97. }
  98. }
  99. string nameHotel = hotel.Name;
  100. foreach (HotelImage item in DataBase.DB.HotelImage.Where(x => x.HotelId == id))
  101. {
  102. _ = DataBase.DB.HotelImage.Remove(item);
  103. }
  104. _ = DataBase.DB.Hotel.Remove(hotel);
  105. _ = DataBase.DB.SaveChanges();
  106. _ = MessageBox.Show("Был удален отель: " + nameHotel);
  107. HotelList = DataBase.DB.Hotel.ToList();
  108. ListHotel.ItemsSource = HotelList;
  109. }
  110. }
  111. }