public ActionResult Edit(BookingEdit booking, string bookingType)
        {
            booking.DateCreated = DateTime.Now;
            booking.CreatedBy   = User.Identity.Name.ToString();
            booking.StartDate   = booking.StartDate.AddHours(double.Parse(booking.StartTime));
            booking.EndDate     = booking.EndDate.AddHours(double.Parse(booking.EndTime));
            booking.BookingType = bookingType;



            if (ModelState.IsValid)
            {
                bool conflict = false;

                foreach (var id in booking.RoomIds)
                {
                    if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id) && b.BookingId != booking.BookingId))
                    {
                        conflict      = true;
                        ViewBag.Error = "This room and time have been reserved.";
                    }
                }
                if (!conflict)
                {
                    if (booking.EndDate < booking.StartDate)
                    {
                        ViewBag.Error = "The End Date Can't Be Before the Start Date.";
                    }
                    else
                    {
                        foreach (var id in booking.RoomIds)
                        {
                            db.Entry(booking.GetBooking(id)).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        return(RedirectToAction("Details", new { id = booking.BookingId }));
                    }
                }
            }
            var mgmt = new BookingMgmt(bookingRepository);

            return(View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName)));
        }
        public ActionResult Create(BookingEdit booking, string bookingType, string viewName, string viewRoomName, string option, string monthlyoption, int increment)
        {
            DateTime resetDate = booking.StartDate;

            booking.RepeadEnd = booking.RepeadEnd.AddDays(1).AddSeconds(-1);

            booking.DateCreated = DateTime.Now;
            booking.CreatedBy   = User.Identity.Name.ToString();
            booking.StartDate   = booking.StartDate.AddHours(double.Parse(booking.StartTime));
            booking.EndDate     = booking.EndDate.AddHours(double.Parse(booking.EndTime));
            booking.BookingType = bookingType;

            var mgmt = new BookingMgmt(bookingRepository);

            switch (option)
            {
            case "Daily":
                potentialBookings = mgmt.DailyRecurring(booking);
                break;

            case "Weekly":
                potentialBookings = mgmt.WeeklyRecurring(booking, increment);
                break;

            case "Monthly":
                if (monthlyoption == "Day")
                {
                    potentialBookings = mgmt.MonthlyByDay(booking);
                }
                else if (monthlyoption == "Increment")
                {
                    potentialBookings = mgmt.MonthlyIncrement(booking);
                }
                break;

            default:     // code for non recurring
                if (ModelState.IsValid)
                {
                    bool conflict = false;

                    foreach (var id in booking.RoomIds)
                    {
                        if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id)))
                        {
                            conflict      = true;
                            ViewBag.Error = "This room and time have been reserved.";
                        }
                    }

                    if (!conflict)
                    {
                        if (booking.EndDate < booking.StartDate)
                        {
                            ViewBag.Error = "The End Date Can't Be Before the Start Date.";
                        }
                        else
                        {
                            foreach (var id in booking.RoomIds)
                            {
                                bookingRepository.InsertBooking(booking.GetBooking(id));
                                bookingRepository.Save();
                            }

                            if (viewName == "/")
                            {
                                return(RedirectToAction("DailyBookings", new { date = booking.StartDate.ToString("yyyy-MM-dd") }));
                            }
                            else
                            {
                                return(RedirectToAction(viewName, new { date = booking.StartDate.AddDays(-(((int)booking.StartDate.DayOfWeek) + 6) % 7), roomName = viewRoomName }));
                            }
                        }
                    }
                }
                return(View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName)));
                //break;
            }

            List <Booking> failedBookings = new List <Booking>();

            foreach (var date in potentialBookings)
            {
                booking.StartDate = date.AddHours(double.Parse(booking.StartTime));
                booking.EndDate   = date.AddHours(double.Parse(booking.EndTime));

                if (ModelState.IsValid)
                {
                    foreach (var id in booking.RoomIds)
                    {
                        if (db.Bookings.Any(b => ((b.StartTime >= booking.StartDate) && (b.EndTime <= booking.EndDate)) && b.RoomId.Equals(id)))
                        {
                            failedBookings.Add(booking.GetBooking(id));
                            //ViewBag.Error = "This room and time have been reserved.";
                            //potentialBookings.Remove(date);
                        }
                        else
                        {
                            bookingRepository.InsertBooking(booking.GetBooking(id));
                            bookingRepository.Save();
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();

            if (failedBookings.Count >= 1)
            {
                sb.Append("The following were not booked due to confilcts: \n\n");

                foreach (var item in failedBookings)
                {
                    string roomName = db.Rooms.
                                      Where(r => (r.RoomId.Equals(item.RoomId))).
                                      Select(r => r.RoomName).FirstOrDefault();

                    sb.Append(item.StartTime + " - " + item.EndTime.TimeOfDay + " " + roomName + "\n");
                }
            }
            TempData["ErrorList"] = sb.ToString();

            if (viewName == "/")
            {
                return(RedirectToAction("DailyBookings", new { date = resetDate.ToString("yyyy-MM-dd") }));
            }
            else
            {
                return(RedirectToAction(viewName, new { date = resetDate.AddDays(-(((int)resetDate.DayOfWeek) + 6) % 7), roomName = viewRoomName }));
            }

            //var mgmt = new BookingMgmt(bookingRepository);
            //return View(mgmt.GetBookingEdit(booking.StartDate, booking.StartDate, booking.EndDate, booking.ViewRoomName, booking.ViewName));
        }