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 Hotel_Course_Project
{
    /// <summary>
    /// Логика взаимодействия для ChangePasLogWindow.xaml
    /// </summary>
    public partial class ChangePasLogWindow : Window
    {
        Staff _staff;
        public ChangePasLogWindow(Staff staff)
        {
            _staff = staff;
            InitializeComponent();
        }

        private void BackBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            string pass = SomeMethods.Hash(TBPassword.Text);
            var user = DataBase.db.Staff.SingleOrDefault(x => x.Login == TBLogin.Text && x.Password == pass && x.Login != _staff.Login);
            if (TBLogin.Text == "" || TBPassword.Text == "")
            {
                MessageBox.Show("Пустые поля");
            }
            else if (user != null)
            {
                MessageBox.Show("Такой логин уже существует");
            }
            else
            {
                var _user = DataBase.db.Staff.SingleOrDefault(x => x.Login == _staff.Login);
                _user.Login = TBLogin.Text;
                _user.Password = SomeMethods.Hash(TBPassword.Text);
                DataBase.db.SaveChanges();
                this.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TBLogin.Text = _staff.Login;
            TBPassword.Text = SomeMethods.pass;
        }
    }
}
