using System;
using System.Collections.Generic;
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;
using System.Windows.Navigation;
using System.Data.SqlClient;
using System.Data;

namespace KursachMukha692gr
{
    /// <summary>
    /// Логика взаимодействия для Raspisaniye.xaml
    /// </summary>
    public partial class Raspisaniye : Window
    {
        //KURSACH1Entities2 db;
        public Raspisaniye()
        {
            InitializeComponent();
            SqlConnection connection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
            connection.Open();
            string cmd = "select RASPISANIYE.id as 'Код',den_nedeli.dennedeli as 'День недели',vremya_zanyatiya.nachalo as 'Начало занятия', vremya_zanyatiya.konec as 'Конец занятия', fk_gruppi_id as 'Номер группы',disciplina.disciplina as 'Дисциплина',zal_trenerovki.zal as 'Зал',CONCAT(infotrener.familiyaTrener,' ',left(infotrener.imyaTrener,1) ,'.',left(infotrener.otchestvoTrener,1),'.') as 'ФИО тренера',corpus.corpus as 'Корпус'from den_nedeli,vremya_zanyatiya,RASPISANIYE,disciplina,zal_trenerovki,infotrener,corpus,gruppa where den_nedeli.id = RASPISANIYE.fk_den_nedeli_id and vremya_zanyatiya.id = RASPISANIYE.fk_vremya_zanyatiya_id and gruppa.id = RASPISANIYE.fk_gruppi_id and disciplina.id = RASPISANIYE.fk_disciplina_id and zal_trenerovki.id = RASPISANIYE.fk_zal_trenirovki_id and infotrener.id = RASPISANIYE.fk_trener_id and corpus.id = RASPISANIYE.fk_corpus_id";
            SqlCommand createcommand = new SqlCommand(cmd, connection);
            createcommand.ExecuteNonQuery();
            
            SqlDataAdapter sql = new SqlDataAdapter(createcommand);
            DataTable dt = new DataTable("raspisaniyes");
            sql.Fill(dt);
            raspisaniyegrid.ItemsSource = dt.DefaultView;
            connection.Close();

        }
        /*private void Load()
        {
            raspisaniyegrid.ItemsSource = Helper.context.RASPISANIYEs.ToList();
        }*/
        private void Button_Click_Back(object sender, RoutedEventArgs e)
        {
            KabinetAdmina kab = new KabinetAdmina();
            kab.Show();
            Close();
        }

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

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

        private void Button_Click_update(object sender, RoutedEventArgs e)
        {
            ViborUpdateRaspisaniye updateRaspisaniye = new ViborUpdateRaspisaniye();
            updateRaspisaniye.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 RASPISANIYE.id as 'Код',den_nedeli.dennedeli as 'День недели',vremya_zanyatiya.nachalo as 'Начало занятия', vremya_zanyatiya.konec as 'Конец занятия', fk_gruppi_id as 'Номер группы',disciplina.disciplina as 'Дисциплина',zal_trenerovki.zal as 'Зал',CONCAT(infotrener.familiyaTrener,' ',left(infotrener.imyaTrener,1) ,'.',left(infotrener.otchestvoTrener,1),'.') as 'ФИО тренера',corpus.corpus as 'Корпус'from den_nedeli,vremya_zanyatiya,RASPISANIYE,disciplina,zal_trenerovki,infotrener,corpus,gruppa where den_nedeli.id = RASPISANIYE.fk_den_nedeli_id and vremya_zanyatiya.id = RASPISANIYE.fk_vremya_zanyatiya_id and gruppa.id = RASPISANIYE.fk_gruppi_id and disciplina.id = RASPISANIYE.fk_disciplina_id and zal_trenerovki.id = RASPISANIYE.fk_zal_trenirovki_id and infotrener.id = RASPISANIYE.fk_trener_id and corpus.id = RASPISANIYE.fk_corpus_id " +
                    " and ( RASPISANIYE.id like '%" + search.Text + "%' or den_nedeli.dennedeli like '%" + search.Text + "%' or vremya_zanyatiya.nachalo like '%" + search.Text + "%' or vremya_zanyatiya.konec like '%" + search.Text + "%' or fk_gruppi_id like '%" + search.Text + "%' or disciplina.disciplina like '%" + search.Text + "%' or zal_trenerovki.zal like '%" + search.Text + "%' or CONCAT(infotrener.familiyaTrener,' ',left(infotrener.imyaTrener,1) ,'.',left(infotrener.otchestvoTrener,1)) like '%" + search.Text + "%' or corpus.corpus like '%" + search.Text + "%' )";
                SqlCommand createcommand = new SqlCommand(cmd, connection);
                createcommand.ExecuteNonQuery();
                SqlDataAdapter sql = new SqlDataAdapter(createcommand);
                DataTable dt = new DataTable("sorevi");
                sql.Fill(dt);
                raspisaniyegrid.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 RASPISANIYE.id as 'Код',den_nedeli.dennedeli as 'День недели',vremya_zanyatiya.nachalo as 'Начало занятия', vremya_zanyatiya.konec as 'Конец занятия', fk_gruppi_id as 'Номер группы',disciplina.disciplina as 'Дисциплина',zal_trenerovki.zal as 'Зал',CONCAT(infotrener.familiyaTrener,' ',left(infotrener.imyaTrener,1) ,'.',left(infotrener.otchestvoTrener,1),'.') as 'ФИО тренера',corpus.corpus as 'Корпус'from den_nedeli,vremya_zanyatiya,RASPISANIYE,disciplina,zal_trenerovki,infotrener,corpus,gruppa where den_nedeli.id = RASPISANIYE.fk_den_nedeli_id and vremya_zanyatiya.id = RASPISANIYE.fk_vremya_zanyatiya_id and gruppa.id = RASPISANIYE.fk_gruppi_id and disciplina.id = RASPISANIYE.fk_disciplina_id and zal_trenerovki.id = RASPISANIYE.fk_zal_trenirovki_id and infotrener.id = RASPISANIYE.fk_trener_id and corpus.id = RASPISANIYE.fk_corpus_id";
                SqlCommand createcommand = new SqlCommand(cmd, connection);
                createcommand.ExecuteNonQuery();
                SqlDataAdapter sql = new SqlDataAdapter(createcommand);
                DataTable dt = new DataTable("sorevi");
                sql.Fill(dt);
                raspisaniyegrid.ItemsSource = dt.DefaultView;
                connection.Close();
            }
        }

        private void search_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    } 
}
