using SlnMesnac.WPF.Attribute;
using SlnMesnac.WPF.ViewModel.Login;
using System;
using System.Windows;

namespace SlnMesnac.WPF.Page.Login
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    [RegisterAsSingletonAttribute]
    public partial class LoginWindow : Window
    {
        private readonly LoginViewModel _loginViewModel;
        public LoginWindow(LoginViewModel loginViewModel)
        {
            _loginViewModel = loginViewModel;
            InitializeComponent();
            this.DataContext = _loginViewModel;
        }

        private async void LoginBtn_Click(object sender, RoutedEventArgs e)
        {
            string userName = UserNameStr.Text.ToString();
            string password = PasswordStr.Password;

            //if (string.IsNullOrEmpty(userName))
            //{
            //    MessageBox.Show("用户名不允许为空");
            //    return;
            //}

            //if (string.IsNullOrEmpty(password))
            //{
            //    MessageBox.Show("密码不允许为空");
            //    return;
            //}

            bool res = _loginViewModel.Login(userName,password);
            if (res)
            {
                this.Closing += MainWindow_Closing;
                this.Close();
            }
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Closing -= MainWindow_Closing; // 防止多次绑定
            e.Cancel = true;
            this.Visibility = Visibility.Hidden; // 隐藏窗口
        }
    }
}
