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

        private void RegBtn_Click(object sender, RoutedEventArgs e)
        {
            gr606_midvlContext contextCheck = new gr606_midvlContext();
            var usersCheck = contextCheck.Persons;
            Person user = usersCheck.SingleOrDefault(x => x.Login == LoginBox.Text);
            if (usersCheck == null)
            {
                if (NameBox.Text != "" && LastNameBox.Text != "" && MiddleNameBox.Text != "" && LoginBox.Text != "" && PasswordBox.Text != "" && PhoneBox.Text != "" && PasSerialBox.Text != "" && PasNumBox.Text != "" && PasKemVidanBox.Text != "" && PasKemVidanBox.Text != "")
                {
                    gr606_midvlContext context = new gr606_midvlContext();
                    var users = context.Persons;
                    var passport = context.Passports;




                    //context.SaveChanges(); // Сохраняю

                    Passport newPassport = new Passport()
                    {
                        Serial = PasSerialBox.Text,
                        Number = PasNumBox.Text,
                        Issuedby = PasKemVidanBox.Text,
                        DateOfIssue = Convert.ToDateTime(PasDateGived.Text),

                    };

                    Person newUser = new Person()
                    {
                        FirstName = NameBox.Text,
                        LastName = LastNameBox.Text,
                        MidleName = MiddleNameBox.Text,
                        PhoneNumber = PhoneBox.Text,
                        Login = LoginBox.Text,
                        Password = PasswordBox.Text,
                        Passports = new List<Passport>() { newPassport },
                    };
                    users.Add(newUser);
                    context.SaveChanges();


                    //passport.Add(newPassport); // Добовляю

                    //newUser.Passport = passport.Count() + 1;
                    // users.Update(newUser);

                    //context.SaveChanges();

                    PersonSave.Login = LoginBox.Text;
                    PersonSave.Password = PasswordBox.Text;
                    PersonSave.LastName = LastNameBox.Text;
                    PersonSave.Name = NameBox.Text;
                    PersonSave.MidleName = MiddleNameBox.Text;
                    PersonSave.PhoneNumber = PhoneBox.Text;
                    PersonSave.Passport = newPassport.Id;
                    HomeWindow homeWindow = new HomeWindow();
                    homeWindow.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Все поля должны быть заполнены!");
                }
            }
            else
            {
                MessageBox.Show("Логин занят!");
            }
        }
    }
}
