public ActionResult Create(AppointmentConcern appointmentconcern)
        {
            if (ModelState.IsValid)
            {
                db.AppointmentConcerns.Add(appointmentconcern);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(appointmentconcern);
        }
        public ActionResult CreateMultiple(AppointmentCreateMultipleViewModel ViewModel, bool consultation = false)
        {
            Appointment appointment = ViewModel.appointment;
            if (consultation)
            {
                int programid = ViewModel.concerns.First();
                var concern = db.AppointmentConcerns.Where(c => c.program_id == programid).SingleOrDefault();
                if (concern == null)
                {
                    var program = programdb.Programs.Find(programid);
                    if (program != null)
                    {
                        concern = new AppointmentConcern { name = "Consultation", program_id = program.id };
                        db.AppointmentConcerns.Add(concern);
                    }
                }
                appointment.AppointmentConcerns.Add(concern);
            }
            else
            {
                if (ViewModel.concerns != null)
                {
                    foreach (var concernid in ViewModel.concerns)
                    {
                        var concern = db.AppointmentConcerns.Find(concernid);
                        if (concern != null)
                        {
                            appointment.AppointmentConcerns.Add(concern);
                        }
                    }
                }
            }

            int openedStatusId = db.AppointmentStatus.Where(x => x.name == "Opened").FirstOrDefault().id;
            appointment.status_id = openedStatusId;
            List<bool> availableDaysOfWeek = PrepareAvailableDaysOfWeek(ViewModel.config);
            List<Timeslot> timeslots = new List<Timeslot>();
            DateTime datecursor = ViewModel.config.start_date.Date;

            //initialize skipped_dates to avoid null reference
            var skipped_dates = new List<DateTime>();
            if (ViewModel.skipped_dates != null)
            {
                skipped_dates = ViewModel.skipped_dates.ToList();
            }

            try
            {
                while (datecursor <= ViewModel.config.end_date.Date)
                {
                    if (availableDaysOfWeek[Convert.ToInt16(datecursor.DayOfWeek)] && !skipped_dates.Any(d => d.Date == datecursor.Date))
                    {
                        foreach (var session in ViewModel.sessions.Where(s => !s.excluded).ToList())
                        {
                            TimeSpan starttime = session.start_time.TimeOfDay;
                            TimeSpan duration = new TimeSpan(0, ViewModel.duration, 0);
                            TimeSpan endtime = starttime.Add(duration);

                            while (endtime <= session.end_time.TimeOfDay)
                            {
                                bool overlapped = false;
                                TimeSpan newstarttime = new TimeSpan();

                                foreach (var ex_session in ViewModel.sessions.Where(x => x.excluded))
                                {
                                    TimeSpan ex_starttime = ex_session.start_time.TimeOfDay;
                                    TimeSpan ex_endtime = ex_session.end_time.TimeOfDay;
                                    if ((starttime >= ex_starttime && starttime < ex_endtime)
                                        || (endtime > ex_starttime && endtime <= ex_endtime)
                                        || (starttime <= ex_starttime && endtime >= ex_endtime))
                                    {
                                        overlapped = true;
                                        newstarttime = ex_endtime > newstarttime ? ex_endtime : newstarttime;
                                        goto Action;
                                    }
                                }

                                foreach (var ex_session in db.Appointments.Where(a => a.venue_id == appointment.venue_id))
                                {
                                    DateTime ex_starttime = ex_session.start_time;
                                    DateTime ex_endtime = ex_session.end_time;
                                    if ((datecursor.Add(starttime) >= ex_starttime && datecursor.Add(starttime) < ex_endtime)
                                        || (datecursor.Add(endtime) > ex_starttime && datecursor.Add(endtime) <= ex_endtime)
                                        || (datecursor.Add(starttime) <= ex_starttime && datecursor.Add(endtime) >= ex_endtime))
                                    {
                                        overlapped = true;
                                        newstarttime = ex_endtime.TimeOfDay > newstarttime ? ex_endtime.TimeOfDay : newstarttime;
                                        goto Action;
                                    }
                                }

                            Action:
                                if (!overlapped && endtime <= session.end_time.TimeOfDay)
                                {
                                    appointment.start_time = datecursor.Add(starttime);
                                    appointment.end_time = datecursor.Add(endtime);
                                    db.Appointments.Add(appointment);
                                    db.SaveChanges();
                                    starttime = endtime;
                                }
                                else
                                {
                                    starttime = newstarttime;
                                }
                                endtime = starttime.Add(duration);
                            }
                        }
                    }
                    datecursor = datecursor.AddDays(1);
                }
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                var status = db.AppointmentStatus.Where(s => s.name == "Opened").SingleOrDefault();
                ViewBag.statusList = new SelectList(db.AppointmentStatus, "id", "name", status.id);
                ViewBag.concernList = new MultiSelectList(db.AppointmentConcerns.Where(c => c.program_id == null && !c.custom), "id", "name");
                ViewBag.programList = new SelectList(programdb.Programs.Where(p => p.require_appointment), "name", "name");
                if (User.IsInRole("Admin") || User.IsInRole("Advising") || User.IsInRole("StudentDevelopment"))
                {
                    ViewBag.hostList = new SelectList(db.AppointmentHosts, "id", "name");
                }
                else
                {
                    ViewBag.hostList = new SelectList(db.AppointmentHosts.Where(h => h.SystemUsers.Any(u => u.UserName == User.Identity.Name)), "id", "name");
                }
                ViewBag.venueList = new SelectList(db.AppointmentVenues, "id", "name");
                ViewBag.consultation = consultation;
                Session["FlashMessage"] = "Failed to create appointment timeslots." + e.Message;
                return View(ViewModel);
            }
        }
        public ActionResult BookingSubmit(AppointmentBookingViewModel ViewModel)
        {
            ViewModel.host = db.AppointmentHosts.Find(ViewModel.host_id);
            ViewModel.appointment = db.Appointments.Find(ViewModel.appointment_id);
            ViewModel.concerns = db.AppointmentConcerns.Where(c => ViewModel.concern_ids.Contains(c.id)).ToList();

            ViewModel.appointment.AppointmentConcerns.Clear();
            if (!String.IsNullOrEmpty(ViewModel.other_concern))
            {
                var otherconcern = new AppointmentConcern { name = ViewModel.other_concern, custom = true };
                db.AppointmentConcerns.Add(otherconcern);
                ViewModel.appointment.AppointmentConcerns.Add(otherconcern);
            }
            foreach (var concern in ViewModel.concerns)
            {
                ViewModel.appointment.AppointmentConcerns.Add(concern);
            }
            var student = db.StudentProfiles.Find(User.Identity.Name);
            if (student != null)
            {
                ViewModel.appointment.student_id = student.id;
            }
            try
            {
                db.SaveChanges();
                Session["FlashMessage"] = "Appointment has been successfully booked.";
                return RedirectToAction("MyAppointment", "Appointment");
            }
            catch (Exception e)
            {
                Session["FlashMessage"] = "Failed to make the appointment booking.<br/><br/>" + e.Message;
                return RedirectToAction("Booking", "Appointment");
            }
        }
 public ActionResult Edit(AppointmentConcern appointmentconcern)
 {
     if (ModelState.IsValid)
     {
         db.Entry(appointmentconcern).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(appointmentconcern);
 }