@ -1,8 +1,8 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Collections.ObjectModel ;
using System.Linq ;
using System.Linq ;
using System.Windows ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Input ;
using System.Windows.Input ;
namespace eCompanies
namespace eCompanies
@ -13,53 +13,80 @@ namespace eCompanies
public partial class UsersWindow : Window
public partial class UsersWindow : Window
{
{
Company currentCompany ;
Company currentCompany ;
UpdateCompanyGridDelegate updateCompanyGrid ;
UpdateCompanyGridDelegate updateCompanyItem ;
List < int > deletingUsersList = new List < int > ( ) ;
public UsersWindow ( Company c , UpdateCompanyGridDelegate d )
public UsersWindow ( Company c , UpdateCompanyGridDelegate d )
{
{
InitializeComponent ( ) ;
InitializeComponent ( ) ;
currentCompany = c ;
currentCompany = c ;
updateCompanyGrid = d ;
updateCompanyItem = d ;
updateUsersGrid ( ) ;
updateUsersGrid ( c ) ;
}
}
private void updateUsersGrid ( )
private void updateUsersGrid ( Company c )
{
{
currentCompany . Users = App . getUsers ( currentCompany . CompanyId ) ;
companyNameBox . Text = c . Name ;
companyNameBox . Text = currentCompany . Name ;
contractStatusComboBox . SelectedIndex = c . ContractStatus ;
contractStatusComboBox . SelectedIndex = currentCompany . ContractStatus ;
currentCompany . Users = App . getUsers ( c . CompanyId ) ;
usersGrid . DataContext = new ObservableCollection < Users > ( currentCompany . Users . AsEnumerable ( ) ) ;
usersGrid . DataContext = new ObservableCollection < Users > ( c . Users . AsEnumerable ( ) ) ;
}
}
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e )
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e )
{
{
MessageBoxResult mbr = MessageBox . Show ( "Вы закрываете окно с компанией. Хотите сохранить результаты?" , "Сохранение изменений" , MessageBoxButton . YesNo ) ;
MessageBoxResult mbr = MessageBox . Show ( "Вы закрываете окно с компанией. Хотите сохранить результаты?" , "Сохранение изменений" , MessageBoxButton . YesNo ) ;
if ( mbr = = MessageBoxResult . No )
if ( mbr = = MessageBoxResult . No )
return ;
return ;
currentCompany . Users . Clear ( ) ;
foreach ( Users u in ( ObservableCollection < Users > ) usersGrid . DataContext )
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 . CompanyId = currentCompany . CompanyId ;
u . Company = currentCompany ;
u . Company = currentCompany ;
currentCompany . Users . Add ( u ) ;
currentCompany . Users . Add ( u ) ;
}
}
currentCompany . ContractStatus = contractStatusComboBox . SelectedIndex ;
currentCompany . ContractStatus = contractStatusComboBox . SelectedIndex ;
currentCompany . Name = companyNameBox . Text ;
currentCompany . Name = companyNameBox . Text ;
App . updateCompany ( currentCompany ) ;
updateCompanyItem ( currentCompany ) ;
updateCompanyGrid ( ) ;
}
}
private void usersGrid_RowEditEnding ( object sender , DataGridRowEditEndingEventArgs e )
private void deleteUserFromGrid ( )
{
{
}
var users = ( ObservableCollection < Users > ) usersGrid . DataContext ;
foreach ( Users u in users )
private void usersGrid_AddingNewItem ( object sender , AddingNewItemEventArgs e )
{
{
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 )
private void RowDelete_Click ( object sender , RoutedEventArgs e )
{
{
App . removeUser ( ( ( Users ) usersGrid . CurrentItem ) . UserId ) ;
deleteUserFromGrid ( ) ;
updateUsersGrid ( ) ;
}
}
}
}
}
}