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

        private void Button_Click_Up(object sender, RoutedEventArgs e)
        {
            if (id_turik.Text.Length != 0 && Name.Text.Length != 0)
            {
                bool turik = false, imya = false;
                DataTable sel1 = Select1("select * from [dbo].[RASPISANIYE] where id = " + id_turik.Text);
                for (int i = 0; i < id_turik.Text.Length; i++)
                {
                    if (turik)
                    {
                        turik = false;
                    }
                    if (id_turik.Text[i] >= '1' && id_turik.Text[i] <= '9')
                    {
                        if (sel1.Rows.Count > 0)
                        {
                            turik = true;
                        }
                        else
                        {
                            turik = false;
                        }

                    }
                    if (turik == false)
                    {
                        MessageBox.Show("Поле код занятия должно содержать только цифры или вы ввели код, которого нет");
                    }
                }
                DataTable sel = Select("select * from [dbo].[vremya_zanyatiya] 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-(06:00-08:00) , 2-(08:30-10:30), 3-(11:00-13:00), 4-(13:30-15:30), 5-(16:00-18:00), 6-(18:30-20:30), 7-(21:00-23:00))");
                    }
                }
                if (turik && imya)
                {
                    DataTable upname = Update("update [dbo].[RASPISANIYE] set fk_vremya_zanyatiya_id = '" + Name.Text + "' where id = " + id_turik.Text);
                    MessageBox.Show("Поле время занятия для занятия " + id_turik.Text + " успешно изменено");
                    ViborUpdateRaspisaniye updateRaspisaniye = new ViborUpdateRaspisaniye();
                    updateRaspisaniye.Show();
                    Close();
                }
                else
                {
                    MessageBox.Show("Неправильный формат");
                }
            }
            else
            {
                MessageBox.Show("Все поля должны быть введены");
            }
        }

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