using Avalonia.Controls; using Avalonia.Media; using FinancialManagement.Models; using ReactiveUI; using FinancialManagement.Views; using FinancialManagement.ViewModels; using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; namespace FinancialManagement.ViewModels { public class MainWindowViewModel : ViewModelBase { ShmelevTeamContext ppContext = new ShmelevTeamContext(); UserControl uc = new AuthorizationPage(); AuthorizationPageViewModel authorizationPageViewModel = new AuthorizationPageViewModel(); RegistrationPageViewModel registrationPageViewModel; FinancialManagementViewModel financialManagementViewModel; FinanceTableViewModel financeTableViewModel; FinanceDataAccountViewModel financeDataAccountViewModel; UserProfileViewModel userProfileViewModel; CategoryTableViewModel categoryTableViewModel; CategoryDataAccountViewModel categoryDataAccountViewModel; public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); } public AuthorizationPageViewModel AuthorizationPageVM { get => authorizationPageViewModel; set => authorizationPageViewModel = value; } public RegistrationPageViewModel RegistrationPageVM { get => registrationPageViewModel; set => registrationPageViewModel = value; } public FinancialManagementViewModel FinancialManagementVM { get => financialManagementViewModel; set => financialManagementViewModel = value; } public FinanceTableViewModel FinanceTableVM { get => financeTableViewModel; set => financeTableViewModel = value; } public FinanceDataAccountViewModel FinanceDataAccountVM { get => financeDataAccountViewModel; set => financeDataAccountViewModel = value; } public UserProfileViewModel UserProfileVM { get => userProfileViewModel; set => userProfileViewModel = value; } public CategoryTableViewModel CategoryTableVM { get => categoryTableViewModel; set => categoryTableViewModel = value; } public CategoryDataAccountViewModel CategoryDataAccountVM { get => categoryDataAccountViewModel; set => categoryDataAccountViewModel = value; } public void LodedToProgram() { User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); if (currentUser != null) { FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username); UC = new FinancialManagementPage(); } else AuthorizationPageVM.Message = "Вы ввели неккоректные данные"; } public void LogedToProgram(User? currentUser) { AuthorizationPageVM.Login = currentUser.Login; AuthorizationPageVM.Password = currentUser.Password; if (currentUser != null) { FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext); FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username); UC = new FinancialManagementPage(); } else AuthorizationPageVM.Message = "Вы ввели неккоректные данные"; } public void ToAuthorizathionPage() { UC = new AuthorizationPage(); } public void ToFinanceManagementPage() { UC = new FinancialManagementPage(); } public void RegistrationInDB() { User? currentUser = registrationPageViewModel.AddUserInDataBase(); if (currentUser != null) { LogedToProgram(currentUser); } } public void ToRegistrationPage() { registrationPageViewModel = new RegistrationPageViewModel(ppContext); UC = authorizationPageViewModel.LoadPageRegistration(); } public void ToFinanceTable() { User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext); FinanceDataAccountVM = new FinanceDataAccountViewModel(); UC = new FinanceTable(); } public void OpenNewFinanceDate() { User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext); FinanceDataAccountVM = new FinanceDataAccountViewModel(); FinanceDataAccountVM = new FinanceDataAccountViewModel(ppContext, currentUser); UC = new FinanceDataAccount(); } public void OpenFinance() { User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); FinanceDataAccountVM = new FinanceDataAccountViewModel(FinanceTableVM.ItemFinance,currentUser, ppContext); UC = new FinanceDataAccount(); } public bool SaveFinance() { User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); Dictionary objectFinance = FinanceDataAccountVM.GetDataFinance(currentUser); if (objectFinance.ContainsKey("4")) { Finance currentFinance = objectFinance["4"]; if (ppContext.Finances.FirstOrDefault(x => x.FinanceId == currentFinance.FinanceId) != null) { ppContext.SaveChanges(); } else { ppContext.Add(currentFinance); ppContext.SaveChanges(); } FinanceDataAccountVM.Message = "Данные сохранены в базе"; FinanceDataAccountVM.ColorMessage = Brushes.Azure; return true; } else if (objectFinance.ContainsKey("3")) { FinanceDataAccountVM.Message = "Нулевые данные"; FinanceDataAccountVM.ColorMessage = Brushes.Red; return false; } else if (objectFinance.ContainsKey("2")) { FinanceDataAccountVM.Message = "Название категории не может превышать длину в 50 символов"; FinanceDataAccountVM.ColorMessage = Brushes.Red; return false; } else if (objectFinance.ContainsKey("1")) { FinanceDataAccountVM.Message = "Не все поля заполнены"; FinanceDataAccountVM.ColorMessage = Brushes.Red; return false; } return false; } public void DeleteSrticleInBD() { if (FinanceTableVM.Finance == null) { return; } else { ppContext.Remove(FinanceTableVM.ItemFinance); ppContext.SaveChanges(); ToFinanceTable(); } } public void ToProfile() { User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); UserProfileVM = new UserProfileViewModel(currentUser, ppContext); UC = new UserProfilePage(); } public void SaveChangeUserData() { User currentUser = UserProfileVM.SaveChanges(); if (currentUser != null) { LogedToProgram(currentUser); } } public void BackNotChanges() { LodedToProgram(); } public void ToCategoryTable() { User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext); UC = new CategoryTable(); } public void ToFinanceDataAccount() { UC = new FinanceDataAccount(); } public void ToCategoryData() { UC = new CategoryDataAccount(); } public void OpenNewCategoryData() { User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext); CategoryDataAccountVM = new CategoryDataAccountViewModel(); CategoryDataAccountVM = new CategoryDataAccountViewModel(ppContext, currentUser); UC = new CategoryDataAccount(); } public void OpenCategory() { User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); CategoryDataAccountVM = new CategoryDataAccountViewModel(CategoryTableVM.ItemCategory, currentUser, ppContext); UC = new CategoryDataAccount(); } public bool SaveCategory() { User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password); Dictionary objectFinance = CategoryDataAccountVM.GetDataFinance(currentUser); if (objectFinance.ContainsKey("4")) { Category currentFinance = objectFinance["4"]; if (ppContext.Categories.FirstOrDefault(x => x.CategoryId == currentFinance.CategoryId) != null) { ppContext.SaveChanges(); } else { ppContext.Add(currentFinance); ppContext.SaveChanges(); } CategoryDataAccountVM.Message = "Данные сохранены в базе"; CategoryDataAccountVM.ColorMessage = Brushes.Azure; return true; } else if (objectFinance.ContainsKey("2")) { CategoryDataAccountVM.Message = "Название категории не может превышать длину в 50 символов"; CategoryDataAccountVM.ColorMessage = Brushes.Red; return false; } else if (objectFinance.ContainsKey("1")) { CategoryDataAccountVM.Message = "Не все поля заполнены"; CategoryDataAccountVM.ColorMessage = Brushes.Red; return false; } return false; } public void DeleteCategory() { if (CategoryTableVM.Categores == null) { return; } else { ppContext.Remove(CategoryTableVM.ItemCategory); ppContext.SaveChanges(); ToCategoryTable(); } } public void ShowHidePasswordAuthorization() { if (AuthorizationPageVM.PasswordChar == null) { AuthorizationPageVM.PasswordChar = '*'; } else if (AuthorizationPageVM.PasswordChar != null) { AuthorizationPageVM.PasswordChar = null; } } public void ShowHidePasswordRegistration() { if (RegistrationPageVM.PasswordChar == null) { RegistrationPageVM.PasswordChar = '*'; } else if (RegistrationPageVM.PasswordChar != null) { RegistrationPageVM.PasswordChar = null; } } public void ShowHidePasswordProfile() { if (UserProfileVM.PasswordChar == null) { UserProfileVM.PasswordChar = '*'; } else if (UserProfileVM.PasswordChar != null) { UserProfileVM.PasswordChar = null; } } public void ToLoadOut() { AuthorizationPageVM.Login = null; AuthorizationPageVM.Password = null; ToAuthorizathionPage(); } } }