using BPASmart.Model; using BPASmartClient.Helper; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmart.RecipeManagement.ViewModel { public class TechnologyProcessViewModel : ObservableObject { public static int currentItemId; public int Id { get { return _id; } set { _id = value; OnPropertyChanged(); } } private int _id = 0; public ObservableCollection technologyProcessModels { get; set; } = new ObservableCollection(); public string currentRecipeName { get { return _currentRecipeName; } set { _currentRecipeName = value; OnPropertyChanged(); } } private string _currentRecipeName = string.Empty; public RelayCommand CloseWindowCommand { get; set; } public ObservableCollection meterailItems { get; set; } = new ObservableCollection(); public RelayCommand ConfirmCommand { get; set; } public RelayCommand AddLastFlowItemCommand { get; set; } public RelayCommand AddFrontFlowItemCommand { get; set; } public RelayCommand DeleteFlowItemCommand { get; set; } public TechnologyProcessViewModel() { foreach (var item in Globle.GlobleData.recipeTechnologyProcess.recipeMaterials) { meterailItems.Add(item.Name); } ActionManage.GetInstance.Register(new Action((obj) => { if (obj != null) try { currentItemId = Convert.ToInt32(obj); } catch (Exception) { } }), "CurrentItemId"); technologyProcessModels = Globle.GlobleData.recipeTechnologyProcess.TechnologyProcessModels; CloseWindowCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("CloseTechnologyProcessView"); }); ConfirmCommand = new RelayCommand(() => { var res = Json.Data.locaRecipes.FirstOrDefault(p => p.ID == Globle.GlobleData.recipeTechnologyProcess.ID); if (res != null) { res.TechnologyProcessModels = technologyProcessModels; } Json.Save(); ; ActionManage.GetInstance.Send("CloseTechnologyProcessView"); }); AddFrontFlowItemCommand = new RelayCommand(() => { try { if (currentItemId == 0) { technologyProcessModels.Insert(0, new TechnologyProcessModel()); } else { technologyProcessModels.Insert(currentItemId, new TechnologyProcessModel()); currentItemId = currentItemId + 1; } } catch (Exception) { //throw; } }); AddLastFlowItemCommand = new RelayCommand(() => { try { if (technologyProcessModels.Count <= 0) { technologyProcessModels.Insert(0, new TechnologyProcessModel()); } else if (currentItemId != 0) { technologyProcessModels.Insert(currentItemId + 1, new TechnologyProcessModel()); } else { technologyProcessModels.Add(new TechnologyProcessModel()); } } catch (Exception) { // throw; } }); DeleteFlowItemCommand = new RelayCommand(() => { if (technologyProcessModels.Count > 0) technologyProcessModels.RemoveAt(currentItemId); currentItemId = 0; }); } } }