using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BusinessLayer;
using DTO;

namespace NikaiDSR
{
    public partial class Login : Page
    {
        #region declararation
        UserManager UM = new UserManager();
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "NIKAI- LOGIN";
            //Session.Abandon();
            //FormsAuthentication.SignOut();
            //loginControl.Focus();
            //Button thisIB = new Button();
            //thisIB = (Button)loginControl.FindControl("LoginButton");
            //form2.DefaultButton = thisIB.UniqueID;
        }
        #endregion

        #region code loginControl_Authenticate
        protected void loginControl_Authenticate(object sender, AuthenticateEventArgs e)
        {
            e.Authenticated = false;
            Image thisImg = new Image();
            thisImg = (Image)loginControl.FindControl("imgErr");
            thisImg.Visible = false;
            string Msg = "";
            UserDTO userDetails = UM.GetUser(null, loginControl.UserName, loginControl.Password);
            if (userDetails != null && userDetails.Status == true)
            {
                Session["UserInfo"] = userDetails;
                IList<RoleRightsDTO> RoleRights = UM.GetRoleRights(userDetails.Role_Id);
                Session["RoleRights"] = RoleRights;
                FormsAuthentication.RedirectFromLoginPage(loginControl.UserName, false);

                if (userDetails.Role_Id == 5)
                    loginControl.DestinationPageUrl = "~/Executive/LandingPageExecutive.aspx";
                else
                    loginControl.DestinationPageUrl = "~/Admin/LandingPageAdmin.aspx";

                e.Authenticated = true;
            }

            if (!e.Authenticated)
            {
                thisImg.Visible = true;
                Msg = "User Name & Password is incorrect";
                loginControl.FailureText = Msg;
            }
            if (userDetails != null && userDetails.Status == false)
            {
                thisImg.Visible = true;
                Msg = "Inactive Login";
                loginControl.FailureText = Msg;
            }
        }
        #endregion
    }
}