using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
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;

namespace MYAPP.Manage.CustomMAP.Project.Marker
{
    [AjaxPro.AjaxNamespace("AjaxDemo")]
    public partial class index : MyApp.Web.UI.UserPage
    {
        public string _Project_ID = "";

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }

        override protected void DataInit()//数据初始化
        {

        }

        protected override void pl_register_script()
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(index));
            base.pl_register_script();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildTree();
            }
        }

        protected void BuildTree()
        {
            XmlDocument xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);

            TV_Tree.Nodes.Clear();
            ComponentArt.Web.UI.TreeViewNode Project_Node = CreateNode(GetValue("/Project", "Name"), "0", true, "root.gif", "", "mainFrame", false);
            TV_Tree.Nodes.Add(Project_Node);

            DataSet myds = new DataSet();
            DataTable dt_class=MyApp.Components.CustomMap.Project.GetMarkerClass(_Project_ID);
            myds.Tables.Add(dt_class);
            DataRow dr = dt_class.NewRow(); dr["id"] = "0"; dt_class.Rows.Add(dr);
            myds.Relations.Add("NodeRelation", dt_class.Columns["id"], dt_class.Columns["upid"]);

            PopulateSubTree(dr, Project_Node);
        }

        private void PopulateSubTree(DataRow dbRow, ComponentArt.Web.UI.TreeViewNode node)
        {
            int len = dbRow.GetChildRows("NodeRelation").Length;

            foreach (DataRow dr in dbRow.GetChildRows("NodeRelation"))
            {
                ComponentArt.Web.UI.TreeViewNode newNode = CreateNode( dr["id"]+":"+dr["name"].ToString(), dr["id"].ToString(), true, GetValue(string.Format("/Project/TypeSet/MarkerType/item[@id={0}]", dr["type"]), "icon"), GetValue(string.Format("/Project/TypeSet/MarkerType/item[@id={0}]", dr["type"]), "nav_list").Replace("{Project_ID}",_Project_ID.ToString()).Replace("{Class_ID}", dr["id"].ToString()), "mainFrame", false);
                node.Nodes.Add(newNode);
                PopulateSubTree(dr,  newNode);
            }
        }

        private ComponentArt.Web.UI.TreeViewNode CreateNode(string name, string id, bool expanded, string imageurl, string navurl, string target, bool _ShowCheckBox)
        {
            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = name;
            node.NavigateUrl = navurl;
            node.Target = target;
            node.Value = id.ToString();
            node.ImageUrl = imageurl;
            node.Expanded = expanded;
            node.ShowCheckBox = _ShowCheckBox;
            return node;
        }

        [AjaxPro.AjaxMethod]
        public string GetValue(string _Project_ID, string path, string attribute)
        {
            return MyApp.Components.CustomMap.Project.GetValue(_Project_ID, path,  attribute);
        }

        public string GetValue(string path, string attribute)
        {
            return GetValue(_Project_ID, path, attribute);
        }
    }
}