using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Avalonia.Controls; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using ReactiveUI; using TourAgent.Models; using TourAgent.Views; namespace TourAgent.ViewModels { public class HWindowViewModel : ViewModelBase { public static UP_SmirnovContext DB = new UP_SmirnovContext(); UserControl us = new Views.Menu(); public UserControl US { get => us; set => this.RaiseAndSetIfChanged(ref us, value); } MenuViewModel menuVM = new MenuViewModel(); public MenuViewModel MenuVM { get => menuVM; set => menuVM = value; } ToursViewModel _toursVM = new ToursViewModel(); public ToursViewModel ToursVM { get => _toursVM; set => _toursVM = value; } HotelsViewModel _hotelsVM = new HotelsViewModel(); public HotelsViewModel HotelsVM { get => _hotelsVM; set => _hotelsVM = value; } public void ChangePageToHotels() { US = new Hotels(); } public void ChangePageToTours() { US = new Tours(); } public void ToMenu() { US = new Views.Menu(); } } public class ToursViewModel : ReactiveObject { UP_SmirnovContext DB = new UP_SmirnovContext(); private List? _tours; public List? Tours { get => _tours; set => this.RaiseAndSetIfChanged(ref _tours, value); } private static List? _types; public static List? Types { get => _types; set => _types = value; } private static List? _sort; public static List? Sort { get => _sort; set => _sort = value; } private decimal _priceOfAll; public decimal PriceOfAll { get => _priceOfAll; set => this.RaiseAndSetIfChanged(ref _priceOfAll, value); } private bool _checkOrNot = false; public bool CheckOrNot { get => _checkOrNot; set { _checkOrNot = value; Filter(); } } private string? _findTour; public string? FindTour { get => _findTour; set { _findTour = value; Filter(); } } private string? _selectedItem; public string? SelectedItem { get { return _selectedItem; } set { _selectedItem = value; Filter(); } } private string? _selectedSort; public string? SelectedSort { get { return _selectedSort; } set { _selectedSort = value; Filter(); } } public ToursViewModel() { Tours = DB.Tours.ToList(); Types = DB.Types.Select(x=>x.Name).ToList(); Types.Insert(0, "Все"); SelectedItem = Types[0]; Sort = new List { "Без сортировки", "По возрастанию цены", "По убыванию цены" }; SelectedSort = Sort[0]; } public void Filter() { Tours = DB.Tours.ToList(); PriceOfAll = 0; if (SelectedItem != "Все") { List TypesForFilter = DB.TypeOfTours.ToList(); List IdForFilter = new List(); switch (SelectedItem) { case "Международный туризм": TypesForFilter = TypesForFilter.Where(x=>x.TypeId==1).ToList(); IdForFilter = TypesForFilter.Select(x=>x.TourId).ToList(); Tours = Tours.Where(x=> IdForFilter.Contains(x.Id)).ToList(); break; case "Внутренний туризм": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 2).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Лечебно-оздоровительные туры": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 3).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Пляжные туры": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 4).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Экскурсионные туры": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 5).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Специализированные детские туры": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 6).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Обслуживание корпоративных клиентов по заказу": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 7).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Горнолыжные курорты": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 8).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; case "Культурно-исторические туры": TypesForFilter = TypesForFilter.Where(x => x.TypeId == 9).ToList(); IdForFilter = TypesForFilter.Select(x => x.TourId).ToList(); Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList(); break; default: break; } } if(CheckOrNot) { Tours = Tours.Where(x => x.IsActual[0]==true).ToList(); } if(FindTour!=null && FindTour != "") { Tours = Tours.Where(x=>x.Name.ToUpper().Contains(FindTour.ToString().ToUpper()) || x.DescriptionWithoutNull.ToUpper().Contains(FindTour.ToString().ToUpper())).ToList(); } if(SelectedSort != "Без сортировки") { switch(SelectedSort) { case "По возрастанию цены": Tours = Tours.OrderBy(x=>x.Price).ToList(); break; case "По убыванию цены": Tours = Tours.OrderByDescending(x => x.Price).ToList(); break; } } foreach(Tour t in Tours) { PriceOfAll += (t.Price * t.TicketCount); } } } }