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

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

            cb_filtr.Items.Add("Фамилия");
            cb_filtr.Items.Add("Дата рождения");
            cb_filtr.Items.Add("Результаты");
            cb_filtr.DropDownStyle = ComboBoxStyle.DropDownList;

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
        }

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

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

        private void b_entrant_add_Click(object sender, EventArgs e)
        {
            Hide();
            Entrants_add add = new Entrants_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_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant 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_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant WHERE Birthday 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_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant WHERE Result_ball Like @value", DataBase.connection());
                command.Parameters.AddWithValue("value", "%" + t_filtr.Text + "%");
                dataAdapter = new SqlDataAdapter(command);
            }
            else
            {
                dataAdapter = new SqlDataAdapter("SELECT Kod_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant", DataBase.connection());
            }
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];

            
        }

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

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

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_entrant AS 'Код', Surname AS 'Фамилия', Names AS 'Имя', Patronymic AS 'Отчество', Birthday AS 'Дата рождения', Phone AS 'Телефон',  Address AS 'Адрес', Medal AS 'Медаль', Result_ball AS 'Сумма результатов', Kod_specialties AS 'Специальность', Admission AS 'Зачисление' FROM Entrant", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView1.DataSource = dataset.Tables[0];
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
        }
    }
}
