//protected override void OnActionExecuting(ActionExecutingContext filterContext)
        //{
        //    try
        //    {
        //        if (Session["AuthenticatedUser"] != null)
        //        {
        //            userData = ((User)Session["AuthenticatedUser"]);
        //        }
        //        else
        //        {
        //            //return RedirectToAction("UserLogin", "Login", new { lbl = "Your Session Expired" });
        //            filterContext.Controller.TempData.Add("UserLogin", "Login");
        //        }
        //    }
        //    catch
        //    {
        //        //filterContext.Result = new RedirectResult("~/Login/UserLogin");
        //        filterContext.Controller.TempData.Add("UserLogin", "Login");
        //    }
        //}

        /// <summary>
        /// CreatedBy : Kanishka SHM
        /// CreatedDate: 2016/01/16
        ///
        /// Inserting user details
        ///
        /// argument: None
        ///
        /// </summary>
        /// <returns>Return view</returns>

        // GET: CreateUser
        public ActionResult Create(string lbls)
        {
            int.Parse(Session["userId"].ToString());

            if (lbls != null)
            {
                ViewBag.SuccessMsg = "User Successfully Created";
            }
            //int id = (int)Session["userId"];
            int        id      = Convert.ToInt32(Session["userId"].ToString());
            UserAccess ua      = new UserAccess();
            User       curUser = ua.retreiveUserByUserId(id);

            ViewBag.CurrUserRoleType = curUser.RoleId;

            //Restrict to create above user role
            RoleAccess      ra           = new RoleAccess();
            List <UserRole> roleList     = ra.GetAllUserRoles();
            List <UserRole> tempRoleList = new List <UserRole>();

            //if current user is first super admin he can create aditional super admin
            if (curUser.UserId == curUser.CreatedBy)
            {
                //ViewBag.RoleId = new SelectList(roleList, "RoleId", "RoleName");
                tempRoleList = roleList;
            }
            else
            {
                for (int i = 1; i < roleList.Count && ViewBag.CurrUserRoleType != 3; i++)
                {
                    UserRole tempRole = new UserRole()
                    {
                        RoleId   = roleList[i].RoleId,
                        RoleName = roleList[i].RoleName
                    };
                    tempRoleList.Add(tempRole);
                }
            }
            _createById    = curUser.UserId;
            _companyId     = curUser.Company_Id;
            _curUserRoleId = curUser.RoleId;
            ViewBag.RoleId = new SelectList(tempRoleList, "RoleId", "RoleName");
            _curBranchId   = curUser.BranchId;

            // get all branches
            List <Branch> branchesLists = (new BranchAccess()).getBranches(curUser.Company_Id);

            //if current user is admin restrict to creat user for another branch
            if (ViewBag.CurrUserRoleType == 2)
            {
                //ViewBag.BranchId = new SelectList(branchesLists, "BranchId", "BranchName", curUser.BranchId);
                _curBranchId = curUser.BranchId;
            }
            else
            {
                ViewBag.BranchId = new SelectList(branchesLists, "BranchId", "BranchName");
            }

            return(PartialView("Create"));
        }
        public ActionResult Create(User user)
        {
            int currentUser = 0;

            try
            {
                currentUser = int.Parse(Session["userId"].ToString());
            }
            catch (Exception) {
                return(RedirectToAction("UserLogin", "Login"));
            }

            user.CreatedBy = _createById;
            user.IsDelete  = false;
            user.Status    = false;

            //Set admin branch to new user
            if (_curUserRoleId == 2)
            {
                user.BranchId = _curBranchId;
            }

            //Check role is selected
            if (user.RoleId == 0)
            {
                user.RoleId = 2;
            }

            //Check branch is selected
            if (_curUserRoleId == 1 && user.BranchId == 0)
            {
                user.BranchId = _curBranchId;
            }
            string passwordTemp = user.Password;

            UserAccess ua = new UserAccess();

            string newSalt = PasswordEncryption.RandomString();

            user.Password = PasswordEncryption.encryptPassword(user.Password, newSalt);
            user.Email    = user.NewEmail;

            //Check this
            CompanyAccess ca      = new CompanyAccess();
            Company       company = new Company();//ca.GetCompanyDetailsByFirstSpUserId(currentUser);

            //Insert user
            user.Company_Id = company.CompanyId;
            int res = ua.InsertUser(user);

            //Insert new user to user activation table
            string activationCode = Guid.NewGuid().ToString();
            int    userId         = (new UserAccess()).getUserId(user.Email);

            res = ua.InsertUserActivation(userId, activationCode);
            if (res == 1)
            {
                ViewBag.SuccessMsg = "Data Successfully inserted!";

                string body = "Hi " + user.FirstName + "! <br /><br /> Your account has been successfully created. Below in your account detail." +
                              "<br /><br /> User name: " + user.UserName +
                              "<br /> Password : <b>" + passwordTemp +
                              "<br />Click <a href='http://localhost:57318/CreateUser/ConfirmAccount?userId=" + userId + "&activationCode=" + activationCode + "'>here</a> to activate your account." +
                              "<br /><br/> Thanks,<br /> Admin.";

                Email email = new Email(user.Email);
                email.SendMail(body, "Account details");


                // check the user as superadmin or admin..
                if (user.RoleId == 1 || user.RoleId == 2)
                {
                    ViewBag.SuccessMsg = "User Successfully Created";



                    return(RedirectToAction("create", new { lbls = ViewBag.SuccessMsg }));
                }

                Session["editUserIds"] = userId;


                return(RedirectToAction("SetRights", "EditRights", new { @lbl1 = ViewBag.SuccessMsg }));
            }
            else
            {
                ViewBag.ErrorMsg = "Failed to create user!";

                //Restrict to create above user role
                RoleAccess      ra           = new RoleAccess();
                List <UserRole> roleList     = ra.GetAllUserRoles();
                List <UserRole> tempRoleList = new List <UserRole>();

                for (int i = roleList[_curUserRoleId - 1].RoleId; i <= roleList.Count && _curUserRoleId != 3; i++)
                {
                    UserRole tempRole = new UserRole()
                    {
                        RoleId   = roleList[i - 1].RoleId,
                        RoleName = roleList[i - 1].RoleName
                    };
                    tempRoleList.Add(tempRole);
                }

                ViewBag.RoleId = new SelectList(tempRoleList, "RoleId", "RoleName");

                // get all branches
                List <Branch> branchesLists = (new BranchAccess()).getBranches(_companyId);
                ViewBag.BranchId = new SelectList(branchesLists, "BranchId", "BranchName");


                return(PartialView("Create"));
            }
        }