using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Commission
{
    public partial class Avtorization : Form
    {
        public Avtorization()
        {
            InitializeComponent();
            admin = false;
        }
        public static bool admin;
        public static int id_user;

        public bool logpas(string password, string login)
        {
            if (password == "" || login == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void b_lock_Click(object sender, EventArgs e)
        {
            if (t_password.UseSystemPasswordChar == true)
            {
                t_password.UseSystemPasswordChar = false;
            }
            else
            {
                t_password.UseSystemPasswordChar = true;
            }
        }

        private void Avtorization_Load(object sender, EventArgs e)
        {

        }

        private void b_enter_Click(object sender, EventArgs e)
        {
            bool check = false;
            if (t_login.Text == "" || t_password.Text == "")
            {
                MessageBox.Show("Введите логин и пароль", "Авторизация", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SqlDataReader sqlReader = null;
                SqlCommand command = new SqlCommand("SELECT * FROM Logins", DataBase.connection());
                sqlReader = command.ExecuteReader();
                while (sqlReader.Read())
                {
                    string login = Convert.ToString(sqlReader["login"]);
                    string password = Convert.ToString(sqlReader["password"]);
                    if (t_login.Text == login && t_password.Text == password)
                    {
                        check = true;
                        id_user = Convert.ToInt32(sqlReader["kod_user"]);
                        break;
                    }
                }
                sqlReader.Close();
                if (check)
                {
                    getRole();
                    Hide();
                    Menu m = new Menu();
                    m.Show();
                    m.Location = Location;

                    DataBase.diagWrite("Вошел пользователь с логином " + t_login.Text + " и паролем " + t_password.Text);
                }
                else
                {
                    MessageBox.Show("Неверные данные для входа", "Авторизация", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    DataBase.diagWrite("Неудачная попытка входа с логином " + t_login.Text + " и паролем " + t_password.Text);
                }
            }
        }
        private void getRole()
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand("SELECT Name_dolgnosti FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg WHERE Employees.kod_user = @kod", DataBase.connection());
            command.Parameters.AddWithValue("kod", id_user);
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (Convert.ToString(sqlReader["Name_dolgnosti"]) == "Администратор")
                {
                    admin = true;
                    break;
                }
            }
            sqlReader.Close();
        }

        private void Avtorization_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
