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=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
            connection.Open();
            string cmd = "select sorevnovaniya.id as 'Код',name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', disciplina.disciplina as 'Дисциплина',gruppa.gruppa as 'Номер группы, принимающей участие'," +
                "CONCAT(infotrener.familiyaTrener, ' ', left(infotrener.imyaTrener, 1), '.', left(infotrener.otchestvoTrener, 1), '.') as 'ФИО тренера' from sorevnovaniya, disciplina, gruppa, infotrener " +
                "where disciplina.id = sorevnovaniya.fk_disciplina_id and gruppa.id = sorevnovaniya.fk_gruppa_id and infotrener.id = sorevnovaniya.fk_infotrener_id";
            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)
        {
            if(search.Text.Length > 0)
            {
                SqlConnection connection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
                connection.Open();
                string cmd = "select sorevnovaniya.id as 'Код',name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', disciplina.disciplina as 'Дисциплина',gruppa.gruppa as 'Номер группы, принимающей участие'," +
                               "CONCAT(infotrener.familiyaTrener, ' ', left(infotrener.imyaTrener, 1), '.', left(infotrener.otchestvoTrener, 1), '.') as 'ФИО тренера' from sorevnovaniya, disciplina, gruppa, infotrener " +
                               "where disciplina.id = sorevnovaniya.fk_disciplina_id and gruppa.id = sorevnovaniya.fk_gruppa_id and infotrener.id = sorevnovaniya.fk_infotrener_id" +
                               " and (sorevnovaniya.id like '%" + search.Text + "%' or mesto_provedeniya like '%" + search.Text + "%' or disciplina.disciplina like '%" + search.Text + "%' or gruppa.gruppa like '%" + search.Text + "%' or CONCAT(infotrener.familiyaTrener,' ',left(infotrener.imyaTrener,1) ,'.',left(infotrener.otchestvoTrener,1),'.') 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=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
                connection.Open();
                string cmd = "select sorevnovaniya.id as 'Код',name_tournment as 'Название турнира', mesto_provedeniya as 'Место проведения', disciplina.disciplina as 'Дисциплина',gruppa.gruppa as 'Номер группы, принимающей участие'," +
                                "CONCAT(infotrener.familiyaTrener, ' ', left(infotrener.imyaTrener, 1), '.', left(infotrener.otchestvoTrener, 1), '.') as 'ФИО тренера' from sorevnovaniya, disciplina, gruppa, infotrener " +
                                "where disciplina.id = sorevnovaniya.fk_disciplina_id and gruppa.id = sorevnovaniya.fk_gruppa_id and infotrener.id = sorevnovaniya.fk_infotrener_id";
                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]
            }
            */
        }
    }
}
