using Microsoft.AspNetCore.Mvc;

using MVCWebApp.Models;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Reflection;

using Utilities;

 

namespace MVCWebApp.Controllers

{

    public class CarsController : Controller

    {

       

        public IActionResult DisplayCarInventory()

        {

            List<Car> cars = new List<Car>();

            Car car;

 

            DBConnect objDB = new DBConnect();

            DataSet ds = objDB.GetDataSet("SELECT * FROM Cars");

 

            foreach (DataRow record in ds.Tables[0].Rows)

            {

                car = new Car(record["Make"].ToString(),

                                record["Model"].ToString(),

                                int.Parse(record["Year"].ToString()),

                                double.Parse(record["Value"].ToString()),

                                record["VIN"].ToString());

                car.ImageUrl = record["ImageUrl"].ToString();

                cars.Add(car);

            }

 

            // Return the Razor View "DisplayCarInventory.cshtml"

            // and deliver the List<Car> as the View's Model

            return View(cars);

        }

 

        //[HttpGet("Cars/ViewCarDetails/{vin}")]  // route: Cars/ViewCarDetails/vin

        public IActionResult ViewCarDetails(string vin)     // route: route?vin=value

        {

            Car car = new Car();

 

            DBConnect objDB = new DBConnect();

            SqlCommand command = new SqlCommand();

            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "GetCarsByVIN";

 

            command.Parameters.AddWithValue("@theVIN", vin);

 

            DataSet ds = objDB.GetDataSet(command);

 

            if (ds.Tables[0].Rows.Count > 0)

            {

                DataRow record = ds.Tables[0].Rows[0];

                car.Make = record["Make"].ToString();

                car.Model = record["Model"].ToString();

                car.Year = int.Parse(record["Year"].ToString());

                car.Color = record["Color"].ToString();

                car.Value = double.Parse(record["Value"].ToString());

                car.ImageUrl = record["ImageUrl"].ToString();

                car.VIN = record["VIN"].ToString();

            }

 

            // Return the Razor View "ViewCarDetails.cshtml"

            return View(car);

        }

 

        [HttpPost]

        public IActionResult SearchByMake(string make)

        {

            List<Car> cars = new List<Car>();

            Car car;

 

            DBConnect objDB = new DBConnect();

            SqlCommand command = new SqlCommand();

            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "GetCarsByMaker";

 

            command.Parameters.AddWithValue("@theMake", make);

            DataSet ds = objDB.GetDataSet(command);

 

            foreach (DataRow record in ds.Tables[0].Rows)

            {

                car = new Car(record["Make"].ToString(),

                                record["Model"].ToString(),

                                int.Parse(record["Year"].ToString()),

                                double.Parse(record["Value"].ToString()),

                                record["VIN"].ToString());

                cars.Add(car);

            }

 

            // Return the Razor View "DisplayCarInventory.cshtml"

            // and deliver the List<Car> search results as the View's Model

            return View("DisplayCarInventory", cars);

        }       

    }

}