ServiceListPageViewModel.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using kuzminIVProjectWorkOnDemo.Models;
  7. using ReactiveUI;
  8. namespace kuzminIVProjectWorkOnDemo.ViewModels
  9. {
  10. public class ServiceListPageViewModel : ReactiveObject
  11. {
  12. KuzmintestprojContext db;
  13. List<Sevice> services;
  14. public ServiceListPageViewModel(KuzmintestprojContext db)
  15. {
  16. this.db = db;
  17. this.services = db.Sevices.ToList();
  18. }
  19. public List<Sevice> Services { get => services; set => this.RaiseAndSetIfChanged(ref services, value); }
  20. int indexsort;
  21. int costindex;
  22. string search;
  23. string textcount;
  24. public int IndexSort { get => indexsort; set { indexsort = value; filter(); } }
  25. public int Costindex { get => costindex; set { costindex = value; filter(); } }
  26. public string Search { get => search; set { search = value; filter(); } }
  27. public string Textcount { get => textcount; set => this.RaiseAndSetIfChanged(ref textcount, value); }
  28. public bool AdminBool { get => Models.Sevice.IsVisibleAdmin; }
  29. void filter()
  30. {
  31. Services = db.Sevices.ToList();
  32. switch (Costindex)
  33. {
  34. case 1:
  35. Services = Services.Where(x => x.Discount >= 0 && x.Discount < 0.05).ToList();
  36. break;
  37. case 2:
  38. Services = Services.Where(x => x.Discount >= 0.05 && x.Discount < 0.15).ToList();
  39. break;
  40. case 3:
  41. Services = Services.Where(x => x.Discount >= 0.15 && x.Discount < 0.3).ToList();
  42. break;
  43. case 4:
  44. Services = Services.Where(x => x.Discount >= 0.3 && x.Discount < 0.7).ToList();
  45. break;
  46. case 5:
  47. Services = Services.Where(x => x.Discount >= 0.7 && x.Discount < 1).ToList();
  48. break;
  49. }
  50. if (!string.IsNullOrWhiteSpace(Search))
  51. {
  52. Services = Services.Where(x => x.Title.ToLower().Contains(Search.ToLower())).ToList();
  53. }
  54. switch (IndexSort)
  55. {
  56. case 0:
  57. Services = Services.OrderBy(x => x.CostWithDiscount).ToList();
  58. break;
  59. case 1:
  60. Services = Services.OrderByDescending(x => x.CostWithDiscount).ToList();
  61. break;
  62. }
  63. Textcount = Services.Count.ToString() + " èç " + db.Sevices.ToList().Count.ToString();
  64. }
  65. public void pressButton()
  66. {
  67. ClientZapisWindow clientZapisWindow = new ClientZapisWindow { DataContext = new ClientZapisWindowViewModel() };
  68. var mainWindow = Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop ? desktop.MainWindow : null;
  69. clientZapisWindow.ShowDialog(mainWindow);
  70. //clientZapisWindow.Show();
  71. }
  72. }
  73. }