using MWS.Data.Manager;
using MWS.Library.Common.MD5Poxy;
using MWS.Model;
using MWSDomain.Common.CoreLib;
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 MWS.Domain.View.SystemManage
{
    /// <summary>
    /// UserAddWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserAddWindow : Window
    {
        private UserManager userManager;

        private User t_User;
        public UserAddWindow()
        {
            InitializeComponent();
            userManager = new UserManager();
            t_User = new User();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            BindLockTypeComboBox();
        }
        /// <summary>
        /// 绑定锁定类型
        /// </summary>
        private void BindLockTypeComboBox()
        {
            try
            {                
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                      new KeyValuePair<string, string>("0", "否"),
                    new KeyValuePair<string, string>("1", "是")
                 };
                cmLock.ItemsSource = list;
                cmLock.SelectedValuePath = "Key";
                cmLock.DisplayMemberPath = "Value";
                cmLock.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

            }
        }

        private void tbsave_Click(object sender, RoutedEventArgs e)
        {
            t_User.Account = tbUserAccount.Text.Trim();
            t_User.UserName = tbRealName.Text.Trim();
            t_User.Password = tbPassword.Text.Trim();
            t_User.IsLocked = Convert.ToBoolean(Convert.ToInt16(cmLock.SelectedValue));
            t_User.Role = 1;
            if (string.IsNullOrEmpty(t_User.Account))
            {
                Msg.Error("用户名不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(tbRealName.Text.Trim()))
            {
                Msg.Error("真实姓名不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(tbPassword.Text.Trim()))
            {
                Msg.Error("密码不能为空！");
                return;
            }
            t_User.Password = CEncoder.Encode(t_User.Password);
            t_User.Time = DateTime.Now;
            if (userManager.Insert(t_User)>0)
            {
                Msg.Info("添加成功！");
                this.Close();
            }
            else
            {
                Msg.Info("添加失败！");
            }
            //获取redis中的用户账号
            //if (operateResult.Content != null)
            //{

            //    t_User.Account = userObject.GetValue("Account").ToSafeString();
            //    t_User.UserName = userObject.GetValue("UserName").ToSafeString();
            //    t_User.Password = userObject.GetValue("Password").ToSafeString();

            //}
            //var loginTask = await user.Login(t_User.Account, CEncoder.Encode(oldpassword));
        }

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