// GET: /Usuarios/Create
        public ActionResult Create()
        {
           

            RegisterViewModelByAdmin u = new RegisterViewModelByAdmin();            

            u.UserId = "null";

        
              
           
            return View("AddEdit",u);
        }
        public async Task<ActionResult> Create(RegisterViewModelByAdmin model)
        {
           

            if (ModelState.IsValid)
            {

                
                   

                    var user = new ApplicationUser() { 
                        UserName = model.UserName, 
                   
            };
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        TempData["Guardado"] = true;

                            UserManager.AddToRole(user.Id, "Administrador");
                      
                        //await SignInAsync(user, isPersistent: false);
                            return RedirectToAction("Edit", new { id = user.Id });

                            //return View("AddEdit", model);
                         
                    }
                    else
                    {
                        TempData["GuardadoError"] = true;

                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
                

                //Problems
                   
                    return View("AddEdit",model);
             
               
            }
            TempData["GuardadoError"] = true;

          
            return View("AddEdit", model);
        }
        public async Task<ActionResult> Edit(RegisterViewModelByAdmin model)
        {

            if (model.Password == null)
            {
                ModelState["Password"].Errors.Clear();
                ModelState["ConfirmPassword"].Errors.Clear();

            }
            if (ModelState.IsValid)
            {
             

                var user = UserManager.FindById(model.UserId);

                user.UserName = model.UserName;
        

                var result = await UserManager.UpdateAsync(user);

               
                if (result.Succeeded)
                {
                    //UserManager.RemoveFromRole(user.Id, "Role");
                  

                    //UserManager.AddToRole(user.Id, model.Role);



                    if (model.Password != null)
                    {
                        UserManager.RemovePassword(user.Id);
                        UserManager.AddPassword(user.Id, model.Password);

                    }
                    //await SignInAsync(user, isPersistent: false);
                    TempData["Guardado"] = true;

                    return RedirectToAction("Edit", new { id = model.UserId});
                }
                else
                {
                    TempData["GuardadoError"] = true;

                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }


                // Hubo Problemas
                //ViewBag.vPacientes = db.Pacientes.OrderBy(r=>r.Nombre).ToList();

                return View("AddEdit", model);


            }
            //ViewBag.vPacientes = db.Pacientes.OrderBy(r => r.Nombre).ToList();
            TempData["GuardadoError"] = true;


            return View("AddEdit", model);
        }
        // GET: /Usuarios/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ApplicationUser usuariodetalles = db.Users.Find(id);
            if (usuariodetalles == null)
            {
                return HttpNotFound();
            }

            RegisterViewModelByAdmin u = new RegisterViewModelByAdmin();
            u.UserName = usuariodetalles.UserName;
            u.UserId = usuariodetalles.Id;          
           


            return View("AddEdit", u);
        }