using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Drawing;
using System.Collections.Generic;

public partial class left : System.Web.UI.Page
{

    
    
    
    

    string codiceFiscaleCliente;
    string tipoRete;
    string codPB;
    string token;

    //------------------------------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {

        codiceFiscaleCliente = Request.QueryString.Get("codCliente");
        tipoRete = Request.QueryString.Get("tipoRete");
        codPB = Request.QueryString.Get("codPB");
        token = Request.QueryString.Get("token");

        if (!Page.IsPostBack)
            PopulateRootLevel();
    }
    //------------------------------------------------------------------------
    private void PopulateRootLevel()
    {
        DataTable dt = DataAccess.ExecuteDataTable(DBProvider.SqlServer,CommandType.StoredProcedure,"sp_getMenuElements");
        PopulateNodes(dt,tv.Nodes);
    }
    //------------------------------------------------------------------------
    private void PopulateSubLevel(int parentid, TreeNode parentNode)
    {
        List<Parametro> parametri = new List<Parametro>();
        Parametro parametro = new Parametro();
        parametro.DbType = DbType.Int32;
        parametro.ParameterName = "pID";
        parametro.Value = parentid;
        parametri.Add(parametro);

        DataTable dt = DataAccess.ExecuteDataTableStoredProcedure(DBProvider.SqlServer, "sp_getMenuElements_ByParentID", parametri);
        PopulateNodes(dt, parentNode.ChildNodes);
    }
    //------------------------------------------------------------------------
    protected void tv_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        //QUI FIDEURAM VUOLE TUTTA LA TV APERTA FIN DAL PRINCIPIO
        //PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
    }
    //------------------------------------------------------------------------
    private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            string pageName = dr["page_name"].ToString();
            string aspxPagePath = dr["aspxPagePath"].ToString();
            tn.Text = dr["logical_name"].ToString();
            tn.Value = dr["id"].ToString();

            int flag = 0;
            int leaf = 0;
            int rc = 0;
            string [] flatTree;
            int maxDim = 3;
            flatTree = new string[maxDim]; //3 livelli...

            if (pageName != "" || aspxPagePath != "")   //check: se � una foglia deve avere un url "valido"
            {
                    if (aspxPagePath != "")
                    {
                        switch (aspxPagePath)
                        {
                            case "manageErrors.aspx":
                                tn.Target = "top.parent.CENTER";
                                rc = 1;
                                break;

                            case "manageActivities.aspx":
                                tn.Target = "top.parent.CENTER";
                                rc = 1;
                                break;

                            default:   //le altre pagine dovrebbero essere visibili a "TUTTI"
                                tn.Target = "top.parent.CENTER";
                                rc = 1;
                                break;
                        }
                    }
                    else
                    {
                        rc = 1; 
                    }
                switch (rc) //0: Neanche vede l'oggetto, 1: solo lettura, 2: lettura scrittura
                {
                    case 0:
                        flag = 1;
                        break;
                    case 1:
                        tn.Target = "CENTER";
                        if (aspxPagePath == "")
                        {
                            tn.NavigateUrl = pageName + "?codiceFiscale=" + codiceFiscaleCliente + "&codicePB=" + codPB + "&codiceRete=" + tipoRete + "&token=" + token;
                            leaf = 1;
                        }
                        else
                        {
                            tn.NavigateUrl = aspxPagePath;
                        }
                        break;
                }
            }

            if (flag == 0)
            {
                nodes.Add(tn);

                if (leaf == 1)
                {
                    int idx = 0;
                    leaf = 0;

                    flatTree[idx] = tn.Text.ToUpper();
                    idx++;

                    TreeNode tnN = tn.Parent;
                    while (tnN != null)
                    {
                        flatTree[idx] += tnN.Text.ToUpper();
                        tnN = tnN.Parent;
                        idx++;
                    }
                    //ordiniamo il flat tree view
                    string flatTreeView = "";
                    for (int j = maxDim-1; j>-1; j--)
                    {
                        if (flatTree[j] != null)
                        {
                            flatTreeView += flatTree[j];
                            if(j!=0) flatTreeView += "-->";
                        }
                    }
                    tn.NavigateUrl += "&flatTreeView=" + flatTreeView;
                }
            }

            //Se il nodo ha figli allora abilito l'on-demand populating.
            //tn.PopulateOnDemand = (Convert.ToInt32((dr["childnodecount"])) > 0);
           
            
            //In questo caso, invece, carico tutto alla prima lettura...per cui...
            if (Convert.ToInt32((dr["childnodecount"])) > 0)
            {
                PopulateSubLevel(Int32.Parse(tn.Value), tn); 
                tn.Expand();
            }

        }
    }

//----------------------------------------------------------------------------------
    protected void tv_SelectedNodeChanged(object sender, EventArgs e)
    {
        //QUI, invece, FIDEURAM VUOLE TUTTO APERTO.

        //tv.CollapseAll();  //chiudo TUTTO...

        //if (tv.SelectedNode.Parent != null)  //e poi riapro quel che mi interessa.
        //{
        //    tv.SelectedNode.Parent.Expand();
        //    if (tv.SelectedNode.Parent.Parent != null)
        //        tv.SelectedNode.Parent.Parent.Expand();  //visto che abbiamo al max 3 LIVELLI...
        //}

        //tv.SelectedNode.Expand();
    }
    //-----------------------------------------------------------------------------------
    protected void expandParent(TreeNode node)
    {
        if (node.Parent != null) 
        {
            node.Expand();
            expandParent(node.Parent);
        }
    }
    //-----------------------------------------------------------------------------------


}