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

namespace Commission
{
    public partial class Cabinet : Form
    {
        public Cabinet()
        {
            InitializeComponent();
        }

        public static int id;

        private void Cabinet_Load(object sender, EventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT *, Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg = Employees.Kod_dolg", DataBase.connection());
            command.Parameters.AddWithValue("id", Avtorization.id_user);
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                t_surname.Text = Convert.ToString(sqlReader["Surname"]);
                t_name.Text = Convert.ToString(sqlReader["Names"]);
                t_patr.Text = Convert.ToString(sqlReader["Patronymic"]);
                date.Value = Convert.ToDateTime(sqlReader["Birthday"]);
                t_phone.Text = Convert.ToString(sqlReader["Phone"]);
                t_dolgnost.Text = Convert.ToString(sqlReader["Name_dolgnosti"]);
            }
            sqlReader.Close();
        }
        private void b_save_Click(object sender, EventArgs e)
        {
            bool check = false;
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT *  FROM Employees WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (t_surname.Text != Convert.ToString(sqlReader["Surname"]))
                {
                    command = new SqlCommand($"UPDATE Employees SET Surname='{t_surname.Text}' WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
                    sqlReader = command.ExecuteReader();
                    b_save.Enabled = false;
                    check = true;
                }
                if (t_name.Text != Convert.ToString(sqlReader["Names"]))
                {
                    command = new SqlCommand($"UPDATE Employees SET Names='{t_name.Text}' WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
                    sqlReader = command.ExecuteReader();
                    b_save.Enabled = false;
                    check = true;
                }
                if (t_patr.Text != Convert.ToString(sqlReader["Patronymic"]))
                {
                    command = new SqlCommand($"UPDATE Employees SET Patronymic='{t_patr.Text}' WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
                    sqlReader = command.ExecuteReader();
                    b_save.Enabled = false;
                    check = true;
                }
                if (date.Value != Convert.ToDateTime(sqlReader["Birthday"]))
                {
                    command = new SqlCommand($"UPDATE Employees SET Birthday='{date.Value}' WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
                    sqlReader = command.ExecuteReader();
                    b_save.Enabled = false;
                    check = true;
                }
                if (t_phone.Text != Convert.ToString(sqlReader["Phone"]))
                {
                    command = new SqlCommand($"UPDATE Employees SET Phone='{t_phone.Text}' WHERE Employees.Kod_user = {Avtorization.id_user}", DataBase.connection());
                    sqlReader = command.ExecuteReader();
                    b_save.Enabled = false;
                    check = true;
                }
                if (check)
                {
                    MessageBox.Show("Данные успешно изменены!", "Изменение данных", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void b_back_Click(object sender, EventArgs e)
        {
            Hide();
            Menu menu = new Menu();
            menu.Show();
            menu.Location = Location;
            DataBase.diagWrite("Переход на форму Главное меню");
        }

        private void date_ValueChanged(object sender, EventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT * , Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg=Employees.Kod_dolg", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (date.Value != Convert.ToDateTime(sqlReader["Birthday"]))
                {
                    b_save.Enabled = true;
                }
                else
                {
                    b_save.Enabled = false;
                }
            }
        }

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

        private void t_surname_TextChanged(object sender, EventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT * , Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg=Employees.Kod_dolg", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (t_surname.Text != Convert.ToString(sqlReader["Surname"]))
                {
                    b_save.Enabled = true;
                }
                else
                {
                    b_save.Enabled = false;
                }
            }
        }

        private void t_name_TextChanged(object sender, EventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT * , Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg=Employees.Kod_dolg", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (t_name.Text != Convert.ToString(sqlReader["Names"]))
                {
                    b_save.Enabled = true;
                }
                else
                {
                    b_save.Enabled = false;
                }
            }
        }

        private void t_patr_TextChanged(object sender, EventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT * , Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg=Employees.Kod_dolg", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (t_patr.Text != Convert.ToString(sqlReader["Patronymic"]))
                {
                    b_save.Enabled = true;
                }
                else
                {
                    b_save.Enabled = false;
                }
            }
        }

        private void t_phone_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand($"SELECT * , Dolgnosti.Name_dolgnosti FROM Employees, Dolgnosti WHERE Employees.Kod_user = {Avtorization.id_user} and Dolgnosti.Kod_dolg=Employees.Kod_dolg", DataBase.connection());
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                if (t_phone.Text != Convert.ToString(sqlReader["Phone"]))
                {
                    b_save.Enabled = true;
                }
                else
                {
                    b_save.Enabled = false;
                }
            }
        }

        private void change_Click(object sender, EventArgs e)
        {
            Hide();
            Change change = new Change();
            change.Show();
            change.Location = Location;
            DataBase.diagWrite("Переход на форму Изменение пароля");
        }
    }
}
