using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XHandler.Class;
using DataEntity.User;
using XImagingXhandler.XDAL;
using HxUserManagement.HxDAL;
using XHandler.View.MethodProperty;
using XHandler.Properties;
using HxModel;
using HxUserManagement.HxBLL;
namespace XHandler.View.User
{
///
/// EditUserInfo.xaml 的交互逻辑
///
public partial class EditUserInfo : UserControl
{
public event EventHandler closeEvent;
public event EventHandler okEvent;
private UserInfo userInfoModel = null;
private bool bAdd = false;
List roleList;
List userList;
public EditUserInfo()
{
InitializeComponent();
bAdd = true;
userInfoModel = new UserInfo();
this.DataContext = userInfoModel;
userList = UserDB.GetAllUserInfo();
title.Text = Properties.Resources.strNewUser;
roleList = UserDB.GetRoles();
comboxRole.ItemsSource = roleList;
comboxRole.SelectedIndex = 0;
comboxStatus.ItemsSource = GetStatusList();
comboxStatus.SelectedIndex = 1;
}
public EditUserInfo(string id)
{
InitializeComponent();
bAdd = false;
userInfoModel = UserDB.GetUserInfByUserId(id);//userInfoBLL.GetInfodById(id);
this.DataContext = userInfoModel;
userList = UserDB.GetAllUserInfo();//userInfoBLL.GetAllUserInfo();
title.Text = Properties.UserResource.strEditUser;
roleList = UserDB.GetRoles();
comboxRole.ItemsSource = roleList;
comboxStatus.ItemsSource = GetStatusList();
RoleTab role = roleList.Where(s => s.role_id == userInfoModel.authority).FirstOrDefault();
comboxRole.SelectedItem = role;
comboxStatus.SelectedIndex = Convert.ToInt16(userInfoModel.status);
textboxUserName.IsEnabled = false;
}
public List GetStatusList()
{
List list = new List();
string disable = XImagingXhandler.XDAL.UserStatusEnum.DisablEnum.GetDescription();
list.Add(disable);
string enable = XImagingXhandler.XDAL.UserStatusEnum.EnableEnum.GetDescription();
list.Add(enable);
return list;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(0, 500, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromSeconds(0.01));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Begin();
}
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is Grid)
{
Grid grid = e.OriginalSource as Grid;
if (grid.Name == "root")
btnClose_Click(this, null);
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
btnClose_Click(this, null);
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
if (!CheckDataValid())
return;
if (bAdd) // 添加用户
{
AddNewUser();
}
else
{
EditUser();
}
ButtonOK_Click(this, null);
}
private void AddNewUser()
{
var user = userList.Where(s => s.username == userInfoModel.username).FirstOrDefault();
if (user != null)
{
textboxUserName.BorderBrush = Brushes.Red;
usernameTips.Text = Properties.UserResource.strUserNameExist;
usernameTips.Visibility = Visibility.Visible;
return;
}
userInfoModel.id = Guid.NewGuid().ToString();
userInfoModel.userpwd = Utilities.GetMD5("123456");
userInfoModel.createname = UserManagerment.currentUser.username;
userInfoModel.createtime = DateTime.Now;
int newUser = UserDB.AddUserIntodb(userInfoModel);
if (newUser != 0)
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin(Properties.UserResource.strAddSuccess);
plsToolTipWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
plsToolTipWin.Height = 450;
plsToolTipWin.Width = 600;
plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
plsToolTipWin.ShowDialog();
}
else
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin(Properties.UserResource.strAddFailed);
plsToolTipWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
plsToolTipWin.Height = 450;
plsToolTipWin.Width = 600;
plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
plsToolTipWin.ShowDialog();
}
}
private void EditUser()
{
userInfoModel.modifyname = UserManagerment.currentUser.username;
userInfoModel.modifytime = DateTime.Now;
int newUser = UserDB.UpdateUserIntodb(userInfoModel);//userInfoBLL.Update(userInfoModel);
if (newUser != 0)
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin(Properties.UserResource.strModifySucces);
plsToolTipWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
plsToolTipWin.Height = 450;
plsToolTipWin.Width = 600;
plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
plsToolTipWin.ShowDialog();
}
else
{
PlsToolTipWin plsToolTipWin = new PlsToolTipWin(Properties.UserResource.strModifyFailed);
plsToolTipWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
plsToolTipWin.Height = 450;
plsToolTipWin.Width = 600;
plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
plsToolTipWin.ShowDialog();
}
}
private bool IsLegalPhoneNumber(string phoneNumber)
{
string phone = phoneNumber.Trim();
if (phone.Length == 11)
return Regex.IsMatch(phone, @"^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[89])[0-9]{8}$");
else
return false;
}
private bool IsLegalEmail(string email)
{
bool isValid = Regex.IsMatch(email, @"^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$");
return isValid;
}
private bool CheckDataValid()
{
if (string.IsNullOrEmpty(textboxUserName.Text.Trim()))
{
textboxUserName.BorderBrush = Brushes.Red;
usernameTips.Text = Properties.UserResource.strUserNameCannotEmpty;
usernameTips.Visibility = Visibility.Visible;
return false;
}
if (!string.IsNullOrEmpty(textboxPhone.Text.Trim()))
{
if (textboxPhone.Text.Trim().Length < 11)
{
textboxPhone.BorderBrush = Brushes.Red;
phoneTips.Text = Properties.UserResource.strPhoneLengthError;
phoneTips.Visibility = Visibility.Visible;
return false;
}
else if (!IsLegalPhoneNumber(textboxPhone.Text.Trim()))
{
textboxPhone.BorderBrush = Brushes.Red;
phoneTips.Text = Properties.UserResource.strPhoneError;
phoneTips.Visibility = Visibility.Visible;
return false;
}
}
if (!string.IsNullOrEmpty(textboxMail.Text.Trim()))
{
if (!IsLegalEmail(textboxMail.Text.Trim()))
{
textboxMail.BorderBrush = Brushes.Red;
emailTips.Text = Properties.UserResource.strEmailError;
emailTips.Visibility = Visibility.Visible;
return false;
}
}
return true;
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(500, 0, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Completed += delegate { okEvent?.Invoke(this, EventArgs.Empty); };
storyboard.Begin();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(500, 0, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Completed += delegate { closeEvent?.Invoke(this, EventArgs.Empty); };
storyboard.Begin();
}
private void comboxRole_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RoleTab role = comboxRole.SelectedItem as RoleTab;
userInfoModel.authority = role.role_id;
userInfoModel.authorityname = role.name;
}
private void textboxMail_TextChanged(object sender, TextChangedEventArgs e)
{
textboxMail.BorderBrush = Brushes.DarkGray;
emailTips.Visibility = Visibility.Collapsed;
}
private void textboxPhone_TextChanged(object sender, TextChangedEventArgs e)
{
textboxPhone.BorderBrush = Brushes.DarkGray;
phoneTips.Visibility = Visibility.Collapsed;
}
private void textboxUserName_TextChanged(object sender, TextChangedEventArgs e)
{
textboxUserName.BorderBrush = Brushes.DarkGray;
usernameTips.Visibility = Visibility.Collapsed;
}
private void comboxStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
userInfoModel.status = comboxStatus.SelectedIndex;
}
}
}