using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web;
public partial class UserControls_TA_uc_MainMenu : System.Web.UI.UserControl
{
    ResourceManager rm;
    cls_Common objClsCommon = new cls_Common();
    LoginListEventData loginList = new LoginListEventData();
    EmployeeEventData ObjEmployeeEventData = new EmployeeEventData();
    CultureInfo ci;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["EmployeeId"] != null)
        {
            string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
            if (sUser == null || sUser == String.Empty)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                string cValues = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
                string[] cData = cValues.Split(',');
                string cSessionId = cData[2].ToString();
                if (Session.SessionID != cSessionId)
                {
                    Response.Redirect("../index.aspx");
                }
            }
        }
        if (Convert.ToString(Session["user"]).Length > 15)
        {
            lbl_user.Text = Convert.ToString(Session["user"]).Substring(0, 15) + "...";
            lbl_user.ToolTip = Convert.ToString(Session["user"]);
        }
        else
        {
            lbl_user.Text = Convert.ToString(Session["user"]);
            lbl_user.ToolTip = Convert.ToString(Session["user"]);
        }
        loadMenu();
        //get the culture info to set the language
        if (Session["Session_ActiveLanguage"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        else
        {
            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;
            LoadStrings(ci);
        }
        lbtn_logout.OnClientClick = "javascript:return confirm('" + rm.GetString("Alert_Logout", ci) + "');";
    }
    private void loadMenu()
    {
        string source_Language = string.Empty;
        if (Session["Session_ActiveLanguage"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        else
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
        }
        string strType = string.Empty;
        if (Session["Demo"] != null)
        {
            if (Session["Demo"].ToString() == "Standard")
            {
                strType = "Standard";
            }
            else if (Session["Demo"].ToString() == "Express")
            {
                strType = "Express";
            }
        }
        cls_Common getmenu = new cls_Common();
        DataSet ds = new DataSet();
        DataSet dsChildMenus = new DataSet();
        DataSet dsReportsMenu = new DataSet();
        DataSet dsReportsSubMenu = new DataSet();
        int intReportsMenu = 0;
        int intReportsSubMenu = 0;
        int MenuNo = 1;
        this.divMenu.InnerHtml = "<div id='divMenu' class='menu'>";
        this.divMenu.InnerHtml = "<ul class='tabs'>";
        if (Convert.ToInt16(Session["userGroup_Id"]) != 0)
        {
            ds = getmenu.getUserMenu(0, Convert.ToInt16(Session["userGroup_Id"]), strType);
            for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
            {
                String txt = ds.Tables[0].Rows[iloop]["displayName"].ToString();
                this.divMenu.InnerHtml += "<li class='hasmore'><a href='../" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '><span>" + rm.GetString(txt, ci) + "</span> </a>";
                dsChildMenus = getmenu.getUserMenu(Convert.ToInt32(ds.Tables[0].Rows[iloop]["pageid"]), Convert.ToInt16(Session["userGroup_Id"]), strType);
                this.divMenu.InnerHtml += "<ul class='dropdown'>";
                for (int iloopChild = 0; iloopChild <= dsChildMenus.Tables[0].Rows.Count - 1; iloopChild++)
                {
                    string txtChild = dsChildMenus.Tables[0].Rows[iloopChild]["displayName"].ToString();
                    string[] splittedPath = ds.Tables[0].Rows[iloop]["path"].ToString().Split("/".ToCharArray());
                    if (dsChildMenus.Tables[0].Rows[iloopChild]["path"].ToString() != string.Empty)
                    {
                        this.divMenu.InnerHtml += "<li class='hasmore'><a href='../" + splittedPath[0] + "/" + dsChildMenus.Tables[0].Rows[iloopChild]["path"] + "' title='" + rm.GetString(txtChild, ci) + " '>" + rm.GetString(txtChild, ci) + " </a>";
                    }
                    else
                    {
                        this.divMenu.InnerHtml += "<li class='hasmore'><a title='" + rm.GetString(txtChild, ci) + " '><span class='nav-arrow'>" + rm.GetString(txtChild, ci) + "</span> </a>";
                    }
                    dsReportsMenu = getmenu.getReportMenu("ReportsMenu", Convert.ToInt16(Session["userGroup_Id"]));
                    for (intReportsMenu = 0; intReportsMenu <= dsReportsMenu.Tables[0].Rows.Count - 1; intReportsMenu++)
                    {
                        if (dsChildMenus.Tables[0].Rows[iloopChild]["pageId"].ToString() == dsReportsMenu.Tables[0].Rows[intReportsMenu]["ParentIdForSubMenu"].ToString())
                        {
                            dsReportsSubMenu = getmenu.getReportMenu(dsReportsMenu.Tables[0].Rows[intReportsMenu]["ParentIdForSubMenu"].ToString(), Convert.ToInt16(Session["userGroup_Id"]), strType);
                            this.divMenu.InnerHtml += "<ul class='dropdown'>";
                            for (intReportsSubMenu = 0; intReportsSubMenu <= dsReportsSubMenu.Tables[0].Rows.Count - 1; intReportsSubMenu++)
                            {
                                string txtChild1 = dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["displayName"].ToString();
                                this.divMenu.InnerHtml += "<li class='hasmore'><a href='../" + splittedPath[0] + "/" + dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["path"] + "' title='" + rm.GetString(txtChild1, ci) + " '>" + rm.GetString(txtChild1, ci) + "</a></li>";
                            }
                            this.divMenu.InnerHtml += "</ul>";
                        }
                    }
                    this.divMenu.InnerHtml += "</li>";
                }
                this.divMenu.InnerHtml += "</ul>";
                this.divMenu.InnerHtml += "</li>";
                dsChildMenus.Clear();
                dsChildMenus.Dispose();
                MenuNo = MenuNo + 1;
            }
        }
        this.divMenu.InnerHtml += "</ul>";
        this.divMenu.InnerHtml += "</div>";
        ds.Clear();
        ds.Dispose();
        string sYearValue = Convert.ToString(Cache["ActiveYearSetting"]);
        if (sYearValue != null && sYearValue != "")
        {
            lblYear.Text = rm.GetString("Year", ci);
            lblYearData.Text = sYearValue;
            if (lblYearData.Text.Contains("January"))
            {
                lblYearData.Text = lblYearData.Text.Replace("January", "Jan");
            }
            if (lblYearData.Text.Contains("February"))
            {
                lblYearData.Text = lblYearData.Text.Replace("February", "Feb");
            }
            if (lblYearData.Text.Contains("March"))
            {
                lblYearData.Text = lblYearData.Text.Replace("March", "Mar");
            }
            if (lblYearData.Text.Contains("April"))
            {
                lblYearData.Text = lblYearData.Text.Replace("April", "Apr");
            }
            if (lblYearData.Text.Contains("May"))
            {
                lblYearData.Text = lblYearData.Text.Replace("May", "May");
            }
            if (lblYearData.Text.Contains("June"))
            {
                lblYearData.Text = lblYearData.Text.Replace("June", "Jun");
            }
            if (lblYearData.Text.Contains("July"))
            {
                lblYearData.Text = lblYearData.Text.Replace("July", "Jul");
            }
            if (lblYearData.Text.Contains("August"))
            {
                lblYearData.Text = lblYearData.Text.Replace("August", "Aug");
            }
            if (lblYearData.Text.Contains("September"))
            {
                lblYearData.Text = lblYearData.Text.Replace("September", "Sep");
            }
            if (lblYearData.Text.Contains("October"))
            {
                lblYearData.Text = lblYearData.Text.Replace("October", "Oct");
            }
            if (lblYearData.Text.Contains("November"))
            {
                lblYearData.Text = lblYearData.Text.Replace("November", "Nov");
            }
            if (lblYearData.Text.Contains("December"))
            {
                lblYearData.Text = lblYearData.Text.Replace("December", "Dec");
            }
        }
        else
        {
            lblYearData.Text = string.Empty;
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Login.Text = rm.GetString("Login", ci);
        lbtn_logout.Text = rm.GetString("btn_Logout", ci);
    }
    protected void lbtn_logout_Click(object sender, EventArgs e)
    {
        if (Session["EmployeeId"] != null)
        {
            string userId = Session["EmployeeId"].ToString();
            string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
            if (sUser != null && sUser != String.Empty)
            {
                ObjEmployeeEventData.InsertUpdateLastLogout(userId);
                HttpContext.Current.Cache.Remove(Session["EmployeeId"].ToString().ToUpper());
                Session.Clear();
            }
        }
        HttpContext.Current.Response.Redirect("../Index.aspx");
    }
}