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

        private void Button_Click_Delete(object sender, RoutedEventArgs e)
        {
            DataTable sel = Select("select * from [dbo].[raspisaniye] where id = " + id_raspisaniye.Text);
            if (MessageBox.Show("Вы действительно хотите удалить данные ", "Удаление", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if(sel.Rows.Count > 0)
                {
                    for (int i = 0; i < id_raspisaniye.Text.Length; i++)
                    {
                        if (id_raspisaniye.Text.Length > 0 && id_raspisaniye.Text[i] >= '0' && id_raspisaniye.Text[i] <= '9')
                        {
                            DataTable dt_raspisaniye = Delete("delete from [dbo].[raspisaniye] where [id] = " + id_raspisaniye.Text);
                            MessageBox.Show("Занятие удалено");
                            Raspisaniye a = new Raspisaniye();
                            a.Show();
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("Поле код занятия введено не корректно");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Данного кода не существует");
                    
                }
            }
        }
        public DataTable Delete(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_Back(object sender, RoutedEventArgs e)
        {
            Raspisaniye ras = new Raspisaniye();
            ras.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;
        }
    }
}
