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>
    /// ChangeUserWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChangeUserWindow : Window
    {
        public UserMesDataModelClass UserMessage = null;
        public ChangeUserWindow()
        {
            InitializeComponent();
        }

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

        private void UserChangeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (UserMessage == null) return;
            if (UserNameTbx.Text == "" || UserOldPassTbx.Password == "" || UserNewPassTbx.Password == "" 
                || UserReNewPassTbx.Password == "" || UserLevelCbx.SelectedItem == null)
            {
                MessageBox.Show("请将信息填写完整");
                return;
            }
            string OldPass = MessageBus.UserMessageClass.HashPassword(UserOldPassTbx.Password);
            if (OldPass != UserMessage.PassWord)
            {
                MessageBox.Show("旧密码输入错误");
                return;
            }
            if (UserReNewPassTbx.Password != UserNewPassTbx.Password)
            {
                MessageBox.Show("新密码输入不一致");
                return;
            }
            if(ShowMessageBus.ShowBinding.UserMesDataModels.Where(x => x.UserName == UserNameTbx.Text).Count() > 0)
            {
                MessageBox.Show("用户名已存在");
                return;
            }
            UserMesDataModelClass newUserMessage = new UserMesDataModelClass()
            {
                Id = UserMessage.Id,
                UserName = UserNameTbx.Text,
                PassWord = UserNewPassTbx.Password,
                IsDelete = UserMessage.IsDelete,
                IsAdmin = UserLevelCbx.SelectedItem.ToString() == "管理员" ? true : false,
                UserMes = UserLevelCbx.SelectedItem.ToString()
            };
            MessageBus.UserMessageClass.ChangeUserMessage(UserMessage.Id, newUserMessage);
            ShowMessageBus.ShowBinding.UserMesDataModels = MessageBus.UserMessageClass.GetAllUserMessage();
            MessageBox.Show("修改成功");
            this.Close();
        }

        private void UserResetPassBtn_Click(object sender, RoutedEventArgs e)
        {
            if(UserMessage == null) return;
            var confirmResult = MessageBox.Show($"继续执行此操作将会重置用户{UserMessage.UserName}的密码至默认的\"123456\"，是否继续执行",
                                      "执行确认", MessageBoxButton.YesNo);
            if (confirmResult == MessageBoxResult.No)
            {
                return;
            }
            MessageBus.UserMessageClass.ResetPassWord(UserMessage.Id);
            ShowMessageBus.ShowBinding.UserMesDataModels = MessageBus.UserMessageClass.GetAllUserMessage();
            MessageBox.Show("重置成功");
            UserMessage = ShowMessageBus.ShowBinding.UserMesDataModels.Where(x => x.Id == UserMessage.Id).FirstOrDefault();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UserLevelCbx.Items.Clear();
            if (UserMessage.IsAdmin && UserMessage.Id == 1)
            {
                UserLevelCbx.Items.Add("管理员");
                UserLevelCbx.SelectedIndex = 0;
                UserNameTbx.Text = UserMessage.UserName;
                UserNameTbx.IsReadOnly = true;
            }
            else
            {
                UserLevelCbx.Items.Add("管理员");
                UserLevelCbx.Items.Add("普通用户");
                UserLevelCbx.SelectedItem = UserMessage.IsAdmin ? "管理员" : "普通用户";
            }
            UserIDTbx.Text = UserMessage.Id.ToString();
        }
    }
}
