You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

96 lines
3.6 KiB

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace eCompanies
{
/// <summary>
/// Логика взаимодействия для UsersWindow.xaml
/// </summary>
public partial class UsersWindow : Window
{
Company currentCompany;
UpdateCompanyGridDelegate updateCompanyItem;
List<int> deletingUsersList = new List<int>();
public UsersWindow(Company c, UpdateCompanyGridDelegate d)
{
InitializeComponent();
currentCompany = c;
updateCompanyItem = d;
updateUsersGrid(c);
}
private void updateUsersGrid(Company c)
{
companyNameBox.Text = c.Name;
contractStatusComboBox.SelectedIndex = c.ContractStatus;
currentCompany.Users = App.getUsers(c.CompanyId);
usersGrid.DataContext = new ObservableCollection<Users>(c.Users.AsEnumerable());
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mbr = MessageBox.Show("Вы закрываете окно компании. Хотите сохранить результаты?", "Сохранение изменений", MessageBoxButton.YesNoCancel);
switch (mbr)
{
case MessageBoxResult.No: return;
case MessageBoxResult.Cancel: e.Cancel = true;
break;
}
currentCompany.Users.Clear();
foreach (Users u in (ObservableCollection<Users>)usersGrid.DataContext)
{
if (u.Login == null || u.Password == null || u.Name == null)
{
mbr = MessageBox.Show("Вы не полностью заполнили поле одного из пользователей! " +
"Вернуться к форме для исправления? (При ответе Нет все данные будут потеряны!)",
"Поле Логин/Пароль не может быть пустым!",
MessageBoxButton.YesNo);
if (mbr == MessageBoxResult.Yes)
{
e.Cancel = true;
return;
} else if (mbr == MessageBoxResult.No)
return;
}
if (deletingUsersList != null)
App.removeUser(deletingUsersList);
u.CompanyId = currentCompany.CompanyId;
u.Company = currentCompany;
currentCompany.Users.Add(u);
}
currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex;
currentCompany.Name = companyNameBox.Text;
updateCompanyItem(currentCompany);
}
private void deleteUserFromGrid()
{
var users = (ObservableCollection<Users>)usersGrid.DataContext;
foreach (Users u in users)
{
int deletingUser = ((Users)usersGrid.CurrentItem).UserId;
if (Equals(u.UserId, deletingUser))
{
deletingUsersList.Add(((Users)usersGrid.CurrentItem).UserId);
users.Remove(u);
usersGrid.DataContext = users;
return;
}
}
}
private void RowDelete_Click(object sender, RoutedEventArgs e)
{
deleteUserFromGrid();
}
}
}