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.Navigation;
using System.Windows.Shapes;

namespace ViolationNOT
{
    /// <summary>
    /// Interaction logic for AuthPage.xaml
    /// </summary>
    public partial class AuthPage : Page
    {
        public AuthPage()
        {
            InitializeComponent();
        }

        private void BtnAuth_Click(object sender, RoutedEventArgs e)
        {
            Authorization(TxtLogin.Text, TxtPassword.Password);
        }

        public bool Authorization(string Login, string Password)
        {
            if (string.IsNullOrEmpty(Login) || string.IsNullOrEmpty(Password))
            {
                Helper.ErroreMessage("Поля не заполнены");
                return false;
            }
            User AuthUser = DB.db.User.FirstOrDefault(x => x.Login == Login);
            if (AuthUser == null)
            {
                Helper.ErroreMessage("Пользователь не найден");
                return false;
            }
            if (AuthUser.Password != Password)
            {
                Helper.ErroreMessage("Неверный пароль");
                return false;
            }
            Helper.AuthUser = AuthUser;
            if (AuthUser.IDRole ==  1)
            {
                ManagerFrame.MainFrame.Navigate(new UserPage());
            }
            if (AuthUser.IDRole == 2)
            {
                ManagerFrame.MainFrame.Navigate(new AdminPage());
            }

            return true;
        }

        private void BtnRegistration_Click(object sender, RoutedEventArgs e)
        {
            ManagerFrame.MainFrame.Navigate(new RegistrationPage());
        }
    }
}
