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>
    /// Логика взаимодействия для UpdateIdDiscipliniTournment.xaml
    /// </summary>
    public partial class UpdateIdDiscipliniTournment : Window
    {
        public UpdateIdDiscipliniTournment()
        {
            InitializeComponent();
        }

        private void Button_Click_Back(object sender, RoutedEventArgs e)
        {
            ViborUpdateTournments tournments = new ViborUpdateTournments();
            tournments.Show();
            Close();
        }
        public DataTable Select(string selectSQL)
        {
            DataTable dataTable = new DataTable("dataBase");
            SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = selectSQL;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(dataTable);
            sqlConnection.Close();
            return dataTable;
        }
        public DataTable Select1(string selectSQL)
        {
            DataTable dataTable = new DataTable("dataBase");
            SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = selectSQL;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(dataTable);
            sqlConnection.Close();
            return dataTable;
        }
        private void Button_Click_Up(object sender, RoutedEventArgs e)
        {
            if (id_dis.Text.Length != 0 && Name.Text.Length != 0)
            {
                bool turik = false, imya = false;
                DataTable sel1 = Select1("select * from [dbo].[sorevnovaniya] where id = " + id_dis.Text);
                for (int i = 0; i < id_dis.Text.Length; i++)
                {
                    if (turik)
                    {
                        turik = false;
                    }
                    if (id_dis.Text[i] >= '1' && id_dis.Text[i] <= '9')
                    {
                        if (sel1.Rows.Count > 0)
                        {
                            turik = true;
                        }
                        else
                        {
                            turik = false;
                        }

                    }
                    if (turik == false)
                    {
                        MessageBox.Show("Поле код соревнования должно содержать только цифры или вы ввели код, которого нет");
                    }
                }
                DataTable sel = Select1("select * from [dbo].[disciplina] where id = " + Name.Text);
                for (int i = 0; i < Name.Text.Length; i++)
                {
                    if (imya)
                    {
                        imya = false;
                    }
                    if (Name.Text[i] >= '1' && Name.Text[i] <= '7' && Name.Text.Length == 1)
                    {
                        if (sel.Rows.Count > 0)
                        {
                            imya = true;
                        }
                        else
                        {
                            imya = false;
                        }
                    }
                    if (imya == false)
                    {
                        MessageBox.Show("Поле дисциплина должно содержать код дисциплины (1-хоккей,2-Мини-футбол,3-Волейбол,4-Футбол,5-Баскетбол,6-Плавание,7-Бокс)");
                    }
                }
                if (turik && imya)
                {
                    DataTable upname = Update("update [dbo].[sorevnovaniya] set fk_disciplina_id = '" + Name.Text + "' where id = " + id_dis.Text);
                    MessageBox.Show("Поле дисциплина для соревнования " + id_dis.Text + " успешно изменено");
                    ViborUpdateTournments viborUpdate = new ViborUpdateTournments();
                    viborUpdate.Show();
                    Close();
                }
                else
                {
                    MessageBox.Show("Неправильный формат");
                }
            }
            else
            {
                MessageBox.Show("Все поля должны быть введены");
            }
        }

        public DataTable Update(string selectSQL)
        {
            DataTable dataTable = new DataTable("dataBase");
            SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = selectSQL;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(dataTable);
            sqlConnection.Close();
            return dataTable;
        }
    }
}
