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>
    /// Логика взаимодействия для UpdateFamiliyaUchenik.xaml
    /// </summary>
    public partial class UpdateFamiliyaUchenik : Window
    {
        List<string> Alphabetrus = new List<string> {
            "А", "а", "Б", "б", "В", "в", "Г", "г", "Д", "д", "Е", "е", "Ё", "ё", "Ж", "ж", "З", "з", "И", "и", "Й", "й",
            "К", "к", "Л", "л", "М", "м", "Н", "н", "О", "о", "П", "п", "Р", "р", "С", "с", "Т", "т", "У", "у", "Ф", "ф",
            "Х", "х", "Ц", "ц", "Ч", "ч", "Ш", "ш", "Щ", "щ", "Ъ", "ъ", "Ы", "ы", "Ь", "ь", "Э", "э", "Ю", "ю", "Я", "я" };
        public DataTable Update(string selectSQL)
        {
            DataTable dataTable = new DataTable("dataBase");
            SqlConnection sqlConnection = new SqlConnection("server=WIN-2CL665KE0ED; Trusted_Connection=YES;DataBase=KURSACH1;");
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = selectSQL;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(dataTable);
            sqlConnection.Close();
            return dataTable;
        }
        public UpdateFamiliyaUchenik()
        {
            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;
                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')
                    {
                        turik = true;
                    }
                    if (turik == false)
                    {
                        MessageBox.Show("Поле id ученика должно содержать только цифры");
                    }
                }
                for (int i = 0; i < Name.Text.Length; i++)//проверка фамилии
                {
                    if (imya)
                    {
                        imya = false;
                    }
                    for (int j = 0; j < Alphabetrus.Count; j++)
                    {
                        if (Convert.ToString(Name.Text[i]).Contains(Alphabetrus[j]))
                        {
                            imya = true;
                            break;
                        }
                    }
                    if (imya == false)
                    {
                        MessageBox.Show("Поле фамилия должно содержать только русские символы");
                        break;
                    }
                }
                if (turik && imya)
                {
                    DataTable upname = Update("update [dbo].[infouchenik] set familiya = '" + Name.Text + "' where id = " + id_turik.Text);
                    MessageBox.Show("Поле фамилия для id " + id_turik.Text + " успешно изменено");
                    ViborUchenikUpdate viborUchenikUpdate = new ViborUchenikUpdate();
                    viborUchenikUpdate.Show();
                    Close();
                }
                else
                {
                    MessageBox.Show("Неправильный формат");
                }
            }
            else
            {
                MessageBox.Show("Все поля должны быть введены");
            }
        }

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