using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class UserControls_Ta_uc_OrganizationLevels_Modify : System.Web.UI.UserControl
{
    #region Variable Declaration
    cls_Common objCommon = new cls_Common();
    cls_master_levels class1 = new cls_master_levels();
    DataSet dsLevels = new DataSet();
    int TotalLevelCount = 0;
    #endregion
    #region Page Load Events
    /// <summary>
    /// On page loading
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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"]));
        }
        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();
        for (i = 1; i <= TotalLevelCount; i++)
        {
            Literal ltSpaceText = new Literal();
            ltSpaceText.Text = "&nbsp;&nbsp;";
            pnlLevel.Controls.Add(ltSpaceText);
            if (i == 1)
            {
                Label lbl = new Label();
                lbl.Text = ds_ParentLevels.Tables[0].Rows[i - 1]["LevelName"].ToString() + " : ";
                lbl.Width = 76;
                pnlLevel.Controls.Add(lbl);
                Label lbl1 = new Label();
                lbl1.Text = " * ";
                lbl1.ForeColor = System.Drawing.Color.Red;
                lbl1.Width = 4;
                pnlLevel.Controls.Add(lbl1);
            }
            else
            {
                Label lbl = new Label();
                lbl.Text = ds_ParentLevels.Tables[0].Rows[i - 1]["LevelName"].ToString() + " : ";
                lbl.Width = 80;
                pnlLevel.Controls.Add(lbl);
            }
            CreateDropDownList(Convert.ToString(i), i);
            if (i % 2 == 0)
            {
                Literal ltText = new Literal();
                ltText.Text = "<br /><br />";
                pnlLevel.Controls.Add(ltText);
            }
        }
        //if (!IsPostBack)
        //{
        //    // To make the default company to load and based on that the second level will also be loaded.
        //    DropDownList ddlFirstLevel = (DropDownList)pnlLevel.FindControl(Convert.ToString(1));
        //    if (ddlFirstLevel != null)
        //    {
        //        ddlFirstLevel.SelectedIndex = 1;
        //        OnSelectedIndexChanged(ddlFirstLevel, EventArgs.Empty);
        //    }
        //}
        //}
    }
    #endregion
    #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)
    {
        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();
            ddl.DataSource = dtFirstLevel;
            ddl.DataValueField = "id";
            ddl.DataTextField = "name";
            ddl.DataBind();
        }
        ddl.Items.Insert(0, "-Select-");
        ddl.CssClass = "dlist";
        ddl.AutoPostBack = true;
        ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlLevel.Controls.Add(ddl);
        //if (Count == 1)
        //{
        //ddl.SelectedIndex = 1;
        //LoadDropDown(1,Convert.ToInt32(ddl.SelectedValue));
        //}
        //else if (Count == 2)
        //{
        //    DropDownList ddlFirstLevel = (DropDownList)pnlLevel.FindControl(Convert.ToString(1));
        //    LoadDropDown(1, Convert.ToInt32(ddlFirstLevel.SelectedValue));
        //}
        Literal ltText = new Literal();
        ltText.Text = "&nbsp;&nbsp;";
        pnlLevel.Controls.Add(ltText);
    }
    /// <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);
        }
        //Page.RegisterClientScriptBlock("Alert", "<script type = 'text/javascript'>alert('" + ID +" fired SelectedIndexChanged event');</script>");
    }
    /// <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, "-Select-");
            }
            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, "-Select-");
                }
            }
        }
        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, "-Select-");
                }
            }
        }
    }
    #endregion
}
