using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using System.Windows.Input; using XLIMS.CONTRACT; using XLIMS.DATA.Models; using XLIMS.MVVM.Base; using XLIMS.TPRZ.Views; namespace XLIMS.TPRZ.ViewModels { public class MainViewModel : ViewModelBase,IActivityViewModel { #region Constructor public MainViewModel(ILimsService limsService, IDialogService dialogService) { _limsService = limsService; _dialogService = dialogService; _limsService.Spois.SetChanged += OnSpoisChanged; _limsService.Tips.SetChanged += OnTipsChanged; _limsService.Tprzs.SetChanged += OnTprzsChanged; LoadSpoiAsync(); } #endregion //Constructor #region Events private async void OnSpoisChanged() { await LoadSpoiAsync(); } private async void OnTipsChanged() { await LoadTipAsync(); } private async void OnTprzsChanged() { await LoadTprzAsync(); } #endregion //Events #region Fields private readonly IDialogService _dialogService; private readonly ILimsService _limsService; private bool _isLoading; private TipViewModel _currentTip; private TprzViewModel _currentTprz; private Spoi _currentSpoi; #endregion //Fields #region Properties public string Title => "ТПРМ"; public object View => new MainView(); public ObservableCollection AllSpois { get; set; } = new(); public ObservableCollection AllTips { get; set; } = new(); public ObservableCollection AllTprzs { get; set; } = new(); public Spoi CurrentSpoi { get => _currentSpoi; set { _currentSpoi = value; if (_currentSpoi != null) LoadTipAsync(); OnPropertyChanged(); } } public TipViewModel CurrentTip { get => _currentTip; set { _currentTip = value; if (_currentTip != null) LoadTprzAsync(); OnPropertyChanged(); } } public TprzViewModel CurrentTprz { get => _currentTprz; set { _currentTprz = value; OnPropertyChanged(); } } public bool IsLoading { get => _isLoading; set { _isLoading = value; OnPropertyChanged(); } } #endregion //Properties #region Methods public async Task LoadSpoiAsync() { IsLoading = true; try { // Параллельная загрузка данных из разных доменов через подсервисы var spoisTask = await _limsService.Spois.GetAllAsync(); AllSpois = new ObservableCollection(spoisTask); OnPropertyChanged(nameof(AllSpois)); } finally { IsLoading = false; } } public async Task LoadTipAsync() { IsLoading = true; try { // Параллельная загрузка данных из разных доменов через подсервисы var tipsTask = await _limsService.Tips.GetAllAsync(); AllTips = new ObservableCollection(tipsTask.Where(e=>e.Idspoi==CurrentSpoi.Id).Select(s=>new TipViewModel(_limsService,null,s))); OnPropertyChanged(nameof(AllTips)); } finally { IsLoading = false; } } public async Task LoadTprzAsync() { IsLoading = true; try { // Параллельная загрузка данных из разных доменов через подсервисы var tprzsTask = await _limsService.Tprzs.GetAllAsync(); AllTprzs = new ObservableCollection(tprzsTask.Where(e=>e.Idtip==CurrentTip.Id).Select(s => new TprzViewModel(_limsService,0,s))); OnPropertyChanged(nameof(AllTprzs)); } finally { IsLoading = false; } } private void AddTip() { _dialogService.ShowDialog(new TipViewModel(_limsService,CurrentSpoi)); } private void EditTip() { _dialogService.ShowDialog(CurrentTip); } private async Task DelTipAsync() { await CurrentTip.Remove(); AllTips.Remove(CurrentTip); OnPropertyChanged(nameof(AllTips)); } private void AddTprz() { _dialogService.ShowDialog(new TprzViewModel(_limsService,CurrentTip.Id)); } private void EditTprz() { _dialogService.ShowDialog(CurrentTprz); } private async Task DelTprzAsync() { await CurrentTprz.Remove(); AllTprzs.Remove(CurrentTprz); OnPropertyChanged(nameof(AllTprzs)); } #endregion //Methods #region Commands public ICommand AddTipCommand => new RelayCommand(p => AddTip(),p=>CurrentSpoi!=null); public ICommand EditTipCommand => new RelayCommand(p => EditTip(), p => CurrentTip != null); public ICommand DelTipCommand => new AsyncRelayCommand(DelTipAsync, () => CurrentTip != null); public ICommand AddTprzCommand => new RelayCommand(p => AddTprz(),p=>CurrentTip!=null); public ICommand EditTprzCommand => new RelayCommand(p => EditTprz(), p => CurrentTprz != null); public ICommand DelTprzCommand => new AsyncRelayCommand(DelTprzAsync, () => CurrentTprz != null); #endregion //Commands } }