using MWS.Data.Manager;
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>
    /// UserEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserEditWindow : Window
    {
        private UserManager userManager;
        private User t_User;
        public UserEditWindow(User t_User)
        {
            InitializeComponent();
            userManager = new UserManager();
            this.t_User = t_User;
            BindLockTypeComboBox();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tbUserAccount.Text = t_User.Account;
            tbRealName.Text = t_User.UserName;
            cmLock.SelectedValue = t_User.IsLocked?"1":"0";
        }
        private void tbsave_Click(object sender, RoutedEventArgs e)
        {
            t_User.Account = tbUserAccount.Text.Trim();
            t_User.UserName = tbRealName.Text.Trim();
            t_User.IsLocked = Convert.ToBoolean(Convert.ToInt16(cmLock.SelectedValue));
            if (string.IsNullOrEmpty(t_User.Account))
            {
                Msg.Error("用户名不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(tbRealName.Text.Trim()))
            {
                Msg.Error("真实姓名不能为空！");
                return;
            }
           
            if (userManager.Update(t_User))
            {
                Msg.Info("修改成功！");
                this.Close();
            }
            else
            {
                Msg.Info("修改失败！");
            }
        }
        /// <summary>
        /// 绑定锁定类型
        /// </summary>
        private void BindLockTypeComboBox()
        {

            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;
        }

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