// GET : DeviceModel/Add/
        public ActionResult Add(string dealerId)
        {
            // TMSEntities db = new TMSEntities();
            //List<ProtocolServerViewModel> protocol = new List<ProtocolServerViewModel>();
            DataTable dt = Data.GetData(DataBase.Api, CommandType.StoredProcedure, "Report_ProtocolServers");
            List<ProtocolServerViewModel> Server = new List<ProtocolServerViewModel>();
            if (dt != null || dt.Rows.Count > 0)
            {
                Server = dt.AsEnumerable().Select(m => new ProtocolServerViewModel()
                {
                    ProtocolServer = Convert.ToString(m["ProtocolServer"]),

                }).ToList();
            }
            DeviceModelViewModel dealerVM = new DeviceModelViewModel();
            if (Session["UserData"] is Admin)
            {
                var deviceTypes = Server.ToList();
                var deviceTypesSelectList =
                    new SelectList(
                        deviceTypes.Select(
                            dist => new SelectListItem() { Text = dist.ProtocolServer, Value = dist.ProtocolServer }), "Value",
                        "Text");

                ViewData["DeviceTypeList"] = deviceTypesSelectList;
                var currentUser = Session["UserData"] as Admin;
                var distributors = GetDistributors(currentUser.UserId.ToString());
                var distributorsSelectList =
                    new SelectList(
                        distributors.Select(
                            dist => new SelectListItem() { Text = dist.Username, Value = dist.UserId.ToString() }), "Value",
                        "Text");

                ViewData["DistributorList"] = distributorsSelectList;
                List<Dealer> dealers = new List<Dealer>();
                SelectList dealersSelectList;
                if (distributors.FirstOrDefault() != null)
                {
                    dealers = GetDealers(distributors.First().UserId.ToString());
                    dealersSelectList =
                         new SelectList(
                             dealers.Select(
                                 deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value",
                             "Text");
                }
                else
                {
                    dealersSelectList = new SelectList(new List<SelectListItem>() { new SelectListItem() { Text = "Select", Value = "0" } }, "Value", "Text");
                }

                ViewData["DealerList"] = dealersSelectList;
                SelectList customersSelectList;

                if (dealers.FirstOrDefault() != null)
                {
                    //  var currentUser = Session["UserData"] as Admin;
                    var customers = GetCustomers(dealers.First().UserId.ToString());
                    customersSelectList =
                     new SelectList(
                         customers.Select(
                             cust => new SelectListItem() { Text = cust.Username, Value = cust.UserId.ToString() }), "Value",
                         "Text");
                }
                else
                {
                    customersSelectList = new SelectList(new List<SelectListItem>() { new SelectListItem() { Text = "Select", Value = "0" } }, "Value", "Text");
                }

                ViewData["CustomerList"] = customersSelectList;

                if (string.IsNullOrWhiteSpace(dealerId))
                {
                    dealerVM.DeviceId = Guid.Empty;

                }
                else
                {
                    var dealerGuid = Guid.Parse(dealerId);
                    DeviceModels customer = devicemodelService.GetDevice(dealerGuid);
                    dealerVM = Mapper.Map<DeviceModels, DeviceModelViewModel>(customer);
                    dealerVM.ToExpiry = 0;
                }
            }

            return PartialView("_Add", dealerVM);
        }
        public ActionResult Add(DeviceModelViewModel devceModel)
        {
            if (Session["UserData"] is Admin)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        if (devceModel.DeviceId == Guid.Empty)
                        {
                            devceModel.DeviceId = Guid.NewGuid();
                            var deal = Mapper.Map<DeviceModelViewModel, DeviceModels>(devceModel);
                            //     deal.parent = Guid.Parse(Request.Form["CustomerId"]);
                            deal.UserId = Guid.Parse(Request.Form["DeviceUserId"]);
                            deal.ExpiryDate = DateTime.UtcNow.AddDays(deal.ToExpiry);
                            deal.TimeZone = Request.Form["DropDownTimezone"];
                            deal.VehicleType = Request.Form["VehicleType"];
                            devicemodelService.CreateDevice(deal);
                            devicemodelService.SaveDevice();
                        }
                        else
                        {
                            var customer = devicemodelService.GetDevice(devceModel.DeviceId);
                            Mapper.Map<DeviceModelViewModel, DeviceModels>(devceModel, customer);
                            customer.UserId = Guid.Parse(Request.Form["DeviceUserId"]);
                            customer.TimeZone = Request.Form["DropDownTimezone"];
                            customer.VehicleType = Request.Form["VehicleType"];
                            devicemodelService.UpdateDevice(customer);
                            devicemodelService.SaveDevice();
                        }

                        return Json(new { Result = "Success" });
                    }
                    catch (Exception ex)
                    {

                    }
                }
                DataTable dt = Data.GetData(DataBase.Api, CommandType.StoredProcedure, "Report_ProtocolServers");
                List<ProtocolServerViewModel> Server = new List<ProtocolServerViewModel>();
                if (dt != null || dt.Rows.Count > 0)
                {
                    Server = dt.AsEnumerable().Select(m => new ProtocolServerViewModel()
                    {
                        ProtocolServer = Convert.ToString(m["ProtocolServer"]),

                    }).ToList();
                }
                var deviceTypes = Server.ToList();
                var deviceTypesSelectList =
                    new SelectList(
                        deviceTypes.Select(
                            dist => new SelectListItem() { Text = dist.ProtocolServer, Value = dist.ProtocolServer }), "Value",
                        "Text");

                ViewData["DeviceTypeList"] = deviceTypesSelectList;
                var currentUser = Session["UserData"] as Admin;
                var distributors = GetDistributors(currentUser.UserId.ToString());
                var distributorsSelectList =
                    new SelectList(
                        distributors.Select(
                            dist => new SelectListItem() { Text = dist.Username, Value = dist.UserId.ToString() }), "Value",
                        "Text");

                ViewData["DistributorList"] = distributorsSelectList;
                var dealers = GetDealers(distributors.First().UserId.ToString());
                var dealersSelectList =
                    new SelectList(
                        dealers.Select(
                            deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value",
                        "Text");

                ViewData["DealerList"] = dealersSelectList;
                SelectList customersSelectList;
                if (dealers.FirstOrDefault() != null)
                {
                    //  var currentUser = Session["UserData"] as Admin;
                    var customers = GetCustomers(dealers.First().UserId.ToString());
                    customersSelectList =
                     new SelectList(
                         customers.Select(
                             cust => new SelectListItem() { Text = cust.Username, Value = cust.UserId.ToString() }), "Value",
                         "Text");
                }
                else
                {
                    customersSelectList = new SelectList(new List<SelectListItem>() { new SelectListItem() { Text = "Select", Value = "0" } }, "Value", "Text");
                }
                ViewData["CustomerList"] = customersSelectList;

            }
            return PartialView("_Add", devceModel);
        }