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>
    /// Логика взаимодействия для DeleteSpisokUchenikov.xaml
    /// </summary>
    public partial class DeleteSpisokUchenikov : Window
    {
        public DeleteSpisokUchenikov()
        {
            InitializeComponent();
        }
        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;
        }
        private void Button_Click_Delete(object sender, RoutedEventArgs e)
        {
            DataTable sel = Select("select * from [dbo].[infouchenik] where id = " + id_uch.Text);
            if (MessageBox.Show("Вы действительно хотите удалить данные ", "Удаление", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if(sel.Rows.Count > 0)
                {
                    for (int i = 0; i < id_uch.Text.Length; i++)
                    {
                        if (id_uch.Text.Length > 0 && id_uch.Text[i] >= '0' && id_uch.Text[i] <= '9')
                        {
                            DataTable dt_uch = Delete("delete from [dbo].[infouchenik] where [id] = " + id_uch.Text);
                            MessageBox.Show("Ученик удален");
                            SpisokUchenikov uch = new SpisokUchenikov();
                            uch.Show();
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("Поле код ученика введено не корректно");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Данного кода не существует");
                }
            }
        }

        private void Button_Click_Back(object sender, RoutedEventArgs e)
        {
            SpisokUchenikov spisok = new SpisokUchenikov();
            spisok.Show();
            Close();
        }
        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;
        }
    }
}
