using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class UserControls_TA_uc_OrganizationLevels_Mandatory : 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
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserGroupName"] != null)
        {
            dsLevels = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
        }
        int MaxLevelID = 0;
        MaxLevelID = class1.getMaxLevel();
        DataSet ds_ParentLevels = new DataSet();
        objCommon.GetParentLevels_FromLevelID(MaxLevelID);
        ds_ParentLevels = objCommon.GetParentLevels();
        if (ds_ParentLevels.Tables[0].Rows.Count > 0)
        {
            TotalLevelCount = ds_ParentLevels.Tables[0].Rows.Count;
        }
        else
        {
        }
        int i;
        int counti = 0;
        pnlLevel.Controls.Clear();
        for (i = 1; i <= TotalLevelCount; i++)
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            div.Attributes.Add("class", "col-md-2");

            Label lbl = new Label();
            lbl.Text = ds_ParentLevels.Tables[0].Rows[i - 1]["LevelName"].ToString() + "";
            div.Controls.Add(lbl);
            pnlLevel.Controls.Add(div);
            CreateDropDownList(Convert.ToString(i), i);
            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>"));
            }

        }

    }
    #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)
    {
        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();
            ddl.DataSource = dtFirstLevel;
            ddl.DataValueField = "id";
            ddl.DataTextField = "name";
            ddl.DataBind();
            ddl.CssClass = "form-control select2 Man-dat";
        }
        else
        {
            ddl.CssClass = "form-control select2";
        }
        ddl.Items.Insert(0, "-Select-");
        //ddl.CssClass = "form-control DropDownList1";
        ddl.AutoPostBack = true;
        ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        div.Controls.Add(ddl);
        pnlLevel.Controls.Add(div);
        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, "-Select-");
                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, "-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-");
                }
            }
            DropDownList ddlBeforeLevel = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId - 1));
            if (ddlBeforeLevel != null)
            {
                Session["parentLevelId"] = ddlBeforeLevel.SelectedValue;
            }
            else
            {
                Session["parentLevelId"] = 0;
            }
        }
    }
    #endregion
}
