/* Helper Methods below: */


        private void PrepareCarsForViewModel(ReportStartupViewModel model)
        {
            ICollection<Car> cars = _db.Cars.Where(c => c.Active).ToList();
            ReportDAL rDB = new ReportDAL(_db);
            // User doesn't need to select cars if there is only one available
            int? latestOdometerStatus = null;
            if (cars.Count == 1)
            {
                Car selectedCar = cars.FirstOrDefault();
                model.SelectedCarRegistrationNumber = selectedCar.RegistrationNumber;
                model.SelectedCarId = selectedCar.Id;

                latestOdometerStatus = rDB.GetLatestOdometerStatus(selectedCar.Id); ;
            }
            else
            {
                latestOdometerStatus = rDB.GetLatestOdometerStatus(cars.ToArray()[0].Id);
                model.AvailableCars = new SelectList(cars, "Id", "RegistrationNumber");
            }

            if (latestOdometerStatus != null)
                model.OdometerStart = (int)latestOdometerStatus;
        }
        public ActionResult Startup(ReportStartupViewModel model)
        {
            if (ModelState.IsValid)
            {
                ReportDAL dal = new ReportDAL(_db);


                string currUserId = User.Identity.GetUserId(); // Calling GetUserId() within a Lambda-expression causes errors

                int? lastOdometerStatus = dal.GetLatestOdometerStatus(model.SelectedCarId);
                if ( lastOdometerStatus != null && model.OdometerStart < lastOdometerStatus ) {
                    ViewBag.OdometerError = String.Format(AppString.ErrorOdometerStart, lastOdometerStatus-1);
                    PrepareCarsForViewModel(model);
                    PrepareProjectsForViewModel(model, null);
                    return View(model);
                }

                Report report = new Report()
                {
                    Date = DateTime.Now,
                    OdometerStart = model.OdometerStart,
                    AssociatedCar = _db.Cars.Single(c => c.Id == model.SelectedCarId),
                    AssociatedProject = _db.Projects.Single(p => p.Id == model.SelectedProjectId),
                    AssociatedUser = _db.Users.Single(u => u.Id == currUserId),
                    FromLocation = model.FromLocation
                };

                // TODO: Perform checks on the received data to make sure that there isn't any user shenanigans going on. For example if a project id was passed that the user isn't associated with.

                _db.Reports.Add(report);
                _db.SaveChanges();

                return RedirectToAction("Complete", "Report", new { reportStarted = true });
            }

            return View(model);
        }
        public JsonResult LastRegisteredOdometerStatus(int carId)
        {
            ReportDAL dal = new ReportDAL(_db);

            int? oStatus = dal.GetLatestOdometerStatus(carId);

            if (oStatus != null)
                return Json(oStatus.ToString(), JsonRequestBehavior.AllowGet);
            else
                return Json("", JsonRequestBehavior.AllowGet);
        }