using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
public partial class UserControls_TA_uc_OrganizationLevels : System.Web.UI.UserControl
{
    #region Variable Declaration
    public event EventHandler ehClick;
    cls_Common objCommon = new cls_Common();
    cls_master_levels class1 = new cls_master_levels();
    DataSet dsLevels = new DataSet();
    DataTable dsEmailLevels = new DataTable();
    ResourceManager rm;
    CultureInfo ci;
    int TotalLevelCount = 0;
    int EmailParentId = 0;
    bool isBreak = false;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["OrgLevels"] != null)
        //{
        //    dsLevels = (DataSet)Session["OrgLevels"];
        //}
        if (Session["UserGroupName"] != null)
        {
            dsLevels = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
        }
        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;
        int MaxLevelID = 0;
        //if (Session["orgLevelid"] != null)
        //{
        //    MaxLevelID = Convert.ToInt32(Session["orgLevelid"]);
        //}
        //else
        //{
        MaxLevelID = class1.getMaxLevel();
        //}
        DataSet ds_ParentLevels = new DataSet();
        objCommon.GetParentLevels_FromLevelID(MaxLevelID);
        ds_ParentLevels = objCommon.GetParentLevels();
        if (ds_ParentLevels.Tables[0].Rows.Count > 0)
        {
            //if (Session["orgLevelid"] != null)
            //{
            //    TotalLevelCount = (ds_ParentLevels.Tables[0].Rows.Count) - 1;
            //}
            //else
            //{
            TotalLevelCount = ds_ParentLevels.Tables[0].Rows.Count;
            //}
        }
        else
        {
        }
        int i;
        pnlLevel.Controls.Clear();
        if (dsLevels.Tables.Count > 0)
        {
            for (i = 1; i <= TotalLevelCount; i++)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Attributes.Add("class", "col-md-2");
                //Literal ltSpaceText = new Literal();
                //ltSpaceText.Text = "&nbsp;&nbsp;";
                //pnlLevel.Controls.Add(ltSpaceText);
                Label lbl = new Label();
                lbl.Text = ds_ParentLevels.Tables[0].Rows[i - 1]["LevelName"].ToString();
                //lbl.Width = 80;
                div.Controls.Add(lbl);
                pnlLevel.Controls.Add(div);
                CreateDropDownList(Convert.ToString(i), i, sender, e);
                pnlLevel.Controls.Add(new LiteralControl("<div class='col-md-1'></div>"));
                if (i % 2 == 0)
                {
                    pnlLevel.Controls.Add(new LiteralControl("<div class='clearfix form-group'></div>"));
                    // pnlLevel.Controls.Add(new LiteralControl("<div class='row'></div>"));
                }

            }
        }

        int DropDownId = 0;
        //This Line Added by karthikeyan
        if (!IsPostBack)
        {
            if (Convert.ToString(Request.QueryString["Source"]) != null)
            {
                if (Convert.ToString(Request.QueryString["Source"]) == "Email" || Convert.ToString(Request.QueryString["Source"]) == "SMSTemplate")
                {
                    if (Convert.ToString(Session["EMailParentId"]) != string.Empty)
                    {
                        EmailParentId = Convert.ToInt32(Session["EMailParentId"]);
                        dsEmailLevels = objCommon.GetParentLevelIdsForEmailScheduler(EmailParentId);
                        if (dsEmailLevels.Rows.Count > 0)
                        {
                            for (int ii = 1; ii <= TotalLevelCount; ii++)
                            {
                                DropDownList ddl = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId + ii));
                                foreach (ListItem item in ddl.Items)
                                {
                                    if (item.Value != "-Select-")
                                    {
                                        for (int j = 0; j < dsEmailLevels.Rows.Count; j++)
                                        {
                                            if (Convert.ToInt32(dsEmailLevels.Rows[j][0]) == Convert.ToInt32(item.Value))
                                            {
                                                ddl.SelectedValue = Convert.ToString(dsEmailLevels.Rows[j][0]);
                                                OnSelectedIndexChanged(ddl, null);
                                                isBreak = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (isBreak == true)
                                    {
                                        isBreak = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    #region User Defined Functions
    /// <summary>
    /// To create the drop down dynamically
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Count"></param>
    private void CreateDropDownList(string ID, int Count, object sender, EventArgs e)
    {
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.Attributes.Add("class", "col-md-3");
        DropDownList ddl = new DropDownList();
        ddl.ID = ID;
        if (Count == 1)
        {
            DataTable dtFirstLevel = new DataTable();
            dsLevels.Tables[0].DefaultView.RowFilter = "ParentId = 0";
            dtFirstLevel = dsLevels.Tables[0].DefaultView.ToTable();
            ViewState["EmailLevelIdsFirstLevel"] = dtFirstLevel;
            ddl.DataSource = dtFirstLevel;
            ddl.DataValueField = "id";
            ddl.DataTextField = "name";
            ddl.DataBind();
        }
        ddl.Items.Insert(0, rm.GetString("Select", ci));
        ddl.CssClass = "form-control select2";
        ddl.Style.Add("width", "100% !important");
        ddl.AutoPostBack = true;
        ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        div.Controls.Add(ddl);
        pnlLevel.Controls.Add(div);
        //Literal ltText = new Literal();
        //ltText.Text = "&nbsp;&nbsp;";
        //pnlLevel.Controls.Add(ltText);
        //Bug No : 0033625 as per the Testing team suggestion we are remove tool tip by Karthikeyan.R
        //foreach (ListItem item in ddl.Items)
        //{
        //    item.Attributes.Add("title", item.Text);
        //}
    }
    /// <summary>
    /// Event handler on drop down selected index changed.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// 
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string DropDownID = ddl.ID;
        if (ddl.SelectedIndex != 0)
        {
            int SelectedValue = Convert.ToInt32(ddl.SelectedValue);
            Session["parentLevelId"] = SelectedValue;
            LoadDropDown(Convert.ToInt32(DropDownID), SelectedValue);
        }
        else
        {
            LoadDropDown(Convert.ToInt32(DropDownID), -1);
        }
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "LoadDropdownScript()", true);
    }
    /// <summary>
    /// To load the drop down values.
    /// </summary>
    /// <param name="DropDownId"></param>
    /// <param name="SelectedLevel"></param>
    private void LoadDropDown(int DropDownId, int SelectedLevel)
    {
        if (SelectedLevel != -1)
        {
            DropDownList ddl = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId + 1));
            if (ddl != null)
            {
                ddl.Items.Clear();
                DataTable dtLevel = new DataTable();
                dsLevels.Tables[0].DefaultView.RowFilter = "ParentId = " + SelectedLevel;
                dtLevel = dsLevels.Tables[0].DefaultView.ToTable();
                ddl.DataSource = dtLevel;
                ddl.DataTextField = "name";
                ddl.DataValueField = "id";
                ddl.DataBind();
                ddl.Items.Insert(0, rm.GetString("Select", ci));
                //Bug No : 0033625 as per the Testing team suggestion we are remove tool tip by Karthikeyan.R
                //foreach (ListItem item in ddl.Items)
                //{
                //    item.Attributes.Add("title", item.Text);
                //}
            }
            int i = 0;
            for (i = DropDownId + 2; i <= TotalLevelCount; i++)
            {
                DropDownList ddlChilds = (DropDownList)pnlLevel.FindControl(Convert.ToString(i));
                if (ddlChilds != null)
                {
                    ddlChilds.Items.Clear();
                    ddlChilds.Items.Insert(0, rm.GetString("Select", ci));
                }
            }
        }
        else
        {
            int i = 0;
            for (i = DropDownId + 1; i <= TotalLevelCount; i++)
            {
                DropDownList ddl = (DropDownList)pnlLevel.FindControl(Convert.ToString(i));
                if (ddl != null)
                {
                    ddl.Items.Clear();
                    ddl.Items.Insert(0, rm.GetString("Select", ci));
                }
            }
            DropDownList ddlBeforeLevel = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId - 1));
            if (ddlBeforeLevel != null)
            {
                Session["parentLevelId"] = ddlBeforeLevel.SelectedValue;
            }
            else
            {
                Session["parentLevelId"] = 0;
            }
        }
    }
    #endregion
}
