public ActionResult AddUser(int BuildingID, ManagerVM model_User, Permission model_permissions)
        {

            return View();
        }
        public async Task<ActionResult> AddManagerTobuilding(ManagementBuilding model, ManagerVM  model2)
        {
            try
            {
            if (!ModelState.IsValid)
            {
                return View("ManagementBuilding", model);
            }
            ApplicationDbContext context = new ApplicationDbContext();

            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            PasswordHasher hasher = new PasswordHasher();
            var a = UserManager.FindByEmail(model2.Email);
            if (a != null)
            {
                return View("ManagementBuilding", model);
            }
            ApplicationUser AppUser = new ApplicationUser()
            {
                Id = Guid.NewGuid().ToString(),
                Email = model2.Email,
                UserName = model2.Username,
                SecurityStamp = Guid.NewGuid().ToString(),
                PhoneNumber = model2.Phone,
                LockoutEnabled = false,
                LockoutEndDateUtc= DateTime.Now.AddDays(365),
                AccessFailedCount = 0,
                PhoneNumberConfirmed = false,
                TwoFactorEnabled = false,
                EmailConfirmed = false,
                PasswordHash = hasher.HashPassword(model2.Password)
            };
            string[] FullName = model2.FullName.Split(new string[] { " " }, StringSplitOptions.None);
            Manager mgr = new Manager()
            {
                ID = AppUser.Id,
                FirstName = FullName[0].ToString(),
                LastName = FullName[1].ToString(),
                Phone = model2.Phone,
                ClientID = model2.clientID
            };
            db.Manager.Add(mgr);
            context.Users.Add(AppUser);

            await context.SaveChangesAsync();
            await db.SaveChangesAsync();

            RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            if (!RoleManager.RoleExists("Manager"))
            {var roleresult = RoleManager.Create(new IdentityRole("Manager"));}
            var Result = UserManager.AddToRole(AppUser.Id, "Manager");

            ManagerBuilding ObjManagerBuilding = new ManagerBuilding()
            {
                 BuildingID = model2.BuildingID,
                  ManagerID = mgr.ID ,
                   UserID =mgr.ID
            };

            db.ManagerBuilding.Add(ObjManagerBuilding);
            await db.SaveChangesAsync();
         

            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            return RedirectToAction("ManagementBuilding", new { BuildingID=model2.BuildingID});
        }
       public async Task<ActionResult> AddManager(ManagerVM model)
       {

           ApplicationDbContext context = new ApplicationDbContext();

           var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
           PasswordHasher hasher = new PasswordHasher();
          
           ApplicationUser AppUser = new ApplicationUser(){
           Id = Guid.NewGuid().ToString(),
           Email = model.Email,
           UserName = model.Username,
           SecurityStamp = Guid.NewGuid().ToString(),
           PhoneNumber = model.Phone,
           LockoutEnabled = false,
           AccessFailedCount = 0,
           PhoneNumberConfirmed = false,
           TwoFactorEnabled = false,
           EmailConfirmed = false,
           PasswordHash = hasher.HashPassword(model.Password)
          };
           string[] FullName = model.FullName.Split(new string[] {" "}, StringSplitOptions.None);
           Manager mgr = new Manager() {  
                                       ID = AppUser.Id,  
                                       FirstName=FullName[0].ToString(), 
                                       LastName = FullName[1].ToString(),  
                                       Phone = model.Phone,
                                        ClientID = model.clientID};
           db.Manager.Add(mgr); 
           context.Users.Add(AppUser);
          
           await context.SaveChangesAsync();
           await db.SaveChangesAsync();

           RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
           if(!RoleManager.RoleExists("Manager"))
           {
            var roleresult = RoleManager.Create(new IdentityRole("Manager"));
           }
           var Result = UserManager.AddToRole(AppUser.Id, "Manager");

           DisplayClientBuilding ObjDCB = new DisplayClientBuilding(){
            ManagerID = AppUser.Id, ClientID = model.clientID};

           return RedirectToAction("SelectBuilding", ObjDCB);
       }
 public ActionResult AddManager()
 {
    var ObjManager = new ManagerVM();
    ViewBag.clientList = db.Clients
        .Select(c => new SelectListItem { Text = c.ClientName,
                                          Value = c.ID.ToString() }).ToList();
    return View(ObjManager); 
 }
       public JsonResult AddUser(int BuildingID, ManagerVM model_User, PermissionBase Permission, ManageUsersProfileVM model4)
        {
            ManageUsersProfileVM Obj = new ManageUsersProfileVM();
            model4.managerVM = model_User;
         
            Obj.managerVM = model_User;
            var UserID = Obj.InsertUser(Obj);  //Create a user to the mail aspNetUser table.         
            var RoleNames = Obj.ConvertToRoleNames(Permission);//Pass all permissions and return a list type string of accepted roles
          var returnUserID = Obj.AddBuildingUser(model_User, UserID);// Insert building staff.
            foreach (var item in RoleNames)// loop throught the list of roles
            {
                Obj.InserUserPermission(item, returnUserID); // Insert one permission at a time. 
            }


            var mydata = Json("");
            return new JsonResult {Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
        public string AddBuildingUser(ManagerVM model, string AspnetUserID)
        {
            string[] FullName = model.FullName.Split(new string[] { " " }, StringSplitOptions.None);

            BuildingUser ObjBU = new BuildingUser()
            {
                BuildingID = model.BuildingID,
                FirstName = FullName[0].ToString(),
                LastName = FullName[1].ToString(),
                Phone = model.Phone,
                Email = model.Email,
                UserName = model.Username,
                UserID = AspnetUserID
            };
            db.BuildingUser.Add(ObjBU);
            db.SaveChanges();

            return "";
        }