using System; using System.Collections.Generic; using System.Linq; using Avalonia.Controls; using AvaloniaTeachersDB.Models; using AvaloniaTeachersDB.Views; using MsBox.Avalonia.Enums; using MsBox.Avalonia; using ReactiveUI; using Tmds.DBus.Protocol; using System.Collections.ObjectModel; namespace AvaloniaTeachersDB.ViewModels { public class AddTeacherViewModel : ReactiveObject { public static _43pToropovaContext DBConnection = new _43pToropovaContext(); private string _name; private string _surname; private string _patronymic; private string _mail; private string _phone; private DateTimeOffset _birthday = new DateTimeOffset(DateTime.Now); private SchoolSubject _selectSubject; private int _selectIndexSubject; private string _exp; private bool _genderM = true; private bool _enableBTN = false; //private List _collSubSelect = new List(); private ObservableCollection _collSubSelect = new ObservableCollection(); public string TName { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); } public string TSurname { get => _surname; set => this.RaiseAndSetIfChanged(ref _surname, value); } public string TPatronymic { get => _patronymic; set => this.RaiseAndSetIfChanged(ref _patronymic, value); } public DateTimeOffset TBirthday { get => _birthday; set => this.RaiseAndSetIfChanged(ref _birthday, value); } public string TMail { get => _mail; set => this.RaiseAndSetIfChanged(ref _mail, value); } public string TPhone { get => _phone; set => this.RaiseAndSetIfChanged(ref _phone, value); } public string TExp { get => _exp; set => this.RaiseAndSetIfChanged(ref _exp, value); } public bool TGenderM { get => _genderM; set => this.RaiseAndSetIfChanged(ref _genderM, value); } public bool EnableBTN { get => _enableBTN; set => this.RaiseAndSetIfChanged(ref _enableBTN, value); } public ObservableCollection CollSubSelect { get => _collSubSelect; set => this.RaiseAndSetIfChanged(ref _collSubSelect, value); } public List CollSub => DBConnection.SchoolSubjects.ToList(); private bool CheckNotNull() { if (TName == null && TSurname == null && TPatronymic == null) { var box = MessageBoxManager.GetMessageBoxStandard("Error", "нет фио"); box.ShowAsync(); return false; } if (TMail == null) { var box = MessageBoxManager.GetMessageBoxStandard("Error", "нет почты"); box.ShowAsync(); return false; } if(CollSubSelect.Count<=0) { var box = MessageBoxManager.GetMessageBoxStandard("Error", "нет преподаваемых предметов"); box.ShowAsync(); return false; } return true; } public SchoolSubject SelectSubject { get => _selectSubject; set { int f = 0; foreach (var item in CollSubSelect) { if (item == value) { f = 1; } } if (f == 0) CollSubSelect.Add(value); } } /* public int SelectIndexSubject { get => _selectIndexSubject; set { var mews = MessageBoxManager.GetMessageBoxStandard("ff", value.ToString()); mews.ShowAsync(); SchoolSubject tmp; if (CollSubSelect.Count>0) { int ind= 0; foreach(var item in CollSubSelect) { if (value == ind) { tmp = item; break; } ind++; } CollSubSelect.RemoveAt(ind); } } } */ public bool AddNewTeacher() { if (CheckNotNull()) { int indG = 1; int exp; //DateTime tmp = new DateTime(TBirthday.Year, TBirthday.Month, TBirthday.Day); string birtdate = TBirthday.Year +"-"+ TBirthday.Month +"-"+ TBirthday.Day; if (TGenderM == false) indG = 2; if (TExp == null) exp = 0; else exp = Convert.ToInt32(TExp); Teacher nT = new Teacher() { NameTeacher = TName, SurnameTeacher = TSurname, PatronymicTeacher = TPatronymic, BirthdayTeacher = DateOnly.Parse(birtdate), Mail = TMail, Phone = TPhone, ExperienceTeacher = exp, IdGender = indG }; DBConnection.Teachers.Add(nT); DBConnection.SaveChanges(); foreach(var item in CollSubSelect) { TeachersSubject ts = new TeachersSubject() { IdTeacher = nT.IdTeachers, IdSubjects = item.IdSubjects }; DBConnection.TeachersSubjects.Add(ts); } DBConnection.SaveChanges(); CollSubSelect.Clear(); return true; } return false; } } }