public IActionResult SubmitOrder(StoreViewModel model)
        {
            TempData.Keep("StoreID");
            int storeID = (int)TempData["StoreID"];

            try {
                _ = userLoggedIn;
            } catch (NullReferenceException) {
                model.ReasonForError = "You are not logged into the system. You will only be able to view menus until you return to the main page and log in.";
                return(View("Visit", model));
            }

            StoreModel store = _repo.GetStore(storeID);

            model.StoreName = store.Name;
            // reference needs to be re-established if an error occurs submitting the order
            List <SelectListItem> c = new List <SelectListItem>();

            foreach (CrustModel crust in _repo.GetCrusts())
            {
                c.Add(new SelectListItem {
                    Text = crust.Name, Value = crust.ID.ToString()
                });
            }
            model.Crusts = c;

            bool submitOrderClicked    = Request.Form["SubmitOrder"].ToString() != "";
            bool addCustomPizzaClicked = Request.Form["AddCustom"].ToString() != "";
            bool backButtonClicked     = Request.Form["Back"].ToString() != "";
            int  buttonsClicked        = (submitOrderClicked ? 1 : 0) + (addCustomPizzaClicked ? 1 : 0) + (backButtonClicked ? 1 : 0);

            if (buttonsClicked > 1)
            {
                Console.WriteLine("Multiple buttons registered as clicked on the menu page");
                model.ReasonForError = "There was a problem processing your request. Please try again.";
                return(View("Visit", model));
            }
            else if (submitOrderClicked)
            {
                Tuple <int, string> userCanOrder = _repo.UserCanOrder(storeID, userLoggedIn);
                if (userCanOrder.Item1 == 1)
                {
                    model.ReasonForError = $"You cannot place another order at any store for {userCanOrder.Item2}";
                    return(View("Visit", model));
                }
                else if (userCanOrder.Item1 == 2)
                {
                    model.ReasonForError = $"You cannot place another order at this store for {userCanOrder.Item2}";
                    return(View("Visit", model));
                }

                decimal overallCost     = 0.00M;
                int     overallQuantity = 0;

                int max = _repo.GetNextOrderNumber();

                bool noIssues = true;
                foreach (CheckModel selectedPizza in model.Menu)
                {
                    if (selectedPizza.Checked)
                    {
                        string size = selectedPizza.SelectedSize.ToString().ToLower();
                        if (Enum.IsDefined(typeof(Size), size))
                        {
                            model.ReasonForError = $"Invalid size on pizza {selectedPizza.Name}";
                            return(View("Visit", model));
                        }
                        if (selectedPizza.Quantity == 0)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} pizza must have a quantity greater than 0 if selected to be ordered";
                            return(View("Visit", model));
                        }
                        else if (selectedPizza.Quantity < 0)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} pizza must have a positive quantity greater";
                            return(View("Visit", model));
                        }

                        int        crustID;
                        CrustModel crust = null;
                        if (int.TryParse(selectedPizza.SelectedCrust, out crustID))
                        {
                            crust = _repo.GetCrust(crustID);
                        }
                        if (crust == null)
                        {
                            model.ReasonForError = $"No crust was selected on the {selectedPizza.Name} pizza. Please try selecting a different crust.";
                            return(View("Visit", model));
                        }

                        PizzaModel pizza;
                        if (selectedPizza.ID != 0)
                        {
                            pizza = _repo.GetPizza(selectedPizza.ID);
                        }
                        else
                        {
                            pizza = new PizzaModel {
                                Cost = 20.00M
                            };
                        }
                        if (pizza == null)
                        {
                            Console.WriteLine($"Unknown pizza with ID {selectedPizza.ID} submitted; skipping");
                            continue;
                        }
                        decimal costOfThesePizzas = pizza.Cost * (decimal)selectedPizza.Quantity;
                        string  toppingIDs        = "";
                        int     toppingCount      = 0;
                        foreach (ToppingViewModel topping in selectedPizza.SelectedToppings)
                        {
                            if (topping.IsSelected)
                            {
                                toppingIDs += $"{topping.ID},";
                                toppingCount++;
                            }
                        }
                        if (toppingCount > 5)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} has more than 5 toppings selected. Please uncheck some toppings on this pizza.";
                            return(View("Visit", model));
                        }
                        else if (toppingCount < 2)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} needs at least 2 toppings selected. Please add some more toppings on this pizza.";
                            return(View("Visit", model));
                        }
                        toppingIDs = toppingIDs.Substring(0, toppingIDs.Length - 1);


                        noIssues &= _repo.AddOrder(new OrderModel {
                            OrderID   = max + 1,
                            StoreID   = storeID,
                            PizzaID   = pizza.ID,
                            UserID    = userLoggedIn,
                            Created   = DateTime.Now,
                            Quantity  = selectedPizza.Quantity,
                            TotalCost = costOfThesePizzas,
                            Size      = selectedPizza.SelectedSize.ToString(),
                            CrustID   = crust.ID,
                            Toppings  = toppingIDs
                        });
                        overallCost     += costOfThesePizzas;
                        overallQuantity += selectedPizza.Quantity;
                    }
                }
                if (overallCost > 250.00M)
                {
                    model.ReasonForError = "This order exceeds $250. Please remove some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (overallQuantity > 50)
                {
                    model.ReasonForError = "This order exceeds 50 pizzas. Please remove some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (overallQuantity == 0)
                {
                    model.ReasonForError = "There are no pizzas in this order. Please add some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (!noIssues)
                {
                    model.ReasonForError = "There was a problem adding some pizzas to your order";
                }

                return(View("Submitted"));
            }
            else if (addCustomPizzaClicked)
            {
                List <ToppingModel> toppings         = _repo.GetToppings();
                ToppingViewModel[]  toppingsSelected = new ToppingViewModel[toppings.Count()];
                for (int i = 0; i < toppingsSelected.Length; i++)
                {
                    ToppingModel topping = toppings[i];
                    toppingsSelected[i] = new ToppingViewModel {
                        ID = topping.ID, Name = topping.Name, IsSelected = false
                    };
                }
                model.Menu.Add(new CheckModel {
                    ID = 0, Name = "Custom", Checked = true, Cost = 20.00M, DefaultCrust = 0, SelectedToppings = toppingsSelected
                });
                return(View("Visit", model));
            }
            else if (backButtonClicked)
            {
                return(Redirect("/User/StoreSelection"));
            }
            else // no buttons check is placed down here to remove the 'not all code paths return a value' error
            {
                Console.WriteLine("Request was sent but no buttons registered as clicked");
                model.ReasonForError = "There was a problem processing your request. Please try again.";
                return(View("Visit", model));
            }
        }