//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");

            }
        }
        public ActionResult EditOrg(mycompany mycompany)
        {
            Logger.Debug("Inside Organization Controller- Edit HttpPost");
            try
            {
                if (Session["OrganizationGUID"] != null)
                {
                    if (ModelState.IsValid)
                    {
                        if (mycompany.OrganizationEditView != null)
                        {
                            OrganizationEditView organization = mycompany.OrganizationEditView;

                            Organization Organization = new Organization();
                            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;
                            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.AllowContractors = true;
                            Organization.CreateDate = organization.CreatedDate;
                            Organization.CreateBy = organization.CreateBy;
                            Organization.LastModifiedDate = DateTime.UtcNow;
                            if (Session["UserGUID"] != null)
                                Organization.LastModifiedBy = new Guid(Session["UserGUID"].ToString());


                            LatLong latLong = new LatLong();
                            latLong = GetLatLngCode(Organization.AddressLine1, Organization.AddressLine2, Organization.City, Organization.State, Organization.Country, Organization.ZipCode);
                            Organization.TimeZone = Convert.ToDouble(getTimeZone(latLong.Latitude, latLong.Longitude));


                            if (_IOrganizationRepository.UpdateOrganization(Organization) > 0)
                            {
                                return RedirectToAction("Index", "MyCompany");
                            }
                            else
                            {
                                TempData["TabName"] = "Details";
                                mycompany.AspNetUserViewModel = UserDetails();

                                mycompany.RegionViewModel = RegionDetails();

                                mycompany.TerritoryViewModel = TerritoryDetails();
                                return View("Index", mycompany);
                            }
                        }
                        else
                        {
                            return RedirectToAction("Index", "MyCompany");
                        }

                    }
                    else
                    {
                        TempData["TabName"] = "Details";
                        mycompany.AspNetUserViewModel = UserDetails();

                        mycompany.RegionViewModel = RegionDetails();

                        mycompany.TerritoryViewModel = TerritoryDetails();
                        return View("Index", mycompany);
                    }
                }
                else
                {
                    return RedirectToAction("SessionTimeOut", "User");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return View(mycompany);
            }
        }