using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demko6.Models;

namespace demko6;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AuthButton_OnClick(object? sender, RoutedEventArgs e)
    {
        using var context = new DemkoNejykovContext();
        
        var loginText = LoginTextBox?.Text;
        var passwordText = PasswordTextBox?.Text;
        
        if (string.IsNullOrEmpty(loginText) || string.IsNullOrEmpty(passwordText))
        {
            ErrorTextBlock.Text = "Пароль или логин не заполнен";
            ErrorTextBlock.IsVisible = true;
            return;
        }
        
        
        var employee = context.Employees.FirstOrDefault(c => c.Login == loginText);

        if (employee.Password == passwordText)
        {
            FormOrderWindow formOrderWindow = new FormOrderWindow(employee.Id);
            formOrderWindow.ShowDialog(this);
        }
        else
        {
            ErrorTextBlock.Text = "Неправильный пароль";
            ErrorTextBlock.IsVisible = true;
        }
    }
}