@ -1,8 +1,8 @@
using System ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Linq ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Input ;
namespace eCompanies
@ -13,53 +13,80 @@ namespace eCompanies
public partial class UsersWindow : Window
{
Company currentCompany ;
UpdateCompanyGridDelegate updateCompanyGrid ;
UpdateCompanyGridDelegate updateCompanyItem ;
List < int > deletingUsersList = new List < int > ( ) ;
public UsersWindow ( Company c , UpdateCompanyGridDelegate d )
{
InitializeComponent ( ) ;
currentCompany = c ;
updateCompanyGrid = d ;
updateUsersGrid ( ) ;
updateCompanyItem = d ;
updateUsersGrid ( c ) ;
}
private void updateUsersGrid ( )
private void updateUsersGrid ( Company c )
{
currentCompany . Users = App . getUsers ( currentCompany . CompanyId ) ;
companyNameBox . Text = currentCompany . Name ;
contractStatusComboBox . SelectedIndex = currentCompany . ContractStatus ;
usersGrid . DataContext = new ObservableCollection < Users > ( currentCompany . Users . AsEnumerable ( ) ) ;
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 . YesNo ) ;
if ( mbr = = MessageBoxResult . No )
return ;
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 ;
App . updateCompany ( currentCompany ) ;
updateCompanyGrid ( ) ;
updateCompanyItem ( currentCompany ) ;
}
private void usersGrid_RowEditEnding ( object sender , DataGridRowEditEndingEventArgs e )
private void deleteUserFromGrid ( )
{
}
private void usersGrid_AddingNewItem ( object sender , AddingNewItemEventArgs e )
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 )
{
App . removeUser ( ( ( Users ) usersGrid . CurrentItem ) . UserId ) ;
updateUsersGrid ( ) ;
deleteUserFromGrid ( ) ;
}
}
}