using System;
using System.IO;
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 List_Polygon : MyApp.Web.UI.UserPage
    {

        public string _Project_ID = "";
        public string _Class_ID = "";
        public string _Type_ID = "";
        public DataTable list_dt;
        public int _ROW = 0;

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
                _Class_ID = Request.QueryString["Class_ID"].Trim();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
            _Type_ID = MyApp.Components.CustomMap.Project.GetValue(_Project_ID,string.Format("/Project/TypeSet/Class/item[@id={0}]", _Class_ID), "type");
        }

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

        override protected void DataInit()//数据初始化
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
            this.xds.Data = xmldoc.OuterXml;

            #region
            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, 1, 0, "", true);
            #endregion
            this.Class_ID.SelectedValue = _Class_ID;
            base.DataInit();
        }

        private void PopulateSubTree(DataRow dbRow, int level, int _UpID, string ne, bool _enable_tf)
        {
            int len = dbRow.GetChildRows("NodeRelation").Length;
            int i = 0;
            string dr_head = "";
            foreach (DataRow dr in dbRow.GetChildRows("NodeRelation"))
            {
                if (level > -1)
                {
                    if (i == (len - 1))
                    {
                        dr_head = ne + "└";
                    }
                    else
                    {
                        dr_head = ne + "├";
                    }
                }

                if (Math.Abs(Convert.ToDecimal(dr["type"])) == Math.Abs(Convert.ToDecimal(_Type_ID)))
                {
                    this.Class_ID.Items.Add(new ListItem(dr_head + dr["name"].ToString(), dr["id"].ToString()));
                }
                else
                {
                    this.Class_ID.Items.Add(new ListItem(dr_head + dr["name"].ToString(), "-" + dr["id"].ToString()));
                }
                if (i == len - 1)
                {
                    PopulateSubTree(dr, level + 1, Convert.ToInt32(dr["id"]), ne + "　", _enable_tf);
                }
                else
                {
                    PopulateSubTree(dr, level + 1, Convert.ToInt32(dr["id"]), ne + "│", _enable_tf);
                }
                i++;
            }
        }

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

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToDecimal(_Type_ID)>0)
            {
                this.xds.XPath = String.Format("/Project/MapMarker/Polygons/item[@classid={0}]", _Class_ID);
            }
            else
            {
                this.xds.XPath = String.Format("/Project/MapMarker/Polygons/item");
            }
            MyApp.Components.NavUrl.SetUrl(2);
            try
            {
                Page.DataBind();
            }
            catch (Exception) { }
        }

        [AjaxPro.AjaxMethod]
        public string MoveInfo(object _Project_ID, string _id, string _ToClassID)
        {
            var list_id = _id.Split(',');
            try
            {
                XmlDocument xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID.ToString());
                foreach (string info_id in list_id)
                {
                    XmlElement xmlelem = (XmlElement)xmldoc.SelectSingleNode(String.Format("/Project/MapMarker/Polygons/item[@id='{0}']", info_id));
                    SetElemValue(xmlelem, "classid", _ToClassID);
                }
                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(string.Format(MyApp.Components.CustomMap.Project._filename,_Project_ID.ToString())), xmldoc.OuterXml);
                return "true";
            }
            catch (Exception ex) { return ex.ToString(); }
        }
    }
}
