public string GetuserID(string username)
        {
            using (nlbdataEntities _Context = new nlbdataEntities())
            {
                //var userRoles = (from user in _Context.NBA_SYS_Users
                //                 join roleMapping in _Context.NBA_Role
                //                 on user.NBA_Role  equals roleMapping.ROLE_ID
                //                 join role in _Context.NBA_Role
                //                 on roleMapping.RoleId equals role.ROLE_ID
                //                 where user.Username == username
                //                 select role.RoleName).ToArray();


                //var userRoles = (from user in _Context.NBA_SYS_Users
                //                 where user.email == username
                //                 select user.surname
                //                 );

                ////var userRoles = _Context.NBA_SYS_Users.Where(x => x.email == username).Select(n => n.ROLE_ID );
                var userRoles = _Context.NBA_SYS_Users.Where(x => x.email == username).Select(n => n.SYS_USER_ID).Take(1).SingleOrDefault();
                ;

                return(userRoles.ToString());
            }
        }
        public ActionResult VerifyAccount(string id)

        {
            bool Status = false;

            using (nlbdataEntities dc = new nlbdataEntities())

            {
                dc.Configuration.ValidateOnSaveEnabled = false;



                var v = dc.NBA_SYS_Users.Where(a => a.sys_VerificationCode == new Guid(id)).FirstOrDefault();

                if (v != null)

                {
                    v.sys_IsEmailVerified = true;

                    dc.SaveChanges();

                    Status = true;
                }

                else

                {
                    ViewBag.Message = "Invalid Request";
                }
            }

            ViewBag.Status = Status;

            return(View());
        }
 public ActionResult Save(int id)
 {
     using (nlbdataEntities dc = new nlbdataEntities())
     {
         var v = dc.NBA_Agwy.Where(a => a.AGWY_ID == id).FirstOrDefault();
         return(View(v));
     }
 }
        public ActionResult GetGirls()

        {
            using (nlbdataEntities dc = new nlbdataEntities())
            {
                var pr = dc.NBA_Agwy.OrderBy(a => a.name).ToList();
                return(Json(new { data = pr }, JsonRequestBehavior.AllowGet));
            }
        }
        public bool IsEmailExist(string emailID)

        {
            using (nlbdataEntities dc = new nlbdataEntities())

            {
                var v = dc.NBA_SYS_Users.Where(a => a.email == emailID).FirstOrDefault();

                return(v != null);
            }
        }
 public ActionResult Delete(int id)
 {
     using (nlbdataEntities dc = new nlbdataEntities())
     {
         var v = dc.NBA_Agwy.Where(a => a.AGWY_ID == id).FirstOrDefault();
         if (v != null)
         {
             return(View(v));
         }
         else
         {
             return(HttpNotFound());
         }
     }
 }
        public ActionResult DeleteEmployee(int id)
        {
            bool status = false;

            using (nlbdataEntities dc = new nlbdataEntities())
            {
                var v = dc.NBA_Agwy.Where(a => a.AGWY_ID == id).FirstOrDefault();
                if (v != null)
                {
                    dc.NBA_Agwy.Remove(v);
                    dc.SaveChanges();
                    status = true;
                }
            }
            return(new JsonResult {
                Data = new { status = status }
            });
        }
        public ActionResult Save(NBA_Agwy emp)
        {
            bool status = false;

            if (ModelState.IsValid)
            {
                using (nlbdataEntities dc = new nlbdataEntities())
                {
                    if (emp.AGWY_ID > 0)
                    {
                        //Edit
                        var v = dc.NBA_Agwy.Where(a => a.AGWY_ID == emp.AGWY_ID).FirstOrDefault();
                        if (v != null)
                        {
                            v.name     = emp.name;
                            v.surname  = emp.surname;
                            v.known_as = emp.known_as;
                            v.gender   = emp.gender;
                            v.age      = emp.age;
                            v.type_of_identification = emp.type_of_identification;
                            v.idno                 = emp.idno;
                            v.dateOfbirth          = emp.dateOfbirth;
                            v.town_village_address = emp.town_village_address;
                            v.maiden_name          = emp.maiden_name;
                            v.street_name          = emp.street_name;
                            v.town_village         = emp.town_village;
                            v.uic = emp.uic;
                        }
                    }

                    else
                    {
                        //Save
                        dc.NBA_Agwy.Add(emp);
                    }
                    dc.SaveChanges();
                    status = true;
                }
            }
            return(new JsonResult {
                Data = new { status = status }
            });
        }
        public ActionResult Registration([Bind(Exclude = "sys_IsEmailVerified,sys_VerificationCode")] NBA_SYS_Users user)

        {
            bool Status = false;

            string message = "";

            //

            // Model Validation

            if (ModelState.IsValid)

            {
                #region //Email is already Exist

                var isExist = IsEmailExist(user.email);

                if (isExist)

                {
                    ModelState.AddModelError("EmailExist", "Email already exist");

                    return(View(user));
                }

                #endregion



                #region Generate Activation Code

                user.sys_VerificationCode = Guid.NewGuid();

                #endregion



                #region  Password Hashing

                user.password = Crypto.Hash(user.password);

                user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword); //

                #endregion

                user.sys_IsEmailVerified = false;



                #region Save to Database

                using (nlbdataEntities dc = new nlbdataEntities())

                {
                    dc.NBA_SYS_Users.Add(user);

                    dc.SaveChanges();



                    //Send Email to User

                    SendVerificationLinkEmail(user.email, user.sys_VerificationCode.ToString());

                    message = "Registration successfully done. Account activation link " +

                              " has been sent to your email id:" + user.email;

                    Status = true;
                }

                #endregion
            }

            else

            {
                message = "Invalid Request";
            }



            ViewBag.Message = message;

            ViewBag.Status = Status;

            return(View(user));
        }
        public ActionResult Login(UserLogin login, string ReturnUrl = "")

        {
            if (Session["uname"] != null)
            {
                return(RedirectToAction("index", "home", new { email = Session["uname"].ToString() }));
            }



            string message = "";

            using (nlbdataEntities dc = new nlbdataEntities())

            {
                var v = dc.NBA_SYS_Users.Where(a => a.email == login.email).FirstOrDefault();

                if (v != null)

                {
                    if (!v.sys_IsEmailVerified)

                    {
                        ViewBag.Message = "Please verify your email first";

                        return(View());
                    }



                    if (string.Compare(Crypto.Hash(login.password), v.password) == 0)

                    {
                        Session["UseriD"] = GetuserID(login.email);



                        int timeout = login.RememberMe ? 525600 : 20; // 525600 min = 1 year

                        var ticket = new FormsAuthenticationTicket(login.email, login.RememberMe, timeout);

                        string encrypted = FormsAuthentication.Encrypt(ticket);

                        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);

                        cookie.Expires = DateTime.Now.AddMinutes(timeout);

                        cookie.HttpOnly = true;

                        Response.Cookies.Add(cookie);



                        if (Url.IsLocalUrl(ReturnUrl))

                        {
                            return(Redirect(ReturnUrl));
                        }

                        // if (dc.NBA_Role.ToString()=="PGT")
                        string role = GetRolesForUser(login.email);
                        if (role == "4")
                        {
                            Session["uname"] = login.email;


                            return(RedirectToAction("Index", "NBA_ManageBeneficiaries"));
                            // return ("ManageGirls");
                        }


                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }

                    else

                    {
                        ViewBag.message = "Invalid credential provided";
                    }
                }

                else

                {
                    ViewBag.message = "Invalid credential provided";
                }
            }

            ViewBag.Message = message;

            return(View());
        }
        //public ActionResult Create([Bind(Include = "AGWY_ID,name,surname,known_as,gender,race,type_of_identification,idno,dateOfbirth,place_of_birth,maiden_name,address,town_village,uic,email,phone_number,alternative_number,alternative_number_relationship,attended_school,name_of_school,higest_grade_passed,currrent_occupation,approval_flag,concent_flag,mimetype,imagedata,home_language,reg_date,last_updated,last_login,status,ID,SEMESTER_ID,SYS_USER_ID,DOCUMENT_ID,Core_ID,messageInquiry,EntryPoint_ID,HighSchoolQuizz_ID,RISK_ID")] NBA_Agwy nBA_Agwy)
        public ActionResult Create(NBA_Agwy nBA_Agwy)
        {
            if (ModelState.IsValid)
            {
                var db = new nlbdataEntities();

                string uuic = nBA_Agwy.name.Substring(0, 4) + nBA_Agwy.surname.Substring(0, 4) + nBA_Agwy.dateOfbirth.ToString().Substring(0, 8);

                //r dbuic = db.NBA_Agwy.Where(x => x.uic.Substring(0, 15) == uuic).Select( n1 => n1.uic).FirstOrDefault();
                //ar uic = db.NBA_Agwy.Where(x => x.uic.Substring(0, 15) == uuic).Select(n1 => n1.uic).FirstOrDefault();
                //r uic1=(from var in db.NBA_Agwy where var.uic == nBA_Agwy.uic).select var.uic;
                //uic1.FirstOrDefault();
                //tring dbui1 = dbuic.ToString();


                var uiccount = db.NBA_Agwy.Where(x => x.uic.Substring(0, 16) == uuic).Select(n3 => n3.uic).Count();

                int uiccounting = uiccount;


                NBA_Agwy n = new NBA_Agwy();
                {
                    n.name     = nBA_Agwy.name;
                    n.surname  = nBA_Agwy.surname;
                    n.known_as = nBA_Agwy.known_as;
                    n.gender   = nBA_Agwy.gender;
                    n.age      = nBA_Agwy.age;
                    n.type_of_identification = nBA_Agwy.type_of_identification;
                    n.idno     = nBA_Agwy.idno;
                    n.reg_date = DateTime.Today;
                    int useridsession = Convert.ToInt32(Session["UseriD"]);
                    n.SYS_USER_ID                     = useridsession;
                    n.house_number                    = nBA_Agwy.house_number;
                    n.street_name                     = nBA_Agwy.street_name;
                    n.town_village_address            = nBA_Agwy.town_village_address;
                    n.describe_living                 = nBA_Agwy.describe_living;
                    n.dateOfbirth                     = nBA_Agwy.dateOfbirth;
                    n.maiden_name                     = nBA_Agwy.maiden_name;
                    n.name_of_school                  = nBA_Agwy.name_of_school;
                    n.alternative_number              = nBA_Agwy.alternative_number;
                    n.alternative_number_relationship = nBA_Agwy.alternative_number_relationship;
                    n.attended_school                 = nBA_Agwy.attended_school;
                    n.currrent_occupation             = nBA_Agwy.currrent_occupation;
                    n.email               = nBA_Agwy.email;
                    n.EntryPoint_ID       = nBA_Agwy.EntryPoint_ID;
                    n.higest_grade_passed = nBA_Agwy.higest_grade_passed;
                    n.HighSchoolQuizz_ID  = nBA_Agwy.HighSchoolQuizz_ID;
                    n.known_as            = nBA_Agwy.known_as;
                    n.messageInquiry      = nBA_Agwy.messageInquiry;
                    n.NBA_Grade           = nBA_Agwy.NBA_Grade;
                    n.NBA_Identification  = nBA_Agwy.NBA_Identification;
                    n.ID             = nBA_Agwy.ID;
                    n.town_village   = nBA_Agwy.town_village;
                    n.NBA_Occupation = nBA_Agwy.NBA_Occupation;



                    if (uiccounting > 1 || uiccounting == 1)
                    {
                        if (uiccounting < 9)
                        {
                            n.uic = nBA_Agwy.name.Substring(0, 4) + nBA_Agwy.surname.Substring(0, 4) + nBA_Agwy.dateOfbirth.ToString().Substring(0, 8) + "0" + (1 + uiccount).ToString();
                        }
                        else
                        {
                            n.uic = nBA_Agwy.name.Substring(0, 4) + nBA_Agwy.surname.Substring(0, 4) + nBA_Agwy.dateOfbirth.ToString().Substring(0, 8) + (1 + uiccount).ToString();
                        }
                    }
                    else
                    {
                        n.uic = nBA_Agwy.name.Substring(0, 4) + nBA_Agwy.surname.Substring(0, 4) + nBA_Agwy.dateOfbirth.ToString().Substring(0, 8) + "00";
                    }
                }



                db.NBA_Agwy.Add(n);
                db.SaveChanges();
                return(RedirectToAction("Index"));

                ////db.NBA_Agwy.Add(nBA_Agwy);
                ////db.SaveChanges();
                ////return RedirectToAction("Index");
            }

            ViewBag.Core_ID                = new SelectList(db.NBA_Core, "CORE_ID", "description", nBA_Agwy.Core_ID);
            ViewBag.DOCUMENT_ID            = new SelectList(db.NBA_Documents, "DOCUMENT_ID", "doc_name", nBA_Agwy.DOCUMENT_ID);
            ViewBag.EntryPoint_ID          = new SelectList(db.NBA_Entry_Points, "ENTRY_ID", "description", nBA_Agwy.EntryPoint_ID);
            ViewBag.gender                 = new SelectList(db.NBA_Gender, "GenderID", "GenderType", nBA_Agwy.gender);
            ViewBag.higest_grade_passed    = new SelectList(db.NBA_Grade, "Grade_ID", "Description", nBA_Agwy.higest_grade_passed);
            ViewBag.HighSchoolQuizz_ID     = new SelectList(db.NBA_HighSchoolQuizz, "HighSchoolQuiz_ID", "Description", nBA_Agwy.HighSchoolQuizz_ID);
            ViewBag.type_of_identification = new SelectList(db.NBA_Identification, "Identification_ID", "Description", nBA_Agwy.type_of_identification);
            ViewBag.messageInquiry         = new SelectList(db.NBA_MessageID, "MessageID", "Description", nBA_Agwy.messageInquiry);
            ViewBag.currrent_occupation    = new SelectList(db.NBA_Occupation, "Occupation_ID", "Description", nBA_Agwy.currrent_occupation);
            ViewBag.RISK_ID                = new SelectList(db.NBA_RiskAssessment, "RISK_ID", "risk_description", nBA_Agwy.RISK_ID);
            ViewBag.SEMESTER_ID            = new SelectList(db.NBA_SemesterMaintenance, "SEMESTER_ID", "semester_name", nBA_Agwy.SEMESTER_ID);
            ViewBag.ID = new SelectList(db.NBA_PR, "ID", "PR_SR_ID", nBA_Agwy.ID);
            return(View(nBA_Agwy));
        }