public ActionResult Edit([Bind(Include = "Id,UserId,Username,Email,Branch_Practice_No,Status")] UserStatus userStatus)
        {
            db = new VaccinationContext();
            if (ModelState.IsValid)
            {
                
                string prev = "";
                using (var tmpDb = new VaccinationContext())
                {
                    var firstOrDefault = tmpDb.UserStatus.FirstOrDefault(x => x.Username == userStatus.Username);
                    if (firstOrDefault != null)
                    {
                        prev = firstOrDefault.Status;
                    }
                }

                db.Entry(userStatus).State = EntityState.Modified;
                db.SaveChanges();

                using (var appdb = new ApplicationDbContext())
                {
                    var item = appdb.Users.FirstOrDefault(x => x.UserName == userStatus.Username);
                    item.Email = userStatus.Email;

                    appdb.SaveChanges();
                }

                if (userStatus.Status == "Active" && prev == "New")
                    {
                        MailService mail = new MailService("*****@*****.**", "V@@5hY9a");
                        mail.SendMail(userStatus.Email, "Access Granted: Confirmation of Access to Vaccination Manager", "<html><body><h3>Hi " + userStatus.Username +"</h3>" + "<br/><p>This message is to confirm an Approval by Admin for your access request on Vaccination Manager.<br/>You will now be able to log onto the system. For more information you can contact your branch manager or Admin.</p><br/><p>Regards</p><b><p>Vaccination Manager Team</p></b></body></html>");

                    }
                
                return RedirectToAction("Index");
            }
            return View(userStatus);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                Branch resultBranch = db.Branches1.FirstOrDefault(x => x.Practice_No == model.Branch.Trim());

                IdentityResult result;
                var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; ;
                if (resultBranch == null)
                {
                    result = IdentityResult.Failed("The entered Branch Practice Number is not a registered branch. Please contact Admin for a valid Practice Number.");
                    
                }
                else
                {
                    
                    result = await UserManager.CreateAsync(user, model.Password);
                }

                
                if (result.Succeeded)
                {
                    //  Comment the following line to prevent log in until the user is confirmed.
                    //  await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    //string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");


                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var res = await UserManager.ConfirmEmailAsync(user.Id, code);

                    //ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                    //                + "before you can log in.";

                    // For local debug only
                    //ViewBag.Link = callbackUrl;

                    db.UserStatus.Add(new UserStatus
                        {
                            UserId = user.Id,
                            Username = user.UserName,
                            Email = user.Email,
                            Branch_Practice_No = model.Branch,
                            Status = "New"
                           
                        });
                    db.SaveChanges();

                    MailService mail = new MailService("*****@*****.**", "V@@5hY9a");
                    mail.SendMail(user.Email, "Notification: Received Access Request", "<html><body><h3>Hi " + user.UserName + "</h3><br/>" + "<p>This message is to let you know that we acknowledge receipt of your access request on Vaccination Manager.<br/>You will receive a second email confirming access to the system</p><br/><p>Regards</p><b><p>Vaccination Manager Team</p></b></body></html>");
                    var firstOrDefault = db.UserStatus.FirstOrDefault(x => x.Username == "admin01");
                    string email = "";
                    if (firstOrDefault != null)
                    {
                         email = firstOrDefault.Email;
                    }
                    mail.SendMail(email, "ACTION NEEDED: User Access Request. Vaccination Manager", "<html><body><h3>System Access Request</h3><br/><p>A new user request has been sent by the following user:</p><br/> <b><label>Username: </label></b><label>" + user.UserName + "</label><br/><b><label>Email: <label></b><label>" + user.Email + "</label><br/><b><label>Branch Practice Number: </label></b><label>" + model.Branch + "</label><br/><p>Log onto the system to Activate this user.</p></body></html>");

                    return View("ConfirmEmail");
                    //return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }