using CCDCount.MODEL.SqlDataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Shapes;

namespace CCDCountWpf.WpfFrom
{
    /// <summary>
    /// AddUserWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddUserWindow : Window
    {
        public AddUserWindow()
        {
            InitializeComponent();
        }

        private void UserCancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void AddUserBtn_Click(object sender, RoutedEventArgs e)
        {
            if (UserNameTbx.Text == "" || UserPassTbx.Password == "" || UserRePassTbx.Password == "" || UserLevelCbx.SelectedItem == null)
            {
                MessageBox.Show("请将信息填写完整");
                return;
            }
            if (ShowMessageBus.ShowBinding.UserMesDataModels.Where(x => x.UserName == UserNameTbx.Text).Count() > 0)
            {
                MessageBox.Show("用户名已存在");
                return;
            }
            if (UserPassTbx.Password == UserRePassTbx.Password)
            {
                MessageBus.UserMessageClass.InsertUserMessage(new UserMesDataModelClass()
                {
                    UserName = UserNameTbx.Text,
                    PassWord = UserPassTbx.Password,
                    UserMes = UserLevelCbx.SelectedItem.ToString(),
                    DateTime = DateTime.Now,
                    IsDelete = false,
                    IsAdmin = UserLevelCbx.SelectedItem.ToString() == "管理员" ? true : false
                });
            }
            else
            {
                MessageBox.Show("密码不一致");
                return;
            }
            MessageBox.Show("添加成功");
            ShowMessageBus.ShowBinding.UserMesDataModels = MessageBus.UserMessageClass.GetAllUserMessage();
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UserLevelCbx.Items.Add("管理员");
            UserLevelCbx.Items.Add("普通用户");
            UserLevelCbx.SelectedIndex = 1;
        }
    }
}
