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 Commission
{
    public partial class Specialization : Form
    {
        public Specialization()
        {
            InitializeComponent();

            cb_filtr.Items.Add("Специальность");
            //cb_filtr.Items.Add("Факультет");
            cb_filtr.DropDownStyle = ComboBoxStyle.DropDownList;
            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_specialties AS 'Код', Kod_faculty AS 'Факультет', Specialty AS 'Специальность', Admission_plan AS 'План приема', Passing_score AS 'Проходной балл' FROM Specialization", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView2.DataSource = dataset.Tables[0];
        }

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

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

        private void b_add_Click(object sender, EventArgs e)
        {
            Hide();
            Spec_add add = new Spec_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_specialties AS 'Код', Kod_faculty AS 'Факультет', Specialty AS 'Специальность', Admission_plan AS 'План приема', Passing_score AS 'Проходной балл' FROM Specialization WHERE  Specialization 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_specialties AS 'Код', Kod_faculty AS 'Факультет', Specialty AS 'Специальность', Admission_plan AS 'План приема', Passing_score AS 'Проходной балл' FROM Specialization INNER JOIN Specialization.Kod_faculty = Faculty.Kod_faculty WHERE Faculty LIKE @value", DataBase.connection());
            //    command.Parameters.AddWithValue("value", "%" + t_filtr.Text + "%");
            //    dataAdapter = new SqlDataAdapter(command);
            //}
            else
            {
                dataAdapter = new SqlDataAdapter("SELECT Kod_specialties AS 'Код', Kod_faculty AS 'Факультет', Specialty AS 'Специальность', Admission_plan AS 'План приема', Passing_score AS 'Проходной балл' FROM Specialization", DataBase.connection());
            }
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView2.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_specialties FROM Specialization WHERE Kod_specialties = @kod", DataBase.connection());
            command.Parameters.AddWithValue("kod", dataGridView2[0, dataGridView2.CurrentCell.RowIndex].Value);
            sqlReader = command.ExecuteReader();
            while (sqlReader.Read())
            {
                kod = Convert.ToInt32(sqlReader["Kod_specialties"]);
            }

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

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT Kod_specialties AS 'Код', Kod_faculty AS 'Факультет', Specialty AS 'Специальность', Admission_plan AS 'План приема', Passing_score AS 'Проходной балл' FROM Specialization", DataBase.connection());
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            dataGridView2.DataSource = dataset.Tables[0];
            foreach (DataGridViewColumn column in dataGridView2.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
        }
    }
}
