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;
            }
            if (MessageBus.UserMessageClass.HashPassword(UserOldPassTbx.Password) != UserMessage.PassWord)
            {
                MessageBox.Show("旧密码输入错误");
                return;
            }
            if (UserOldPassTbx.Password != UserNewPassTbx.Password)
            {
                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);
            MessageBus.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);
            MessageBus.UserMessageClass.ChangePassWord(UserMessage.Id, UserOldPassTbx.Password, UserNewPassTbx.Password);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UserLevelCbx.Items.Add("管理员");
            UserLevelCbx.Items.Add("普通用户");
            UserLevelCbx.SelectedItem = UserMessage.IsAdmin ? "管理员" : "普通用户";
            UserIDTbx.Text = UserMessage.Id.ToString();
        }
    }
}
