// GET: /User/
        public ActionResult Index(int? page = 1)
        {
            Logger.Debug("Inside Territory Controller- Index");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    var regionList = new RegionViewModel();
                    regionList.Region = new List<RegionModel>();
                    var appRegion = _IRegionRepository.GetRegionByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    foreach (var region in appRegion.ToList())
                    {
                        regionList.Region.Add(new RegionModel { Name = region.Name, Description = region.Description, OrganizationGUID = region.OrganizationGUID != null ? region.OrganizationGUID.ToString() : Guid.Empty.ToString() });
                    }
                    int pageSize = 10;
                    int pageNumber = (page ?? 1);
                    return View(regionList.Region.ToPagedList(pageNumber, pageSize).AsEnumerable());
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("Login", "User");

            }
        }
        //
        // GET: /ServicePoint/
        public ActionResult Index(string id = "", string territoryguid = "")
        {
            Logger.Debug("Inside People Controller- Index");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    DropdownValues();
                    var regionList = new RegionViewModel();
                    regionList.Region = new List<RegionModel>();

                    var appRegion = _IRegionRepository.GetRegionByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();

                    regionList.Region.Add(new RegionModel { Name = "All Regions", RegionGUID = "", Description = "", OrganizationGUID = "" });
                    foreach (var region in appRegion.ToList())
                    {
                        regionList.Region.Add(new RegionModel { Name = region.Name, RegionGUID = region.RegionGUID.ToString(), Description = region.Description, OrganizationGUID = region.OrganizationGUID != null ? region.OrganizationGUID.ToString() : Guid.Empty.ToString() });
                    }

                    var territoryList = new TerritoryViewModel();
                    territoryList.Territory = new List<TerritoryModel>();
                    var appTerritory = new List<Territory>();
                    if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString())
                    {
                        appTerritory = _ITerritoryRepository.GetTerritoryByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    }
                    else
                    {
                        appTerritory = _ITerritoryRepository.GetTerritoryByRegionGUID(new Guid(id)).ToList();
                    }
                    territoryList.Territory.Add(new TerritoryModel { Name = "All Territories", TerritoryGUID = "", RegionGUID = "", Description = "", OrganizationGUID = "" });
                    foreach (var territory in appTerritory.ToList())
                    {
                        territoryList.Territory.Add(new TerritoryModel { Name = territory.Name, RegionGUID = territory.RegionGUID != null ? territory.RegionGUID.ToString() : Guid.Empty.ToString(), TerritoryGUID = territory.TerritoryGUID.ToString(), Description = territory.Description, OrganizationGUID = territory.OrganizationGUID != null ? territory.OrganizationGUID.ToString() : Guid.Empty.ToString() });
                    }

                    var marketList = new ServicePointViewModel();
                    marketList.MarketList = new List<ServicePointModel>();
                    var appMarket = new List<Market>();
                    if (string.IsNullOrEmpty(id) && string.IsNullOrEmpty(territoryguid))
                    {
                        // if (Session["UserType"].ToString() == "ENT_A")
                        {
                            appMarket = _IMarketRepository.GetMarketByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString()), 0).ToList();
                        }
                    }
                    else if (!string.IsNullOrEmpty(id) && string.IsNullOrEmpty(territoryguid))
                    {
                        appMarket = _IMarketRepository.GetMarketByRegionGUID(new Guid(id), 0).ToList();
                        ViewBag.TerritoryGUID = id;
                    }
                    else if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(territoryguid))
                    {
                        appMarket = _IMarketRepository.GetMarketByRegionGUIDandTerritoryGUID(new Guid(territoryguid), new Guid(id), 0).ToList();
                        ViewBag.RegionGUID = territoryguid;
                        ViewBag.TerritoryGUID = id;
                    }
                    foreach (var market in appMarket.ToList())
                    {
                        marketList.MarketList.Add(new ServicePointModel
                        {
                            MarketGUID = market.MarketGUID.ToString(),
                            UserGUID = market.UserGUID.ToString(),
                            OrganizationGUID = market.OrganizationGUID.ToString(),
                            OwnerGUID = market.OwnerGUID.ToString(),
                            MarketName = market.MarketName,
                            MarketPhone = market.MarketPhone,
                            PrimaryContactGUID = market.PrimaryContactGUID.ToString(),
                            FirstName = market.FirstName,
                            LastName = market.LastName,
                            MobilePhone = market.MobilePhone,
                            HomePhone = market.HomePhone,
                            Emails = market.Emails,
                            AddressLine1 = market.AddressLine1,
                            AddressLine2 = market.AddressLine2,
                            City = market.City,
                            State = market.State,
                            Country = market.Country,
                            ZipCode = market.ZipCode,
                            RegionGUID = market.RegionGUID.ToString(),
                            TerritoryGUID = market.TerritoryGUID.ToString(),
                            RegionName = market.RegionGUID != null ? _IRegionRepository.GetRegionNameByRegionGUID(new Guid(market.RegionGUID.ToString())) : "",
                            TerritoryName = market.TerritoryGUID != null ? _ITerritoryRepository.GetTerritoryNameByTerritoryGUID(new Guid(market.TerritoryGUID.ToString())) : "",
                        });
                    }

                    var viewModel = new ServicePointViewModel();
                    viewModel.RegionModel = regionList.Region.AsEnumerable();
                    viewModel.TerritoryModel = territoryList.Territory.AsEnumerable();
                    viewModel.MarketModel = marketList.MarketList.AsEnumerable();

                    return View(viewModel);
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("Login", "User");
            }
        }
        //public UserController(WorkersInMotionJobContext context)
        //{
        // //   this._IJobRepository = new JobRepository(context);
        //}

        #endregion
        public ActionResult Index(string id = "", string regionguid = "", string selection = "")
        {
            Logger.Debug("Inside User Controller- Index");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    if (!string.IsNullOrEmpty(id))
                    {
                        TempData["TabName"] = id;
                    }
                    else
                    {
                        TempData["TabName"] = "Details";
                    }
                    mycompany pmycompany = new mycompany();
                    pmycompany.OrganizationEditView = new OrganizationEditView();
                    pmycompany.AspNetUserViewModel = new AspNetUserViewModel();
                    pmycompany.TerritoryViewModel = new TerritoryViewModel();
                    pmycompany.RegionViewModel = new RegionViewModel();


                    //switch (id)
                    //{
                    //    case "Users":
                    //        ViewBag.TabName = "Users";
                    var userList = new AspNetUserViewModel();
                    userList.Users = new List<AspUser>();
                    var appUser = new List<UserProfile>();

                    appUser = _IUserProfileRepository.GetUserProfilesbyOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).OrderBy(sort => sort.FirstName).ToList();

                    foreach (var user in appUser.ToList())
                    {
                        UserSubscription userSubscription = _IUserSubscriptionRepository.GetUserSubscriptionByUserID(user.UserGUID);
                        GlobalUser _globalUser = _IGlobalUserRepository.GetGlobalUserByID(user.UserGUID);
                        string Regionname = string.Empty;
                        string Territoryname = string.Empty;
                        string Groupname = string.Empty;
                        string userType = string.Empty;
                        if (_globalUser != null)
                        {
                            OrganizationUsersMap _orgUserMap = _IOrganizationRepository.GetOrganizationUserMapByUserGUID(user.UserGUID);

                            if (_orgUserMap != null && _orgUserMap.RegionGUID != null)
                            {
                                Regionname = _IRegionRepository.GetRegionNameByRegionGUID(new Guid(_orgUserMap.RegionGUID.ToString()));
                            }
                            else
                            {
                                Regionname = "";
                            }
                            if (_orgUserMap != null && _orgUserMap.TerritoryGUID != null)
                            {
                                Territoryname = _ITerritoryRepository.GetTerritoryNameByTerritoryGUID(new Guid(_orgUserMap.TerritoryGUID.ToString()));
                            }
                            else
                            {
                                Territoryname = "";
                            }


                            // Groupname = _IGroupRepository.GetGroupNameByGroupGUID(new Guid(_globalUser.GroupGUID.ToString()));
                            userType = _IGlobalUserRepository.GetUserTypeByRoleID(_globalUser.Role_Id);
                        }
                        string UserTypeName = _IGlobalUserRepository.GetUserRoleName(user.UserGUID);

                        if (userSubscription != null)
                            userList.Users.Add(new AspUser { UserTypeName = UserTypeName, RegionName = Regionname, TerritoryName = Territoryname, GroupName = Groupname, OrganizationSubscriptionGUID = userSubscription.OrganizationSubscriptionGUID.ToString(), UserType = userType, IsActive = userSubscription.IsActive, SubscriptionGUID = userSubscription.UserSubscriptionGUID.ToString(), FirstName = user.FirstName, LastName = user.LastName, Id = user.UserGUID.ToString(), EmailID = user.EmailID, MobilePhone = user.MobilePhone, City = user.City, State = user.State, Country = user.Country });
                    }

                    OrganizationSubscription orgSubscription = new OrganizationSubscription();
                    if (Session["UserType"] != null && Session["UserType"].ToString() != "WIM_A")
                    {
                        orgSubscription = _IOrganizationSubscriptionRepository.GetOrganizationSubscriptionByOrgID(new Guid(Session["OrganizationGUID"].ToString()));
                        if (orgSubscription != null)
                        {
                            ViewBag.EnableCreateUserButton = "true";
                        }
                        else
                        {
                            ViewBag.EnableCreateUserButton = "false";
                        }
                        //if (orgSubscription != null && orgSubscription.SubscriptionPurchased > orgSubscription.SubscriptionConsumed)
                        //{
                        //    ViewBag.EnableCreateUserButton = "true";
                        //}
                        //else
                        //{
                        //    ViewBag.EnableCreateUserButton = "false";
                        //}
                    }
                    else
                    {
                        ViewBag.EnableCreateUserButton = "false";
                    }
                    pmycompany.AspNetUserViewModel = userList;
                    //    break;
                    //case "Region":
                    //    ViewBag.TabName = "Regions";
                    var territoryList = new TerritoryViewModel();
                    territoryList.Territory = new List<TerritoryModel>();
                    List<Territory> appTerritory = new List<Territory>();
                    if (!string.IsNullOrEmpty(regionguid) && regionguid != Guid.Empty.ToString())
                    {
                        appTerritory = _ITerritoryRepository.GetTerritoryByRegionGUID(new Guid(regionguid)).ToList();
                        TempData["TabName"] = "Markets";
                        ViewBag.AddTerritory = "true";
                        ViewBag.Id = regionguid;
                    }
                    else
                    {
                        appTerritory = _ITerritoryRepository.GetTerritoryByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    }
                    foreach (var territory in appTerritory.ToList())
                    {
                        territoryList.Territory.Add(new TerritoryModel { Name = territory.Name, TerritoryGUID = territory.TerritoryGUID.ToString(), RegionGUID = territory.RegionGUID != null ? territory.RegionGUID.ToString() : Guid.Empty.ToString(), Description = territory.Description, OrganizationGUID = territory.OrganizationGUID != null ? territory.OrganizationGUID.ToString() : Guid.Empty.ToString() });
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<div class='actions'>");
                    sb.Append("<div class='btn-group'>");
                    if (!string.IsNullOrEmpty(regionguid) && regionguid != Guid.Empty.ToString())
                    {
                        sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i> " + _IRegionRepository.GetRegionNameByRegionGUID(new Guid(regionguid)) + " <i class='icon-angle-down'></i></a>");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(selection) && selection == "All")
                        {
                            sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i>All<i class='icon-angle-down'></i></a>");
                        }
                        else
                        {
                            sb.Append("<a href='#' id='ulaworkergroup' class='btn green' data-toggle='dropdown'><i class='icon-map-marker'></i> Select Region <i class='icon-angle-down'></i></a>");
                        }
                    }
                    sb.Append("<ul id='ulworkgroup' style='height:100px;overflow-y:scroll' class='dropdown-menu pull-right'>");
                    if (string.IsNullOrEmpty(selection) || selection != "All")
                    {
                        sb.Append("<li><a href=" + Url.Action("Index", "MyCompany", new { id = "Markets", selection = "All" }) + ">All</a></li>");
                    }
                    List<Region> RegList = _IRegionRepository.GetRegionByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    foreach (Region item in RegList)
                    {
                        sb.Append("<li><a href=" + Url.Action("Index", "MyCompany", new { regionguid = item.RegionGUID.ToString() }) + " data-groupguid=" + item.RegionGUID + ">" + item.Name + "</a></li>");
                    }
                    sb.Append("</ul>");
                    sb.Append("</div>");
                    sb.Append("</div>");
                    ViewBag.RegionList = sb.ToString();
                    pmycompany.TerritoryViewModel = territoryList;
                    //    break;
                    //case "Territory":
                    //    ViewBag.TabName = "Territories";
                    var regionList = new RegionViewModel();
                    regionList.Region = new List<RegionModel>();
                    var appRegion = _IRegionRepository.GetRegionByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
                    foreach (var region in appRegion.ToList())
                    {
                        regionList.Region.Add(new RegionModel { Name = region.Name, RegionGUID = region.RegionGUID.ToString(), Description = region.Description, OrganizationGUID = region.OrganizationGUID != null ? region.OrganizationGUID.ToString() : Guid.Empty.ToString() });
                    }
                    pmycompany.RegionViewModel = regionList;
                    //    break;
                    //case "Organization":
                    //    ViewBag.TabName = "Details";
                    OrganizationEditView organization = new OrganizationEditView();
                    organization.OrganizationGUID = new Guid(Session["OrganizationGUID"].ToString());
                    Organization Organization = _IOrganizationRepository.GetOrganizationByID(organization.OrganizationGUID);
                    if (organization != null)
                    {
                        organization.OrganizationFullName = Organization.OrganizationFullName;
                        organization.OrganizationName = Organization.OrganizationFullName.Trim();
                        organization.OrganizationGUID = Organization.OrganizationGUID;
                        organization.Website = Organization.Website;
                        organization.Phone = Organization.Phone;
                        organization.TimeZone = Organization.TimeZone.ToString();
                        organization.AddressLine1 = Organization.AddressLine1;
                        organization.AddressLine2 = Organization.AddressLine2;
                        organization.ImageURL = Organization.ImageURL;
                        organization.City = Organization.City;
                        organization.Country = Organization.Country;
                        organization.State = Organization.State;
                        organization.ZipCode = Organization.ZipCode;
                        organization.EmailID = Organization.EmailID;
                        organization.IsActive = Organization.IsActive;
                        organization.IsDeleted = Organization.IsDeleted;
                        organization.CreatedDate = Organization.CreateDate;
                        organization.CreateBy = Organization.CreateBy;
                        pmycompany.OrganizationEditView = organization;
                    }

                    //    break;
                    //default:
                    //    ViewBag.TabName = "Details";
                    //    OrganizationEditView porganization = new OrganizationEditView();
                    //    porganization.OrganizationGUID = new Guid(Session["OrganizationGUID"].ToString());
                    //    Organization pOrganization = _IOrganizationRepository.GetOrganizationByID(porganization.OrganizationGUID);
                    //    if (porganization != null)
                    //    {
                    //        porganization.OrganizationFullName = pOrganization.OrganizationFullName;
                    //        porganization.OrganizationName = pOrganization.OrganizationFullName.Trim();
                    //        porganization.OrganizationGUID = pOrganization.OrganizationGUID;
                    //        porganization.Website = pOrganization.Website;
                    //        porganization.Phone = pOrganization.Phone;
                    //        porganization.TimeZone = pOrganization.TimeZone.ToString();
                    //        porganization.AddressLine1 = pOrganization.AddressLine1;
                    //        porganization.AddressLine2 = pOrganization.AddressLine2;
                    //        porganization.ImageURL = pOrganization.ImageURL;
                    //        porganization.City = pOrganization.City;
                    //        porganization.Country = pOrganization.Country;
                    //        porganization.State = pOrganization.State;
                    //        porganization.ZipCode = pOrganization.ZipCode;
                    //        porganization.EmailID = pOrganization.EmailID;
                    //        porganization.IsActive = pOrganization.IsActive;
                    //        porganization.IsDeleted = pOrganization.IsDeleted;
                    //        porganization.CreatedDate = pOrganization.CreateDate;
                    //        porganization.CreateBy = pOrganization.CreateBy;
                    //        pmycompany.OrganizationEditView.Add(porganization);
                    //    }
                    //    break;
                    // }



                    return View(pmycompany);
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return RedirectToAction("Login", "User");

            }
        }
 private RegionViewModel RegionDetails()
 {
     #region RegionDetails
     try
     {
         var regionList = new RegionViewModel();
         regionList.Region = new List<RegionModel>();
         var appRegion = _IRegionRepository.GetRegionByOrganizationGUID(new Guid(Session["OrganizationGUID"].ToString())).ToList();
         foreach (var region in appRegion.ToList())
         {
             regionList.Region.Add(new RegionModel { Name = region.Name, RegionGUID = region.RegionGUID.ToString(), Description = region.Description, OrganizationGUID = region.OrganizationGUID != null ? region.OrganizationGUID.ToString() : Guid.Empty.ToString() });
         }
         return regionList;
     }
     catch (Exception ex)
     {
         Logger.Error(ex.Message);
         return null;
     }
     #endregion
 }