public ActionResult AddVisit(OfficeVisitCollection model, string studentSelect, string sentbySelect, string homeroomSelect, string remedialSelect,
            string violationSelect, string handledbySelect, string locationSelect)
        {
            OfficeVisitCollection coll = (OfficeVisitCollection)Session["AddVisitModel"];

            if (ValidateData(model, studentSelect, sentbySelect, homeroomSelect, remedialSelect, violationSelect, handledbySelect, locationSelect))
            {
                /* validation and transpose */
                using (var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString))
                {
                      bool sucessful = new OfficeVisitRepository(connection).InsertOfficeVisit(model);
                }

                /* Add message . clear and  return back to add page */
                ModelState.Clear();
                ViewBag.Success = true;
                ModelState.AddModelError(string.Empty,"Office Visit has successully been added");
                coll.arrival_dt = DateTime.Now;
                coll.office_visit_dt = DateTime.Now;
                coll.comments = null;
                coll.nap = false;
                coll.StudentSelectList = new SelectList(coll.allStudents, "student_id", "student_name", null );
                coll.ReportersSelectList = new SelectList(coll.allReporters, "contact_id", "contact_name", null);
                coll.HomeRoomSelectList = new SelectList(coll.allHomeRooms, "homeroom_id", "homeroom_name", null);
                coll.HandleBySelectList = new SelectList(coll.allHandledBys, "contact_id", "contact_name", null);
                coll.LocationSelectList = new SelectList(coll.allLocations, "content_course_id", "name", null);
                coll.RemedialSelectList = new SelectList(coll.allRemedials, "remedial_action_id", "name", null);
                coll.ViolationSelectList = new SelectList(coll.allCodeViolations, "code_of_conduct_violation_id", "name", null);

            }
            else
            {

               
                ViewBag.Success = false;
                ModelState.AddModelError(string.Empty, "Student, Homeroom, Violation, Location and SentBy are required to save an Office Visit.");
       
                coll.arrival_dt = model.arrival_dt;
                coll.office_visit_dt = model.office_visit_dt;
                coll.comments = model.comments;
                coll.nap = model.nap;
                coll.StudentSelectList = new SelectList(coll.allStudents, "student_id", "student_name", studentSelect);
                coll.ReportersSelectList = new SelectList(coll.allReporters, "contact_id", "contact_name", sentbySelect);
                coll.HomeRoomSelectList = new SelectList(coll.allHomeRooms, "homeroom_id", "homeroom_name", homeroomSelect);
                coll.HandleBySelectList = new SelectList(coll.allHandledBys, "contact_id", "contact_name", handledbySelect);
                coll.LocationSelectList = new SelectList(coll.allLocations, "content_course_id", "name", locationSelect);
                coll.RemedialSelectList = new SelectList(coll.allRemedials, "remedial_action_id", "name", remedialSelect);
                coll.ViolationSelectList = new SelectList(coll.allCodeViolations, "code_of_conduct_violation_id", "name", violationSelect);


            }
            return View("AddOfficeVisit",coll);
        }
        public async System.Threading.Tasks.Task<ActionResult> EditOfficeVisit(string id)
        {

            OfficeVisitCollection coll = new OfficeVisitCollection();

            using (var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString))
            {
                await connection.OpenAsync();
               
                var result = new StudentRepository(connection).GetStudents();
                var result2 = new ContactRepository(connection).GetContacts();
                var result3 = new ContentCourseRepository(connection).GetContentCourses();
                var result4 = new CodeOfConductViolationRepository(connection).GetCodeOfConductViolations();
                var result5 = new HomeRoomRepository(connection).GetHomeRooms();
                var result6 = new RemedialActionRepository(connection).GetRemedialActions();
                var result7 = new OfficeVisitRepository(connection).GetOfficeVisitByID(Convert.ToInt32(id));

                coll.officeVisit = result7.Result;
                
                coll.allStudents = (IEnumerable<Models.Student>)result.Result.ToArray();
                coll.allReporters = coll.allHandledBys = (IEnumerable<Models.Contact>)result2.Result.ToArray();
                coll.allLocations = (IEnumerable<Models.ContentCourse>)result3.Result.ToArray();
                coll.allCodeViolations = (IEnumerable<Models.CodeOfConductViolation>)result4.Result.ToArray();
                coll.allHomeRooms = (IEnumerable<Models.HomeRoom>)result5.Result.ToArray();
                coll.allRemedials = (IEnumerable<Models.RemedialAction>)result6.Result.ToArray();

                coll.remedialAction = new OfficeVisitRepository(connection).GetOfficeVisitRemedyAction(coll.officeVisit.office_visit_id);
                coll.CodeViolation = new OfficeVisitRepository(connection).GetOfficeVisitCodeViolation(coll.officeVisit.office_visit_id);

                coll.office_visit_id = coll.officeVisit.office_visit_id;
                coll.arrival_dt = coll.officeVisit.arrival_dt;
                coll.office_visit_dt = coll.officeVisit.office_visit_dt;
                coll.nap = coll.officeVisit.nap;
                coll.comments = coll.officeVisit.comments;

                coll.StudentSelectList =  new SelectList(coll.allStudents, "student_id", "student_name", coll.officeVisit.student_id);
                coll.ReportersSelectList = new SelectList(coll.allReporters, "contact_id", "contact_name", coll.officeVisit.sent_by_contact_id);
                coll.HomeRoomSelectList = new SelectList(coll.allHomeRooms, "homeroom_id", "homeroom_name", coll.officeVisit.homeroom_id);
                coll.HandleBySelectList = new SelectList(coll.allHandledBys, "contact_id", "contact_name", coll.officeVisit.handled_by_contact_id);
                coll.LocationSelectList = new SelectList(coll.allLocations, "content_course_id", "name", coll.officeVisit.content_course_id);
                
                coll.RemedialSelectList = new SelectList(coll.allRemedials, "remedial_action_id", "name", coll.remedialAction);
                coll.ViolationSelectList = new SelectList(coll.allCodeViolations, "code_of_conduct_violation_id", "name", coll.CodeViolation);
                Session["OfficeVisitId"] = id;
            }

            return View(coll);
        }
        public async System.Threading.Tasks.Task<ActionResult> Dashboard()
        {
            DashboardCollection coll = new DashboardCollection();

            using (var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString))
            {
                await connection.OpenAsync();
                
                var result = new OfficeVisitRepository(connection).GetOfficeVisitsByHomeroom();
                var result2 = new OfficeVisitRepository(connection).GetOfficeVisitsByOffenseType();
                var result3 = new OfficeVisitRepository(connection).GetOfficeVisitsByTeacher();
                var result4 = new OfficeVisitRepository(connection).GetOfficeVisitsCountByWeek();

                coll.Homerooms = (IEnumerable<OfficeVisitsByHomeroom>)result.Result.ToArray();
                coll.OffenseTypes = (IEnumerable<OfficeVisitsByOffenseType>)result2.Result.ToArray();
                coll.Teachers = (IEnumerable<OfficeVisitsByTeacher>)result3.Result.ToArray();
                coll.ByWeek = (IEnumerable<OfficeVisitsCountsByWeek>)result4.Result.ToArray();
                Session["Dashboard"] = coll;

                return View(coll);
            }
        }
 public bool SaveOfficeVisit(OfficeVisitCollection model)
 {
     bool sucessful = false;
     using (var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString))
     {
         sucessful = new OfficeVisitRepository(connection).UpdateOfficeVisit(model);
     }
     return sucessful;
 }