using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Organization_Settings_LoginList : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    //get the culture info to set the language
    CultureInfo ci;
    LoginListEventData loginList = new LoginListEventData();
    EmployeeEventData ObjEmployeeEventData = new EmployeeEventData();
    LeaveEventData objLeave = new LeaveEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    DataTable dtLoginList = new DataTable();
    cls_Common objCommon = new cls_Common();
    #endregion

    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        grv_LoginList.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objCommon.menu("Others", "72");
            LoadStrings(ci);
            string strSetAlert = string.Empty;
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SureToLogout", ci) + "','" + rm.GetString("Alert_SelectUserIdsToLogout", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            this.btn_Logout.Focus();
            //Set UserRights
            UserRights();
        }
        LoadLoginList();
    }
    #endregion

    #region ButtonEvents
    protected void btn_Logout_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        if (hdnSelectRow.Value != string.Empty)
        {
            string[] UserIDs;
            UserIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            string chkBoxId = "";
            string userId = "";
            for (int i = 0; i < UserIDs.Length; i++)
            {
                userId = UserIDs[i].ToString().ToLower();
                dtLoginList.DefaultView.RowFilter = "UserId ='" + userId + "'";
                chkBoxId = dtLoginList.DefaultView.ToTable().Rows[0]["ConcurrentUserId"].ToString();
                string sUser = Convert.ToString(Cache[chkBoxId.ToUpper()]);
                if (sUser != null && sUser != String.Empty)
                {
                    ObjEmployeeEventData.UpdateLastLogout(userId);
                    HttpContext.Current.Cache.Remove(userId.ToUpper());
                }
                if (userId == Session["EmployeeId"].ToString())
                {
                    str = "alert('" + rm.GetString("Alert_Logged_Out_Successfully", ci) + "');location.replace('../Index.aspx');";
                }
            }
        }
        if (str == "alert('" + rm.GetString("Alert_Logged_Out_Successfully", ci) + "');location.replace('../Index.aspx');")
        {
            Session.Clear();
        }
        else
        {
            str = "alert('" + rm.GetString("Alert_Logged_Out_Successfully", ci) + "');location.replace('LoginList.aspx');";
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
    }

    #endregion

    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderLoginList.Text = rm.GetString("Org_Login_List", ci);
        btn_Logout.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Logout", ci);
        grv_LoginList.Columns[5].Caption = rm.GetString("email_Status", ci);
        grv_LoginList.Columns[2].Caption = rm.GetString("User_Id", ci);
        grv_LoginList.Columns[1].Caption = rm.GetString("lbl_ConcurrentUserId", ci);
        grv_LoginList.Columns[3].Caption = rm.GetString("IP_Address", ci);
        grv_LoginList.Columns[4].Caption = rm.GetString("Login_Time", ci);
    }
    private void LoadLoginList()
    {
        string strCacheData;
        DataColumn dcConcurrentUserId = new DataColumn("ConcurrentUserId");
        DataColumn dcUserId = new DataColumn("UserId");
        DataColumn dcIpAddress = new DataColumn("IpAddress");
        DataColumn dcLoginTime = new DataColumn("LoginTime");
        DataColumn dcStatus = new DataColumn("Status");
        DataRow dr;
        dtLoginList.Columns.Add(dcConcurrentUserId);
        dtLoginList.Columns.Add(dcUserId);
        dtLoginList.Columns.Add(dcIpAddress);
        dtLoginList.Columns.Add(dcLoginTime);
        dtLoginList.Columns.Add(dcStatus);
        DictionaryEntry de = new DictionaryEntry();
        foreach (object obj in Cache)
        {
            de = (DictionaryEntry)obj;
            if (de.Key.ToString() != "ActiveYearSetting")
            {
                strCacheData = de.Value.ToString();
                string[] strData = strCacheData.Split(',');
                if (strData.Length > 1)
                {
                    dr = dtLoginList.NewRow();
                    dr["ConcurrentUserId"] = de.Key.ToString();
                    dr["UserId"] = de.Key.ToString();
                    dr["IpAddress"] = strData[0].ToString();
                    dr["LoginTime"] = strData[1].ToString();
                    dr["Status"] = rm.GetString("LoginListStatus", ci);
                    dr.EndEdit();
                    dtLoginList.Rows.Add(dr);
                }
            }
        }
        grv_LoginList.DataSource = dtLoginList;
        grv_LoginList.DataBind();
    }
    public void UserRights()
    {
        try
        {
            ds_Checkuser = objCheckUser.checkRights(72, Convert.ToInt32(Session["userGroup_Id"]), "");
            if (ds_Checkuser.Tables[0].Rows.Count > 0)
            {
                if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 0)
                {
                    Response.Redirect("OrganizationLandingPage.aspx");
                }
                if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
                {
                    this.btn_Logout.Visible = true;
                }
                else
                {
                    this.btn_Logout.Visible = false;
                }
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
        }
    }
    #endregion

    #region GridviewEvents

    protected void grv_LoginList_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            //Label lbl = (Label)grv_LoginList.FindRowCellTemplateControl(e.VisibleIndex, null, "lblLoginTime");
            //lbl.ID = "lblLoginTime";
            //string[] logintime = lbl.Text.Split(' ');
            //string date1 = (Convert.ToDateTime(logintime[0])).ToString(Session["MDateFormat"].ToString());
            //string time = logintime[1] + " " + logintime[2];
            //time = DateTime.Parse(time).ToString(Session["MTimeFormat"].ToString());
            //lbl.Text = date1 + " " + time;
            //e.Row.Cells[4].Text = lbl.Text;
        }
    }
    #endregion
}
