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;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;

namespace sport_invent_modul_14._05
{
    public partial class Form1 : Form
    {
        
        string connectionString = "Server=85.239.57.125; Database=Primakov_Natalya_modul; Uid=Primakov_Natalya; Pwd=TempPass123!; SslMode=none;";

        // Словарь соответствия ролей и форм
        private Dictionary<string, Form> roleForms = new Dictionary<string, Form>
         {
             { "1", new Admin_Form() },    // Администратор
             { "2", new ManagerForm() },   // Менеджер
             { "3", new Client_Form() }   // Клиент
         };

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                // Изменен запрос под таблицу Klients (логин, пароль, роль)
                string query = "SELECT RoleID FROM Klients WHERE Login = @login AND Password = @pass";
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@login", textBox1.Text);
                    cmd.Parameters.AddWithValue("@pass", textBox2.Text);
                    object result = cmd.ExecuteScalar();

                    if (result != null && roleForms.TryGetValue(result.ToString(), out Form form))
                    {
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("Ошибка авторизации!\nНеверный логин или пароль.", "Ошибка",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

           

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.Show();
        }
    }
}
