public JsonResult TenantEdit(Tenant model)
        {   //this i use forthe link since model didn't pass Nav properties
           
 
            db.Tenant.Attach(model);
            var Entry = db.Entry(model);
            Entry.Property(c => c.FirstName).IsModified = true;
            Entry.Property(c => c.LastName).IsModified = true;
            Entry.Property(c => c.Phone).IsModified = true;
            Entry.Property(c => c.Username).IsModified = true;
            
            db.SaveChanges();



            var tenants = db.Tenant.Where(c => c.aptID == model.aptID).Select(c => new 
            { 
                ID=c.ID,
                FirstName = c.FirstName, 
                LastName = c.LastName, 
                Phone = c.Phone, 
                UserName = c.Username, 
                CreatedDate = c.Created 
            }).ToList();

            var mydata = Json(tenants);

            return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
           // return RedirectToAction("ApartmentProfile", new { ApartmentID = ApartmentID, BuildingID =BuildingID});
        }
        public JsonResult TenantEdit(string TenantID)
        {
            Tenant tn = db.Tenant.Find(TenantID);
            Tenant obj = new Tenant();
            obj.ID = tn.ID;
            obj.FirstName = tn.FirstName;
            obj.LastName = tn.LastName;
            obj.Username = tn.Username;
            obj.Phone = tn.Phone;
            var mydata = Json(obj);

            return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
        public async Task<JsonResult> AddingTenant(TenantVM newTenant)
        {
            try { 
            
           
            if (ModelState.IsValid)
            {

                string password ="";
                if (newTenant.GenerateAutomaticPassword == false){ password = newTenant.Password;}
                else{ password = PasswordGenerator.GeneratePassword("8").ToString();}

              
                var UserID = RegisterUsers.InsertUser(newTenant.Username, newTenant.Phone, password);
                var result = RegisterUsers.InserToRole("Tenant", UserID);
                
                var tenantdb = new Tenant
                {
                    ID = UserID,
                    FirstName = newTenant.FirstName,
                    LastName = newTenant.LastName,
                    Phone = newTenant.Phone,
                    Created = DateTime.Now,
                    aptID = newTenant.aptID,
                    Username = newTenant.Username
                };
                db.Tenant.Add(tenantdb);
                
                await db.SaveChangesAsync();         
                
                if(newTenant.EmailNotification==true)
                {
                    var aptNumber = db.Apartment.Find(newTenant.aptID).ApartmentNumber;

                    
                    string string1 = "Hi " + newTenant.FirstName +" "+ newTenant.LastName + ", An Account has been created for you by PointerWebApp.com ";
                    string string2 = "The login information for apartment: "+ aptNumber+" is below";
                    string string3 = "Username: "******"Temporary password: "******"Click the on this http://localhost:64083/Account/Manage link and follow the instructions to initiate your account ";
                    string string6 = "Company Description";
                    string string7 = "Find more information...";

                    string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

                    Gmail gmail = new Gmail("pointerwebapp", "Dmc10040!");
                    MailMessage msg = new MailMessage("*****@*****.**", tenantdb.Username);
                    msg.Subject = "Pointer Security New Account Notification";
                    msg.Body = x;
                    gmail.Send(msg);
                }

                var mydata = Json("");

                return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
             
                //return RedirectToAction("ApartmentProfile", new { ApartmentID = newTenant.aptID, BuildingID= newTenant.BuildingID });

            }
            }
                catch(Exception e){

                    ViewBag.Message = e.Message;
                
            }
            var mydata1 = Json("");

            return new JsonResult { Data = mydata1, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; ;
        }