public ActionResult Delete(int thisTripId, FormCollection collection)
        {
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId()))
            {
                if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights
                {
                    return(RedirectToAction("AccessDenied", "Manage"));
                }
            }


            int NumberOfReservations = countReservavtionsMade((int)thisTripId);

            Trip trip = dbcontext.Trips.Find(thisTripId); //get current trip

            //check for reservations. Cannot edit while there are any and trip is yet to end
            var currTime = DateTime.Now;

            if (NumberOfReservations > 0 && trip.DateBack > currTime)
            {
                return(RedirectToAction("Index", new { Message = ManageMessageId.CannotEditEntry }));
            }

            dbcontext.Trips.Remove(trip);
            dbcontext.SaveChanges();

            return(RedirectToAction("Index", new { Message = ManageMessageId.DeleteEntrySuccess }));
        }
        // GET: Trip/Delete/5
        public ActionResult Delete(int?thisTripId)
        {
            if (thisTripId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId()))
            {
                if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights
                {
                    return(RedirectToAction("AccessDenied", "Manage"));
                }
            }

            //check for reservations. Cannot edit while there are any. Placeholder
            int NumberOfReservations = countReservavtionsMade((int)thisTripId);

            Trip trip = dbcontext.Trips.Find(thisTripId); //get current trip

            if (trip == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            //check for reservations. Cannot edit while there are any and trip is yet to end
            var currTime = DateTime.Now;

            if (NumberOfReservations > 0 && trip.DateBack > currTime)
            {
                return(RedirectToAction("Index", new { Message = ManageMessageId.CannotEditEntry }));
            }


            return(View(trip));
        }
        // GET: Trip/Create
        public ActionResult Create()
        {
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId()))
            {
                if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights
                {
                    return(RedirectToAction("AccessDenied", "Manage"));
                }
            }

            return(View());
        }
        // duplicated action EDIT POST/GET and changed to EditUserRoles
        // GET: ManageUsers/EditUserRoles/5
        public ActionResult EditUserRoles(string Id)
        {
            if (Id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId())) // check if current user has admin rights
            {
                return(RedirectToAction("AccessDenied", "Manage"));
            }
            ApplicationUser CurrUser = dbcontext.Users.Find(Id);

            if (CurrUser == null)
            {
                return(HttpNotFound());
            }

            EditUserRoleViewModel field = new EditUserRoleViewModel();    // get access to model fields we will be showing
            var userStore   = new UserStore <ApplicationUser>(dbcontext); // access to roles using Identity Framework
            var userManager = new UserManager <ApplicationUser>(userStore);

            // update db info with new data given by user in form View
            field.Id          = CurrUser.Id;
            field.UserName    = CurrUser.UserName;
            field.Email       = CurrUser.Email;
            field.Name        = CurrUser.Name;
            field.Surname     = CurrUser.Surname;
            field.Country     = CurrUser.Country;
            field.Town        = CurrUser.Town;
            field.Street      = CurrUser.Street;
            field.NumHouse    = CurrUser.NumHouse;
            field.NumFlat     = CurrUser.NumFlat;
            field.ZIPCode     = CurrUser.ZIPCode;
            field.PhoneNumber = CurrUser.PhoneNumber;

            if (UserRoleHelper.IsAdmin(field.Id))
            {
                field.RoleType = UserRoleTypes.Administrator;
            }
            if (UserRoleHelper.IsEmployee(field.Id))
            {
                field.RoleType = UserRoleTypes.Employee;
            }
            if (UserRoleHelper.IsUser(field.Id))
            {
                field.RoleType = UserRoleTypes.Customer;
            }

            return(View(field));
        }
        public ActionResult Create(Trip model)
        {
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId()))
            {
                if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights
                {
                    return(RedirectToAction("AccessDenied", "Manage"));
                }
            }
            if (ModelState.IsValid)
            {
                // CoachNumberId is a required field in Trip Table BUT because when assigning a coach to a trip we need to be sure its not already
                // assigned to a different trip going on in  the time of creating this trip (checked by looking for trips in progress)
                // there is no way to know at the time of this action what coaches can be used since we dont know when THIS trip will start-end.
                // we know that only after its created. so we assign a coachID which will never be created by DB and later assign a proprt value in edit action
                model.CoachNumberId = -1;

                dbcontext.Trips.Add(model);
                dbcontext.SaveChanges();
                return(RedirectToAction("Index", new { Message = ManageMessageId.CreateEntrySuccess }));
            }
            return(View(model));
        }
        // GET: Trip/Edit/5
        public ActionResult Edit(int?thisTripId)
        {
            if (thisTripId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId()))
            {
                if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights
                {
                    return(RedirectToAction("AccessDenied", "Manage"));
                }
            }

            //check for reservations. Cannot edit while there are any.
            int NumberOfReservations = countReservavtionsMade((int)thisTripId);

            Trip trip = dbcontext.Trips.Find(thisTripId); //get current trip

            if (trip == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            ViewEditTripsViewModel model = new ViewEditTripsViewModel();

            model.TripInstance = trip;


            //check for reservations. Cannot edit while there are any and trip is yet to end
            var currTime = DateTime.Now;

            if (NumberOfReservations > 0 && model.TripInstance.DateBack > currTime)
            {
                return(RedirectToAction("Index", new { Message = ManageMessageId.CannotEditEntry }));
            }


            //get a list of all sub-locations that this trip has
            var list = new List <TripLocationsInstanceViewModels>();

            foreach (var item in dbcontext.Trip_Locations.ToList())
            {
                if (item.Id_Trip == thisTripId)
                {
                    foreach (var location in dbcontext.Locations.ToList())
                    {
                        if (item.Id_Location == location.Id)
                        {
                            list.Add(new TripLocationsInstanceViewModels
                            {
                                Country         = location.Country,
                                Town            = location.Town,
                                Name            = location.Name,
                                Description     = location.Description,
                                LocationImage   = location.LocationImage,
                                Number          = item.Number,
                                RouteInstanceId = item.Id
                            });
                        }
                    }
                }
            }
            model.Route = new TripLocationsViewModels();
            if (list.Count() > 0)
            {
                model.Route.ListElement = list;
            }
            if (thisTripId != null && dbcontext.Trips.Find(thisTripId) != null)
            {
                model.Route.Id_Trip = (int)thisTripId;
            }

            // list that has every coach in database
            var listOfCoaches = dbcontext.Coaches.ToList();

            var currDate = DateTime.Now;

            // go through every trip in db that is in progress atm. A coach assigned to that trip will be removed from our list, so it cant be assigned
            //to this currently edited trip
            foreach (var coach in dbcontext.Coaches.ToList())
            {
                foreach (var tripInstance in dbcontext.Trips.ToList())
                {
                    if (tripInstance.DateDeparture < model.TripInstance.DateDeparture && model.TripInstance.DateDeparture < tripInstance.DateBack)
                    {
                        if (coach.Id == tripInstance.CoachNumberId)
                        {
                            listOfCoaches.Remove(coach);
                        }
                    }
                }
            }
            ViewBag.DateDeparture = model.TripInstance.DateDeparture;
            ViewBag.DateBack      = model.TripInstance.DateBack;

            model.CoachVehicleIdList = new SelectList(listOfCoaches, "Id", "VehicleNumber");

            return(View(model));
        }