private void buttonDelete_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete " + section.SectionName + "? Student list, classes and attendance data of this section will be permanently deleted", "Confirmation", MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                SectionController        scontroller  = new SectionController();
                SectionTimeController    stcontroller = new SectionTimeController();
                SectionStudentController sscontroller = new SectionStudentController();
                try
                {
                    ClassController      ccontroller = new ClassController();
                    List <ClassModel>    classList   = ccontroller.GetBySectionId(section.Id);
                    AttendanceController acontroller = new AttendanceController();

                    foreach (ClassModel Class in classList)
                    {
                        acontroller.DeleteAllByClass(Class.Id);
                    }

                    ccontroller.DeleteAllBySection(section.Id);
                    sscontroller.RemoveAllBySection(section.Id);
                    stcontroller.RemoveAllBySection(section.Id);
                    scontroller.Delete(section);
                    MessageBox.Show("Deleted!");
                    buttonBack.PerformClick();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void Remove_Click(object sender, EventArgs e, string data, string id)
        {
            var confirmResult = MessageBox.Show("Are you sure you want to remove " + data + "?",
                                                "Confirm Delete",
                                                MessageBoxButtons.YesNo);

            if (confirmResult == DialogResult.Yes)
            {
                UserController ucontroller = new UserController();
                try
                {
                    int gotId = ucontroller.GetIdByAcademicId(id);
                    Console.WriteLine("deleting number: " + gotId);
                    SectionStudentController sscontroller = new SectionStudentController();
                    try
                    {
                        sscontroller.Remove(section.Id, gotId);
                        MessageBox.Show("Removed!" + data);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            ClassModel Class = new ClassModel();

            Class.ClassDate   = datePicker.Value.ToString("yyyy-MM-dd");
            Class.StartTimeId = comboBoxStartTime.SelectedIndex + 1;
            Class.EndTimeId   = comboBoxEndTime.SelectedIndex + 1;
            Class.RoomNo      = textBoxRoomNo.Text;
            Class.SectionId   = section.Id;
            if (comboBoxClassType.SelectedIndex == 0)
            {
                Class.ClassType = ClassTypes.Lab;
            }
            else if (comboBoxClassType.SelectedIndex == 1)
            {
                Class.ClassType = ClassTypes.Theory;
            }
            else
            {
                try
                {
                    throw new Exception("Invalid class type");
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            ClassController ccontroller = new ClassController();

            try
            {
                Class.IsValid();

                List <ClassModel> sameDayClasses = ccontroller.GetByDateAndFacultyId(Class.ClassDate, faculty.Id);
                //Console.WriteLine("Found " + sameDayClasses.Count + " classes");
                foreach (ClassModel model in sameDayClasses)
                {
                    //Console.WriteLine("start id: " + model.StartTimeId + " end id: " + model.EndTimeId);
                    //Console.WriteLine("this start: " + Class.StartTimeId + " this end: " + Class.EndTimeId);

                    if (Class.StartTimeId < model.EndTimeId && Class.StartTimeId >= model.StartTimeId)
                    {
                        throw new Exception("Class time clashes with another class on " + Class.ClassDate);
                    }
                    if (Class.EndTimeId <= model.EndTimeId && Class.EndTimeId > model.StartTimeId)
                    {
                        throw new Exception("Class time clashes with another class on " + Class.ClassDate);
                    }
                }
                //Console.WriteLine("Clash checking complete");
                try
                {
                    var createdClass = ccontroller.Create(Class);

                    string qrstring        = createdClass.Id.ToString() + "|" + section.SectionName.ToString() + "|" + createdClass.CreatedAt.ToString();
                    string encodedqrstring = Convert.ToBase64String(Encoding.UTF8.GetBytes(qrstring));
                    string decodedqrstring = (Encoding.UTF8.GetString(Convert.FromBase64String(encodedqrstring)));
                    //Console.WriteLine(decodedqrstring);
                    try
                    {
                        ccontroller.InsertQRCode(createdClass.Id, encodedqrstring);

                        SectionStudentController sscontroller = new SectionStudentController();
                        List <int> sectionStudentsId          = sscontroller.GetAllBySection(section.Id);

                        AttendanceController acontroller = new AttendanceController();

                        foreach (int StudentId in sectionStudentsId)
                        {
                            AttendanceModel attendance = new AttendanceModel();
                            attendance.ClassId   = createdClass.Id;
                            attendance.StudentId = StudentId;

                            try
                            {
                                acontroller.Create(attendance);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        //Console.WriteLine("Created class id: " + createdClass.Id);
                        MessageBox.Show("Class added on " + (createdClass.ClassDate));
                        this.Hide();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            StudentUserModel student = new StudentUserModel();

            student.FirstName  = textBoxStudentFirstName.Text.Trim();
            student.LastName   = textBoxStudentLastName.Text.Trim();
            student.AcademicId = textBoxStudentId.Text;

            try
            {
                student.IsValid();
                UserController controller = new UserController();
                try
                {
                    BaseUserModel createdStudent = controller.Create(student);

                    SectionStudentController controller1 = new SectionStudentController();
                    controller1.Create(section.Id, createdStudent.Id);

                    ClassController      ccontroller = new ClassController();
                    List <ClassModel>    classList   = ccontroller.GetBySectionId(section.Id);
                    AttendanceController acontroller = new AttendanceController();

                    foreach (ClassModel Class in classList)
                    {
                        AttendanceModel attendance = new AttendanceModel();
                        attendance.StudentId = createdStudent.Id;
                        attendance.ClassId   = Class.Id;
                        try
                        {
                            acontroller.Create(attendance);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    MessageBox.Show(student.FullName + " added in " + section.SectionName);
                    var students = new FormStudentList(faculty, section);
                    students.FormClosed += new FormClosedEventHandler(dash_FormClosed);
                    students.Show();
                    this.Hide();
                }
                catch (NullReferenceException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (SQLiteException ex)
            {
                try
                {
                    UserController controller = new UserController();
                    var            gotStudent = controller.GetByAcademicId(student.AcademicId);
                    string         message    = "Student already exists in our database. Are you sure you want to add " + gotStudent.FullName + "(" + gotStudent.AcademicId + ")?";

                    DialogResult dialogResult = MessageBox.Show(message, "Confirm", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        try
                        {
                            SectionStudentController controller1 = new SectionStudentController();
                            controller1.Create(section.Id, gotStudent.Id);
                            MessageBox.Show(gotStudent.FullName + " added in " + section.SectionName);

                            var students = new FormStudentList(faculty, section);
                            students.FormClosed += new FormClosedEventHandler(dash_FormClosed);
                            students.Show();
                            this.Hide();
                        }
                        catch (SQLiteException exc)
                        {
                            if (exc.ErrorCode == 19)
                            {
                                MessageBox.Show("Error: Student already exists in this section");
                            }
                        }
                        catch (Exception exv)
                        {
                            MessageBox.Show(exv.Message);
                        }
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.Hide();
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ButtonCreate_Click(object sender, EventArgs e)
        {
            SectionModel section = new SectionModel();

            section.SectionName = textBoxSectionName.Text.Trim();
            section.FacultyId   = faculty.Id;

            SectionTimeController   stcontroller = new SectionTimeController();
            List <SectionTimeModel> sectionTimes = stcontroller.GetByFaculty(faculty.Id);

            try
            {
                section.IsValid();

                //Console.WriteLine("Section name: " + section.SectionName);
                //Console.WriteLine("Section facultyID: " + section.FacultyId);

                SectionTimeModel sectionTime1 = new SectionTimeModel();
                if (comboBoxClassType1.SelectedIndex == 0)
                {
                    sectionTime1.ClassType = ClassTypes.Lab;
                }
                else if (comboBoxClassType1.SelectedIndex == 1)
                {
                    sectionTime1.ClassType = ClassTypes.Theory;
                }

                sectionTime1.RoomNo      = textBoxRoomNo1.Text;
                sectionTime1.StartTimeId = comboBoxStartTime1.SelectedIndex + 1;
                sectionTime1.EndTimeId   = comboBoxEndTime1.SelectedIndex + 1;
                sectionTime1.WeekDayId   = comboBoxWeekDay1.SelectedIndex + 1;

                //Console.WriteLine("Section Time 1 info:");
                //Console.WriteLine("Start Id: " + sectionTime1.StartTimeId);
                //Console.WriteLine("End Id: " + sectionTime1.EndTimeId);
                //Console.WriteLine("Day Id: " + sectionTime1.WeekDayID);
                //Console.WriteLine("Room no: " + sectionTime1.RoomNo);
                //Console.WriteLine("Class type: " + sectionTime1.ClassType.ToString());
                //Console.WriteLine("Class length: " + (sectionTime1.EndTimeId - sectionTime1.StartTimeId).ToString());

                try
                {
                    sectionTime1.IsValid();

                    foreach (SectionTimeModel model in sectionTimes)
                    {
                        if (model.WeekDayId == sectionTime1.WeekDayId)
                        {
                            if (sectionTime1.StartTimeId < model.EndTimeId && sectionTime1.StartTimeId >= model.StartTimeId)
                            {
                                //Console.WriteLine("Clash 1 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                            if (sectionTime1.EndTimeId <= model.EndTimeId && sectionTime1.EndTimeId > model.StartTimeId)
                            {
                                //Console.WriteLine("Clash 2 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                            if (sectionTime1.StartTimeId < model.StartTimeId && sectionTime1.EndTimeId > model.EndTimeId)
                            {
                                //Console.WriteLine("Clash 3 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                throw new Exception("Section 1 time clashes with another section. Please choose a different time");
                            }
                        }
                    }

                    SectionTimeModel sectionTime2 = new SectionTimeModel();
                    if (!checkBoxIgnore.Checked)
                    {
                        if (comboBoxClassType2.SelectedIndex == 0)
                        {
                            sectionTime2.ClassType = ClassTypes.Lab;
                        }
                        else if (comboBoxClassType2.SelectedIndex == 1)
                        {
                            sectionTime2.ClassType = ClassTypes.Theory;
                        }
                        sectionTime2.RoomNo      = textBoxRoomNo2.Text;
                        sectionTime2.StartTimeId = comboBoxStartTime2.SelectedIndex + 1;
                        sectionTime2.EndTimeId   = comboBoxEndTime2.SelectedIndex + 1;
                        sectionTime2.WeekDayId   = comboBoxWeekDay2.SelectedIndex + 1;

                        //Console.WriteLine("Section Time 2 info:");
                        //Console.WriteLine("Start Id: " + sectionTime2.StartTimeId);
                        //Console.WriteLine("End Id: " + sectionTime2.EndTimeId);
                        //Console.WriteLine("Day Id: " + sectionTime2.WeekDayID);
                        //Console.WriteLine("Room no: " + sectionTime2.RoomNo);
                        //Console.WriteLine("Class type: " + sectionTime2.ClassType.ToString());
                        //Console.WriteLine("Class length: " + (sectionTime2.EndTimeId - sectionTime2.StartTimeId).ToString());
                    }

                    SectionModel createdSection = new SectionModel();
                    try
                    {
                        if (!checkBoxIgnore.Checked)
                        {
                            sectionTime2.IsValid();
                            foreach (SectionTimeModel model in sectionTimes)
                            {
                                if (model.WeekDayId == sectionTime2.WeekDayId)
                                {
                                    if (sectionTime2.StartTimeId < model.EndTimeId && sectionTime2.StartTimeId >= model.StartTimeId)
                                    {
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                    if (sectionTime2.EndTimeId <= model.EndTimeId && sectionTime2.EndTimeId > model.StartTimeId)
                                    {
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                    if (sectionTime2.StartTimeId < model.StartTimeId && sectionTime2.EndTimeId > model.EndTimeId)
                                    {
                                        //Console.WriteLine("Clash 3 with\nstart id: " + model.StartTimeId + "\nend id: " + model.EndTimeId);
                                        throw new Exception("Section 2 time clashes with another section. Please choose a different time");
                                    }
                                }
                            }
                        }
                        SectionController controller = new SectionController();

                        try
                        {
                            createdSection = controller.Create(section);

                            //Console.WriteLine("Created section id: " + createdSection.Id);

                            sectionTime1.SectionId = createdSection.Id;
                            if (!checkBoxIgnore.Checked)
                            {
                                sectionTime2.SectionId = createdSection.Id;
                            }

                            SectionTimeController controller2 = new SectionTimeController();

                            try
                            {
                                controller2.Create(sectionTime1);
                                if (!checkBoxIgnore.Checked)
                                {
                                    controller2.Create(sectionTime2);
                                }

                                MessageBox.Show(createdSection.SectionName + " created!");
                                LoadingForm loadingForm = new LoadingForm();

                                if (studentList.Count != 0)
                                {
                                    UserController ucontroller = new UserController();

                                    //loading screen

                                    loadingForm.Show();
                                    //Console.WriteLine("Starting loop");
                                    foreach (StudentUserModel student in studentList)
                                    {
                                        try
                                        {
                                            var createdStudent = ucontroller.Create(student);
                                            try
                                            {
                                                SectionStudentController sscontroller = new SectionStudentController();
                                                sscontroller.Create(createdSection.Id, student.Id);
                                            }
                                            catch (SQLiteException exc)
                                            {
                                                if (exc.ErrorCode == 19)
                                                {
                                                    MessageBox.Show("Error: Student already exists in this section");
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show(ex.Message);
                                            }
                                        }
                                        catch (SQLiteException ex)
                                        {
                                            var existingStudent = ucontroller.GetByAcademicId(student.AcademicId);
                                            try
                                            {
                                                SectionStudentController sscontroller = new SectionStudentController();
                                                sscontroller.Create(createdSection.Id, existingStudent.Id);
                                            }
                                            catch (SQLiteException exc)
                                            {
                                                if (exc.ErrorCode == 19)
                                                {
                                                    MessageBox.Show("Error: Student already exists in this section");
                                                }
                                            }
                                            catch (Exception exc)
                                            {
                                                MessageBox.Show(exc.Message);
                                            }
                                        }
                                        loadingForm.Step(1);
                                    }
                                    //Console.WriteLine("Ending loop");
                                }
                                loadingForm.Close();

                                buttonBack.PerformClick();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }