using HIA_beadando.Database;
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace HIA_beadando
{
    public partial class Login : Form
    {
        DataTable dataTable = new DataTable();
        SQLiteDataAdapter dataAdapter;
        SQLiteCommand command = new SQLiteCommand();

        public Login()
        {
            InitializeComponent();
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            if (UsernameTextBox.Text != "" && PasswordTextBox.Text != "")
            {
                DatabaseOperator dbo = new DatabaseOperator();

                dbo.openConnection();

                string sql_count = "select count(felhasznaloID), felhasznaloID from felhasznalok where felhasznalonev='"
                                   + UsernameTextBox.Text + "' and jelszo='" + PasswordTextBox.Text + "'";

                command = new SQLiteCommand(sql_count, dbo.GetConnection());

                dataAdapter = new SQLiteDataAdapter(command);

                dataTable.Clear();

                dataAdapter.Fill(dataTable);

                // Azaz van ilyen felhasznaló
                if (dataTable.Rows[0][0].ToString() == "1")
                {
                    Vendeg vendeg = new Vendeg(int.Parse(dataTable.Rows[0][1].ToString()));

                    vendeg.Show();

                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Hibás felhasznalónév vagy jelszó.");
                }



                dbo.closeConnection();
            }
        }

        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }
    }
}
