using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using XLIMS.CONTRACT; using XLIMS.DATA.Models; using XLIMS.MVVM.Base; using XLIMS.PSV.Views; namespace XLIMS.PSV.ViewModels { public class MainViewModel : ViewModelBase,IActivityViewModel { #region Constructor public MainViewModel(ILimsService limsService, IDialogService dialogService) { _limsService = limsService; _dialogService = dialogService; LoadSide(); _limsService.Datas.SetChanged += Datas_SetChanged; } private void Datas_SetChanged() { } #endregion //Constructor #region Events #endregion //Events #region Fields private readonly ILimsService _limsService; private readonly IDialogService _dialogService; private SideItemViewModel _currentSideItem; private DocumentSet _currentPsv; private IGrouping _currentGroupPov; private DataSet _currentPov; #endregion //Fields #region Properties public ObservableCollection SideItems { get; set; } = new(); public SideItemViewModel CurrentSideItem { get => _currentSideItem; set { _currentSideItem = value; if (_currentSideItem != null) { LoadPsvAsync(_currentSideItem.Index); } OnPropertyChanged(); } } public ObservableCollection AllPsvs { get; set; } = new(); public DocumentSet CurrentPsv { get => _currentPsv; set { _currentPsv = value; if (_currentPsv != null) { LoadDataAsync(_currentPsv); } OnPropertyChanged(); } } public ObservableCollection> GroupedPovs { get; set; } public IGrouping CurrentGroupPov { get => _currentGroupPov; set { _currentGroupPov = value; OnPropertyChanged(); } } public DataSet CurrentPov { get => _currentPov; set { _currentPov = value; OnPropertyChanged(); } } public string Title => "ПСВ"; public object View => new MainView(); #endregion //Properties #region Methods public void LoadSide() { var side=new List(); side.Add(new SideItemViewModel(1,"Срочные")); side.Add(new SideItemViewModel(2,"Открытые")); side.Add(new SideItemViewModel(3,"Архив")); SideItems = new ObservableCollection(side); OnPropertyChanged(nameof(SideItems)); } public async Task LoadPsvAsync(int index) { try { var psvsTask = await _limsService.Documents.GetAllAsync(); if (index == 1) { AllPsvs = new ObservableCollection(psvsTask.Where(e => e.Name == "ПСВ" && (DateTime.Today - e.DocDate).Value.TotalDays > 15)); } if (index == 2) { AllPsvs = new ObservableCollection(psvsTask.Where(e => e.Name == "ПСВ")); } if (index == 3) { AllPsvs = new ObservableCollection(psvsTask.Where(e => e.Name == "ПСВ" && e.Status == "close")); } } finally { OnPropertyChanged(nameof(AllPsvs)); } } public async Task LoadDataAsync(DocumentSet psv) { try { var povsTask = await Task.Run>(()=>psv.DataSetDocuments.ToList()); GroupedPovs = new ObservableCollection>(povsTask.GroupBy(e => e.Device.Dpzn + e.Device.Hrtc + e.Device.Gsrs)); } finally { OnPropertyChanged(nameof(GroupedPovs)); } } private void AddPsv() { _dialogService.ShowDialog(new PsvViewModel(_limsService, _dialogService)); } private void EditPsv() { _dialogService.ShowDialog(new PsvViewModel(_limsService, _dialogService,CurrentPsv)); } private async Task DelPsvAsync() { await _limsService.Documents.RemoveAsync(CurrentPsv); AllPsvs.Remove(CurrentPsv); } private void GoodPov() { } private void BadPov() { _dialogService.ShowDialog(new BadViewModel(_limsService,CurrentPov)); } #endregion //Methods #region Commands public ICommand AddPsvCommand => new RelayCommand(p => AddPsv()); public ICommand EditPsvCommand => new RelayCommand(p => EditPsv(),p=>CurrentPsv!=null); public ICommand BadCommand => new RelayCommand(p => BadPov()); public ICommand DelPsvCommand => new AsyncRelayCommand(DelPsvAsync,()=>CurrentPsv!=null); #endregion //Commands } }