using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.FSE.Common; using Tango.FSE.Common.Navigation; using Tango.FSE.UsersAndRoles.Navigation; using Tango.FSE.UsersAndRoles.Views; using static Tango.FSE.UsersAndRoles.ViewModels.OrganizationUsersViewVM; namespace Tango.FSE.UsersAndRoles.ViewModels { public class OrganizationUsersViewVM : UsersAndRolesViewModel, INavigationObjectReceiver { public class NavigationObject { public Organization Organization { get; set; } public User SelectedUser { get; set; } } private bool _requiresReloading; private User _initialUser; private Organization _organization; /// /// Gets or sets the organization. /// public Organization Organization { get { return _organization; } set { _organization = value; RaisePropertyChangedAuto(); } } private ObservableCollection _users; /// /// Gets or sets the users. /// public ObservableCollection Users { get { return _users; } set { _users = value; RaisePropertyChangedAuto(); } } private User _selectedUser; public User SelectedUser { get { return _selectedUser; } set { _selectedUser = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the users view source. /// public ICollectionView UsersView { get; set; } private String _filter; /// /// Gets or sets the users search filter. /// public String Filter { get { return _filter; } set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } private bool _showDeleted; public bool ShowDeleted { get { return _showDeleted; } set { _showDeleted = value; RaisePropertyChangedAuto(); UsersView?.Refresh(); } } /// /// Navigates to the user details view with the specified user. /// public RelayCommand EditUserCommand { get; set; } /// /// Removes the specified user. /// public RelayCommand RemoveUserCommand { get; set; } /// /// Adds a new user. /// public RelayCommand AddUserCommand { get; set; } /// /// Initializes a new instance of the class. /// public OrganizationUsersViewVM() { Users = new ObservableCollection(); EditUserCommand = new RelayCommand(EditUser); RemoveUserCommand = new RelayCommand(SuspendUser); AddUserCommand = new RelayCommand(AddUser, () => IsFree); } private void AddUser() { ModularNavigationManager.NavigateTo(UsersAndRolesView.UserDetailsView, new UserDetailsViewVM.NavigationObject() { IsNewUser = true, Organization = Organization }); } private async void SuspendUser(User user) { if (await NotificationProvider.ShowWarningQuestion($"Are you sure you want to suspend '{user.Contact.FullName}' ?")) { try { IsFree = false; using (NotificationProvider.PushTaskItem("Suspending account...")) { var updatedUser = await Services.OrganizationsService.SuspendUser(user); Users.Replace(user, updatedUser); } await NotificationProvider.ShowSuccess("The selected user is now suspended and can be reactivated."); } catch (Exception ex) { LogManager.Log(ex, $"Error suspending user '{user.Email}'."); await NotificationProvider.ShowError($"Error suspending the selected user\n{ex.FlattenMessage()}"); } finally { IsFree = true; } } } private void EditUser(User user) { ModularNavigationManager.NavigateTo(UsersAndRolesView.UserDetailsView, new UserDetailsViewVM.NavigationObject() { User = user }); } private async Task LoadUsers() { if (Organization != null) { try { IsFree = false; var users = await Services.OrganizationsService.GetOrganizationUsers(Organization.Guid, true); var usersCollection = new ObservableCollection(users); UsersView = CollectionViewSource.GetDefaultView(usersCollection); UsersView.Filter = FilterUsers; Users = usersCollection; } catch (Exception ex) { LogManager.Log(ex, $"Error loading organization users for organization '{Organization.Name}'."); await NotificationProvider.ShowError($"Error loading the selected organization users.\n{ex.FlattenMessage()}"); await NavigationManager.NavigateBack(); } finally { IsFree = true; } } else { await NotificationProvider.ShowError("No organization selected."); } } private void OnFilterChanged() { UsersView?.Refresh(); } private bool FilterUsers(object obj) { var user = obj as User; String filter = Filter ?? String.Empty; if (user != null) { return (!filter.IsNotNullOrEmpty() || user.Contact.FullName.ToLower().Contains(filter.ToLower()) || user.Email.ToLower().Contains(filter.ToLower())) && (!user.Deleted || ShowDeleted); } return true; } /// /// Called when the navigation system has navigated to this VM view. /// public async override void OnNavigatedTo() { base.OnNavigatedTo(); if (_requiresReloading) { await LoadUsers(); if (_initialUser != null) { SelectedUser = Users.SingleOrDefault(x => x.Guid == _initialUser.Guid); } } _requiresReloading = false; _initialUser = null; } /// /// Called when navigation system is going to navigate to this instance with the specified object. /// /// The object. public void OnNavigatedToWithObject(NavigationObject obj) { Organization = obj.Organization; _initialUser = obj.SelectedUser; _requiresReloading = true; } } }