using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KursachMukha692gr
{
    /// <summary>
    /// Логика взаимодействия для Tournments.xaml
    /// </summary>
    public partial class Tournments : Window
    {
        public Tournments()
        {
            InitializeComponent();
            SqlConnection connection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
            connection.Open();
            string cmd = "select id,name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', fk_disciplina_id as 'id дисциплины',fk_gruppa_id as 'Номер группы, принимающей участие', fk_infotrener_id as 'id тренера' from sorevnovaniya";
            SqlCommand createcommand = new SqlCommand(cmd, connection);
            createcommand.ExecuteNonQuery();
            SqlDataAdapter sql = new SqlDataAdapter(createcommand);
            DataTable dt = new DataTable("sorevi");
            sql.Fill(dt);
            sorevigrid.ItemsSource = dt.DefaultView;
            connection.Close();
        }

        private void Button_Click_add(object sender, RoutedEventArgs e)
        {
            AddTournment tournment = new AddTournment();
            tournment.Show();
            Close();
        }

        private void Button_Click_delete(object sender, RoutedEventArgs e)
        {
            DeleteTournment tournment = new DeleteTournment();
            tournment.Show();
            Close();
        }

        private void Button_Click_Back(object sender, RoutedEventArgs e)
        {
            KabinetAdmina kabinet = new KabinetAdmina();
            kabinet.Show();
            Close();
        }

        private void Button_Click_update(object sender, RoutedEventArgs e)
        {
            ViborUpdateTournments tournments = new ViborUpdateTournments();
            tournments.Show();
            Close();
        }

        private void Button_Click_Search(object sender, RoutedEventArgs e)
        {
            /*ViborSearchTournment searchTournment = new ViborSearchTournment();
            searchTournment.Show();
            Close();*/
            /*SqlConnection connection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
            connection.Open();
            string cmd = "select id,name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', fk_disciplina_id as 'id дисциплины',fk_gruppa_id as 'Номер группы, принимающей участие', fk_infotrener_id as 'id тренера' from sorevnovaniya";
            SqlCommand createcommand = new SqlCommand(cmd, connection);
            SqlDataAdapter sql = new SqlDataAdapter(createcommand);
            DataTable dt = new DataTable("sorevi");
            if(dt.Rows.Count > 0)
            {
                dt.DefaultView.RowFilter = string.Format("id like '%{0}%'", search.Text);
                sorevigrid.ItemsSource = dt.DefaultView;
            }
            */
            if(search.Text.Length > 0)
            {
                SqlConnection connection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
                connection.Open();
                string cmd = "select id,name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', fk_disciplina_id as 'id дисциплины',fk_gruppa_id as 'Номер группы, принимающей участие', fk_infotrener_id as 'id тренера' from sorevnovaniya where id like '%" + search.Text + "%' or mesto_provedeniya like '%" + search.Text + "%'" +
                    " or fk_disciplina_id like '%" + search.Text + "%' or fk_gruppa_id like '%" + search.Text + "%' or  fk_infotrener_id like '%" + search.Text + "%'";
                SqlCommand createcommand = new SqlCommand(cmd, connection);
                createcommand.ExecuteNonQuery();
                SqlDataAdapter sql = new SqlDataAdapter(createcommand);
                DataTable dt = new DataTable("sorevi");
                sql.Fill(dt);
                sorevigrid.ItemsSource = dt.DefaultView;
                connection.Close();
            }
            else if (search.Text.Length == 0)
            {
                SqlConnection connection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
                connection.Open();
                string cmd = "select id,name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', fk_disciplina_id as 'id дисциплины',fk_gruppa_id as 'Номер группы, принимающей участие', fk_infotrener_id as 'id тренера' from sorevnovaniya";
                SqlCommand createcommand = new SqlCommand(cmd, connection);
                createcommand.ExecuteNonQuery();
                SqlDataAdapter sql = new SqlDataAdapter(createcommand);
                DataTable dt = new DataTable("sorevi");
                sql.Fill(dt);
                sorevigrid.ItemsSource = dt.DefaultView;
                connection.Close();
            }
        }

        private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
           
            /*if (search.Text.Length > 0)
            {
                SqlConnection connection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
                connection.Open();
                string cmd = "select id,name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', fk_disciplina_id as 'id дисциплины',fk_gruppa_id as 'Номер группы, принимающей участие', fk_infotrener_id as 'id тренера' from sorevnovaniya";
                SqlCommand createcommand = new SqlCommand(cmd, connection);
                SqlDataAdapter sql = new SqlDataAdapter(createcommand);
                if(connection.State == ConnectionState.Open)
                {
                    DataTable dt = new DataTable("sorevi");
                    sql.Fill(dt);
                    sorevigrid.ItemsSource = dt.DefaultView;
                    connection.Close();
                }
                //sorevigrid.ItemsSource = dataSet.Tables[o]
            }
            */
        }
    }
}
