// GET: PGRegistrations/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PGRegistration pGRegistration = db.PgRegistrations.Find(id);
            var            user           = UserManager.FindById(pGRegistration.UserId);

            PGRegisterVM pgvm = new PGRegisterVM();

            pgvm.Address       = pGRegistration.Address;
            pgvm.City          = pGRegistration.City;
            pgvm.ContactPerson = pGRegistration.ContactPerson;
            pgvm.District      = pGRegistration.District;
            pgvm.Email         = user.Email;
            pgvm.FirstName     = user.FirstName;
            pgvm.GmapLocation  = pGRegistration.GmapLocation;
            pgvm.LastName      = user.LastName;
            pgvm.NoOfBeds      = pGRegistration.NoOfBeds;
            pgvm.PGID          = pGRegistration.PGID;
            pgvm.PGName        = pGRegistration.PGName;
            pgvm.Phone         = pGRegistration.Phone;
            pgvm.PinCode       = pGRegistration.PinCode;
            pgvm.State         = pGRegistration.State;

            if (pGRegistration == null)
            {
                return(HttpNotFound());
            }
            return(View(pgvm));
        }
        public async Task <ActionResult> Edit([Bind(Include = "PGID,PGName,ContactPerson,Phone,Address,State,District,City,PinCode,GmapLocation,NoOfBeds,FirstName,LastName,Email")] PGRegisterVM pGRegistration)
        {
            if (ModelState.IsValid)
            {
                var pgreg = db.PgRegistrations.FirstOrDefault(x => x.PGID == pGRegistration.PGID);
                pgreg.Address       = pGRegistration.Address;
                pgreg.City          = pGRegistration.City;
                pgreg.ContactPerson = pGRegistration.FirstName + " " + pGRegistration.LastName;
                pgreg.District      = pGRegistration.District;
                pgreg.GmapLocation  = pGRegistration.GmapLocation;
                pgreg.NoOfBeds      = pGRegistration.NoOfBeds;
                pgreg.PGID          = pGRegistration.PGID;
                pgreg.PGName        = pGRegistration.PGName;
                pgreg.Phone         = pGRegistration.Phone;
                pgreg.PinCode       = pGRegistration.PinCode;
                pgreg.State         = pGRegistration.State;

                db.PgRegistrations.Attach(pgreg);
                db.Entry(pgreg).State = EntityState.Modified;
                db.SaveChanges();

                ApplicationUser userModel = UserManager.FindById(pgreg.UserId);
                userModel.FirstName = pGRegistration.FirstName;
                userModel.LastName  = pGRegistration.LastName;
                var result = await UserManager.UpdateAsync(userModel);

                //db.UserPg.Add(new UserPG() { UserId = userModel.Id,  PGID= pgreg.PGID });
                var pgbeds = db.PgBeds.Where(x => x.PgRegistration.PGID == pGRegistration.PGID).Select(y => y.BedID).ToList();
                var ss     = db.PgBedPatientInfo.Include(y => y.PgBed).Where(x => pgbeds.Contains(x.PgBed.BedID)).ToList();
                //var isbedsoccupied = pgbeds.ToList().Count == 0 ? false : (db.PgBedPatientInfo.Include(y => y.PgBed)?.Where(x => pgbeds != null && pgbeds.Contains(x.PgBed.BedID))?.Count() > 0);
                if (ss.Count == 0)
                {
                    var deletepgbeds = db.PgBeds.Where(x => x.PgRegistration.PGID == pGRegistration.PGID);
                    db.PgBeds.RemoveRange(deletepgbeds);
                    db.SaveChanges();

                    for (int i = 1; i <= Convert.ToInt32(pGRegistration.NoOfBeds); i++)
                    {
                        PGBeds pGBeds = new PGBeds();
                        pGBeds.BedNo          = pGRegistration.PGName + "-" + i;
                        pGBeds.BedStatus      = "Available";
                        pGBeds.PgRegistration = pgreg;
                        db.PgBeds.Add(pGBeds);
                        db.SaveChanges();
                    }
                }
                return(RedirectToAction("Index", "PGRegistrations"));
            }

            // AddErrors(result);
            return(View(pGRegistration));
        }
        public async Task <ActionResult> Create([Bind(Include = "PGID,PGName,ContactPerson,Phone,Address,State,District,City,PinCode,GmapLocation,NoOfBeds,FirstName,LastName,Email")] PGRegisterVM pGRegistration)
        {
            if (ModelState.IsValid)
            {
                IdentityResult  result    = new IdentityResult();
                ApplicationUser usercheck = UserManager.FindByEmail(pGRegistration.Email);
                if (usercheck == null)
                {
                    usercheck = new ApplicationUser {
                        UserName = pGRegistration.Email, Email = pGRegistration.Email, FirstName = pGRegistration.FirstName, LastName = pGRegistration.LastName
                    };
                    result = await UserManager.CreateAsync(usercheck, ConfigurationManager.AppSettings["DefaultPwd"]);

                    if (result.Succeeded)
                    {
                        await this.UserManager.AddToRoleAsync(usercheck.Id, "PGAdmin");
                    }
                }

                //if (result.Succeeded)
                {
                    PGRegistration pg = new PGRegistration();
                    pg.Address       = pGRegistration.Address;
                    pg.City          = pGRegistration.City;
                    pg.ContactPerson = pGRegistration.FirstName + " " + pGRegistration.LastName;
                    pg.District      = pGRegistration.District;
                    pg.GmapLocation  = pGRegistration.GmapLocation;
                    pg.NoOfBeds      = pGRegistration.NoOfBeds;
                    pg.PGID          = pGRegistration.PGID;
                    pg.PGName        = pGRegistration.PGName;
                    pg.Phone         = pGRegistration.Phone;
                    pg.PinCode       = pGRegistration.PinCode;
                    pg.State         = pGRegistration.State;
                    pg.UserId        = usercheck.Id;
                    try
                    {
                        db.PgRegistrations.Add(pg);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    db.UserPg.Add(new UserPG()
                    {
                        UserId = usercheck.Id, PGID = pg.PGID
                    });

                    int num = -1;
                    if (int.TryParse(pGRegistration.NoOfBeds, out num))
                    {
                        for (int i = 1; i <= Convert.ToInt32(pGRegistration.NoOfBeds); i++)
                        {
                            PGBeds pGBeds = new PGBeds();
                            pGBeds.BedNo          = pGRegistration.PGName + "-" + i;
                            pGBeds.BedStatus      = "Available";
                            pGBeds.PgRegistration = pg;
                            db.PgBeds.Add(pGBeds);
                            db.SaveChanges();
                        }
                    }

                    return(RedirectToAction("Index", "PGRegistrations"));
                }
                AddErrors(result);
            }

            return(View(pGRegistration));
        }