using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            string login = tbLogin.Text.Trim();
            string password = tbPass.Text;
            if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("Пожалуйста, введите логин и пароль!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string connectionString = "Server=cfif31.ru; port=3306; Database=ISPr25-21_NeshinaPV_Doctor; username=ISPr25-21_NeshinaPV; password=ISPr25-21_NeshinaPV";
            string query = "SELECT idUsers, id_post FROM Users WHERE Login = @Login AND Password = @Password";
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    using (MySqlCommand cmd = new MySqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@Login", login);
                        cmd.Parameters.AddWithValue("@Password", password);
                        using (MySqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                int currentUserId = reader.GetInt32("idUsers");
                                int userRole = reader.GetInt32("id_post");
                                Form nextForm = null;
                                if (userRole == 1)
                                {
                                    nextForm = new Doctor(currentUserId);
                                }
                                else if (userRole == 2)
                                {
                                    nextForm = new Administrator(currentUserId);
                                }                             
                                else
                                {
                                    MessageBox.Show("Ваша роль не имеет доступа к системе.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                                if (nextForm != null)
                                {
                                    nextForm.Show();
                                    this.Hide();
                                    tbLogin.Clear();
                                    tbPass.Clear();
                                }
                            }
                            else
                            {
                                MessageBox.Show("Неверный логин или пароль!", "Ошибка входа", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                tbPass.Clear();
                                tbPass.Focus();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка подключения к базе данных:\n" + ex.Message, "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbLogin_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void tbPass_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
