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 System.Data.SqlClient;

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

            cb_filtr.Items.Add("Фамилия");
            cb_filtr.Items.Add("Должность");
            cb_filtr.DropDownStyle = ComboBoxStyle.DropDownList;

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_employee AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество',  Phone AS 'Телефон', Birthday AS 'Дата рождения', Name_dolgnosti AS 'Должность' FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];
        }

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

        private void b_add_Click(object sender, EventArgs e)
        {
            Hide();
            Employees_add add = new Employees_add();
            add.Show();
            add.Location = Location;
            DataBase.diagWrite("Переход на форму Добавление сотрудника");
        }

        private void b_poisk_Click(object sender, EventArgs e)
        {
            SqlCommand command;
            SqlDataAdapter dataAdapter;
            if (cb_filtr.Text == "Фамилия")
            {
                command = new SqlCommand("SELECT Kod_employee AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Phone AS 'Телефон', Birthday AS 'Дата рождения', Name_dolgnosti AS 'Должность' FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg WHERE Surname LIKE @value", DataBase.connection());
                command.Parameters.AddWithValue("value", "%" + t_filtr.Text + "%");
                dataAdapter = new SqlDataAdapter(command);
            }
            else if (cb_filtr.Text == "Должность")
            {
                command = new SqlCommand("SELECT Kod_employee AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Phone AS 'Телефон', Birthday AS 'Дата рождения', Name_dolgnosti AS 'Должность' FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg WHERE Name_dolgnosti LIKE @value", DataBase.connection());
                command.Parameters.AddWithValue("value", "%" + t_filtr.Text + "%");
                dataAdapter = new SqlDataAdapter(command);
            }
            else
            {
                dataAdapter = new SqlDataAdapter("SELECT Kod_employee AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Phone AS 'Телефон', Birthday AS 'Дата рождения', Name_dolgnosti AS 'Должность' FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg", DataBase.connection());
            }
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];
        }

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

        private void b_delete_Click(object sender, EventArgs e)
        {
            int kod = 0;
            SqlDataReader sqlReader = null;
            SqlCommand command = new SqlCommand("SELECT Kod_user FROM Employees WHERE Kod_employee = @kod", DataBase.connection());
            command.Parameters.AddWithValue("kod", dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value);
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                kod = Convert.ToInt32(sqlReader["Kod_user"]);
            }

            command = new SqlCommand("DELETE FROM Employees WHERE Kod_employee = @kod", DataBase.connection());
            command.Parameters.AddWithValue("kod", dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value);
            command.ExecuteNonQuery();

            command = new SqlCommand("DELETE FROM Logins WHERE kod_user = @kod", DataBase.connection());
            command.Parameters.AddWithValue("kod", kod);
            command.ExecuteNonQuery();

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_employee AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Phone AS 'Телефон', Birthday AS 'Дата рождения', Name_dolgnosti AS 'Должность' FROM Employees INNER JOIN Dolgnosti ON Dolgnosti.kod_dolg = Employees.kod_dolg", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
        }
    }
}

