public ActionResult ManagerBuildingDelete(DisplayClientBuilding model, int BuildingID2)
        {

            ManagerBuilding MB = db.ManagerBuilding
                                    .Where(c => c.BuildingID == BuildingID2 && c.UserID == model.ManagerID)
                                    .FirstOrDefault();
           if(MB!=null)
           {
            db.ManagerBuilding.Remove(MB);
            db.SaveChanges();
            }
            return RedirectToAction("SelectBuilding", model);

        }
        public ActionResult SelectBuilding(DisplayClientBuilding model, int ClientID)
        {
            
            model.ClientID = ClientID;
            model.Manager = db.Manager.Find(model.ManagerID);
            model.clients= db.Clients.Where(c=>c.ID == model.ClientID).ToList();
            
            if (model.clients == null){ return View(model); }
            
            model.buildings = db.Buildings.Where(b => b.Clients.ID == ClientID).ToList();

            //get all entries that belong to the current Manager on the managerbuilding table
            model.ManagerBuildings = db.ManagerBuilding.Where(c => c.UserID == model.ManagerID).ToList();
            foreach (var item in model.ManagerBuildings)
            {    //building that exist on managerbuilding deck
                Buildings removeit = db
               .Buildings
               .First(c => c.ID == @item.BuildingID);
                //note: current user records
                model.buildings.Remove(removeit);
                model.BuildingsOnDeck.Add(removeit);
            } 
            
            return View(model);
        }
        public ActionResult ManagerBuilding(DisplayClientBuilding model, int BuildingID)
        {
            //1. set ManagerBuilding Obj and save it in db
            //2. load clients, Buildings, assigned buildings to the current manager
            //3.
            model.Manager = db.Manager.Find(model.ManagerID);
            ManagerBuilding ObjMB = new ManagerBuilding
            {
                BuildingID =BuildingID,
                UserID = model.ManagerID,
                 ManagerID = model.ManagerID
            };
            if (db.ManagerBuilding.Where(c => c.UserID == model.ManagerID && c.BuildingID == BuildingID).FirstOrDefault() == null)
            {
                db.ManagerBuilding.Add(ObjMB);
                db.SaveChanges();
            }
            //get All clients
            model.clients = db.Clients.Where(c => c.ID == model.ClientID).ToList();
            //get all entries that belong to the current Manager on the managerbuilding table
            model.ManagerBuildings = db.ManagerBuilding.Where(c => c.UserID == model.ManagerID).ToList();
            //selected client's building
            var selectedclitentbuildings = db.Buildings
                .Where(c => c.Clients.ID == model.ClientID).ToList();

            model.buildings = db.Buildings.Where(c=>c.ClientID==model.ClientID).ToList();
            if (selectedclitentbuildings != null)
            {
                foreach(var item in model.ManagerBuildings )
                {    //building that exist on managerbuilding deck
                     Buildings removeit =db
                    .Buildings
                    .First(c=>c.ID == @item.BuildingID);
                    //note: current user records
                     model.buildings.Remove(removeit);
                     model.BuildingsOnDeck.Add(removeit);
                } 
            }
            
            return View("SelectBuilding",model);
        }
       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);
       }