//authorize section on milstone 4 lesson 11
        //[Authorize(Roles = "admin")]
        public ActionResult AddVehicle()
        {
            var model = new AddVehicleViewModel();

            model.SetMakeType(_carDealer.GetAllCarMakes());
            model.SetModelType(_carDealer.GetAllCarModels());
            model.SetBodyType(_carDealer.GetBodyStyles());
            model.SetVehicleType(_carDealer.GetVehicleType());
            model.SetTransmissionType(_carDealer.GetTransmissions());
            model.SetColorOptions();
            model.SetInteriorTypes();
            //throw new Exception();

            return(View(model));
        }
        public ActionResult AddVehicle(AddVehicleViewModel model)
        {
            VehicleDetails vehicleAdded = new VehicleDetails();
            var            localhost    = "http://localhost:50822/Images/";

            if (string.IsNullOrEmpty(model.Vehicle.Description))
            {
                ModelState.AddModelError("Description", "Vehicle must have some sort of description.");
            }
            if (/*string.IsNullOrEmpty(localhost) &&*/ model.VehicleImage == null)
            {
                ModelState.AddModelError("VehicleImage", "Image required for vehicle!");
            }

            if (model.Vehicle.VIN == null || model.Vehicle.VIN.Length != 17)
            {
                ModelState.AddModelError("VIN", "Vehicle VIN numbers must be 17 digits long!");
            }

            if (model.Vehicle.MSRP < vehicleAdded.SalePrice)
            {
                ModelState.AddModelError("SalePrice", "Sale Price can not be larger than MSRP!");
            }

            if (model.VehicleImage != null && model.VehicleImage.ContentLength > 0)
            {
                var savePath = Server.MapPath("~/Images");

                string fileName  = Path.GetFileName(model.VehicleImage.FileName);
                string extension = Path.GetExtension(model.VehicleImage.FileName);

                var filePath = Path.Combine(savePath, fileName + extension);

                int counter = 1;
                while (System.IO.File.Exists(filePath))
                {
                    filePath = Path.Combine(savePath, fileName + counter.ToString() + extension);
                    counter++;
                }

                model.VehicleImage.SaveAs(filePath);
            }


            if (ModelState.IsValid)
            {
                //VehicleDetails vehicleAdded = new VehicleDetails();
                vehicleAdded.Color            = model.Vehicle.Color;
                vehicleAdded.Interior         = model.Vehicle.Interior;
                vehicleAdded.BodyName         = model.Vehicle.BodyName;
                vehicleAdded.Description      = model.Vehicle.Description;
                vehicleAdded.Mileage          = model.Vehicle.Mileage;
                vehicleAdded.ModelName        = model.Vehicle.ModelName;
                vehicleAdded.TransmissionType = model.Vehicle.TransmissionType;
                vehicleAdded.VehicleType      = model.Vehicle.VehicleType;
                vehicleAdded.VIN       = model.Vehicle.VIN;
                vehicleAdded.Year      = model.Vehicle.Year;
                vehicleAdded.MSRP      = model.Vehicle.MSRP;
                vehicleAdded.SalePrice = model.Vehicle.SalePrice;
                vehicleAdded.ModelName.MakeName.MakeID = model.Vehicle.ModelName.MakeName.MakeID;
                vehicleAdded.ImageUrl = localhost + model.VehicleImage.FileName;

                _carDealer.CreateVehicle(model);
            }

            else
            {
                model.SetMakeType(_carDealer.GetAllCarMakes());
                model.SetModelType(_carDealer.GetAllCarModels());
                model.SetBodyType(_carDealer.GetBodyStyles());
                model.SetVehicleType(_carDealer.GetVehicleType());
                model.SetTransmissionType(_carDealer.GetTransmissions());
                model.SetColorOptions();
                model.SetInteriorTypes();

                return(View(model));
            }
            return(RedirectToAction("Vehicles"));
        }