using Npgsql;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestsManipulate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Login(object sender, EventArgs e)
        {
            var conn = new NpgsqlConnection(Program.connString);
            conn.Close();
            try
            {
                conn.Open();
            }
            catch
            {
                MessageBox.Show("Ошибка", "Ошибка");
                return;
            }

            using (var cmd = new NpgsqlCommand("SELECT login, password, role_id, id FROM users WHERE login = @login", conn))
            {
                cmd.Parameters.AddWithValue("login", textBox1.Text);

                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        Program.login = textBox1.Text;
                        string dbPassword = reader.GetString(1);
                        Program.role_id = reader.GetInt16(2);
                        Program.user_id = reader.GetInt32(3);

                        if (dbPassword == textBox2.Text)
                        {
                            MainPage mainPage = new MainPage();
                            this.Hide();
                            mainPage.Show();
                        }
                        else
                        {
                            MessageBox.Show("Неверный пароль!", "Ошибка");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Пользователь не найден!", "Ошибка");
                    }
                }
            }

        }
    }
}
