public void Add(Tasks t)
 {
     using (TaskManEntities tme = new TaskManEntities())
     {
         tme.Tasks.Add(t);
         tme.SaveChanges();
     }
 }
 public void MSD(int SelectedId)
 {
     using (TaskManEntities tme = new TaskManEntities())
     {
         Tasks t = tme.Tasks.Where(p => p.id == SelectedId).FirstOrDefault();
         t.taskStatus = "Виконано";
         tme.SaveChanges();
     }
 }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     id=crud.getId();
     currentDate = DateTime.Now.Date.ToString();
     cd = new TaskManEntities();
     MyGrid.ItemsSource = (from t in cd.Tasks select new { t.id, t.taskName, t.taskPriority, t.taskStatus, t.taskDate, t.UserID }).Where(p=>p.taskDate==currentDate).Where(p=>p.UserID==id).ToList();
     Donebtn.IsEnabled = false;
     Editbtn.IsEnabled = false;
     Deletebtn.IsEnabled = false;
 }
        private void okbtn_Click(object sender, RoutedEventArgs e)
        {
            if (passbx.Password == passbx1.Password && nametbx.Text!=null && logintbx.Text!=null && passbx.Password!=null & passbx1!=null)
            {
                Users u = new Users();
                u.userLogin = logintbx.Text;
                u.userName = nametbx.Text;
                u.userPass = passbx.Password;
                u.userDOB = Dpick1.SelectedDate.ToString();
                u.userEmail = emailtbx.Text;
                int count = 0;
                TaskManEntities tme = new TaskManEntities();
                foreach (var v in tme.Users)
                {
                    if (v.userLogin == u.userLogin)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    try
                    {

                        tme.Users.Add(u);
                        tme.SaveChanges();
                        Login l = new Login();
                        MessageBox.Show("Реєстрація пройшла успішно!");
                        this.Close();
                        l.ShowDialog();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                MessageBox.Show(validationError.ErrorMessage);
                            }
                        }
                    }

                }
                else
                {
                    MessageBox.Show("Цей логін уже зайнятий. Виберіть інший.");
                }
            }
            else
            {
                MessageBox.Show("Ви не заповнили усі поля або введені паролі не співпадають");
            }
        }
        public void Delete(int SelectedId)
        {
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Ви впевнені, що хочете видалити завдання?", "Видалення завдання", System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {

                using (TaskManEntities tme = new TaskManEntities())
                {
                    Tasks t = tme.Tasks.Where(p => p.id == SelectedId).FirstOrDefault();
                    tme.Tasks.Remove(t);
                    tme.SaveChanges();

                }
            }
        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            CRUD c = new CRUD();
            String userName = txtBxuserName.Text.ToLower();
            string pass = passBxPassword.Password;
            lblfrgtPass.Visibility = Visibility.Hidden;
            lblLoading.Visibility = Visibility.Visible;

            txtBxuserName.IsEnabled = false;
            passBxPassword.IsEnabled = false;
            btnLogin.IsEnabled = false;
            TaskManEntities tme = new TaskManEntities();
            var user=tme.Users.Where(p=>p.id==1).FirstOrDefault();
            foreach (var v in tme.Users)
            {
                if (v.userLogin == userName)
                {
                   user = tme.Users.Where(p => p.userLogin == userName).FirstOrDefault();
                }
            }
            
            if (user.userPass == pass)
            {
                MainWindow mw = new MainWindow();
                c.setID(user.id);
                this.Close();
                mw.ShowDialog();
               
                
                
            }

            else
            {
                lblLoading.Visibility = Visibility.Hidden;
                lblfrgtPass.Visibility = Visibility.Visible;
                txtBxuserName.IsEnabled = true;
                txtBxuserName.BorderBrush = Brushes.Red;
                passBxPassword.IsEnabled = true;
                passBxPassword.BorderBrush = Brushes.Red;
                btnLogin.IsEnabled = true;
                Mouse.OverrideCursor = null;
            }

        }
 private void Editbtn_Click(object sender, RoutedEventArgs e)
 {
     object item = MyGrid.SelectedItem;
     string ID = (MyGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
     SelectedId = Int32.Parse(ID);
     using (TaskManEntities tme = new TaskManEntities())
     {
         Tasks t = tme.Tasks.Where(p => p.id == SelectedId).FirstOrDefault();
         if (t.taskStatus == "Виконано")
         {
             MessageBox.Show("Ви не можете змінити виконане завдання.");
         }
         else
         {
             Edit edit = new Edit(SelectedId);
             var result = edit.ShowDialog();
         }
     }
     
 }