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
{
    public partial class AD_Marker : MyApp.Web.UI.UserPage
    {
        public string _Project_ID = "";
        public string _Class_ID = "";
        public string _ID = "";
        public int _ROW = 0;

        public string node_path = "";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
                _Class_ID = Request.QueryString["Class_ID"];
                _ID = Request.QueryString["ID"];
                _ROW = Convert.ToInt32(Request.QueryString["ROW"].Trim());
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }


            node_path = "/Project/MapMarker/Markers/item[@id='{0}']";

            base.AppInit();
        }

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

        }

        protected override void pl_register_script()
        {
            base.pl_register_script();
        }

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

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        override protected void pl_del()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
            XmlNode xmlnode = xmldoc.SelectSingleNode("/Project/MapMarker/Markers");
            XmlNodeList xmlnodelist = xmldoc.SelectNodes(node_path);
            try
            {
                if (_ROW == -1)
                {
                    foreach (XmlElement elem_item in xmlnodelist)
                    {
                        xmlnode.RemoveChild(elem_item);
                    }
                }
                else
                {
                    xmlnode.RemoveChild((XmlElement)xmldoc.SelectSingleNode(String.Format(node_path, _ID)));
                }
                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                Response.Redirect(MyApp.Components.NavUrl.GetUrl(2));
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }
        override protected void pl_add()
        {
            this.name.Focus();
        }
        override protected void pl_edit()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);

            try
            {
                XmlElement elem_item = (XmlElement)xmldoc.SelectSingleNode(String.Format(node_path, _ID));

                GetElemValue(elem_item, "name", this.name, true);
                GetElemValue(elem_item, "address", this.address, true);
                GetElemValue(elem_item, "phone", this.phone, true);
                GetElemValue(elem_item, "web", this.web, true);
                GetElemValue(elem_item, "spic", this.spic, true);
                GetElemValue(elem_item, "logo", this.logo, true);
                GetElemValue(elem_item, "sintro", this.sintro, true);
                GetElemValue(elem_item, "intro", this.intro, true);
                GetElemValue(elem_item, "openurl", this.openurl, true);
                GetElemValue(elem_item, "hint", this.hint, true);
                GetElemValue(elem_item, "visible", this.visible, true);

                GetElemValue(elem_item, "lng", this.lng, true);
                GetElemValue(elem_item, "lat", this.lat, true);
                GetElemValue(elem_item, "zoom", this.zoom, true);

                GetElemValue(elem_item, "url", this.url, true);
                GetElemValue(elem_item, "style", this.style, true);
                GetElemValue(elem_item, "width", this.width, true);
                GetElemValue(elem_item, "height", this.height, true);
                GetElemValue(elem_item, "x", this.x, true);
                GetElemValue(elem_item, "y", this.y, true);

                GetElemValue(elem_item, "click", this.click, true);
                GetElemValue(elem_item, "mouseover", this.mouseover, true);

            }
            catch (Exception ex)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!" + ex.ToString() + "');history.back()</script>");
                return;
            }
        }

        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlElement elem_item;
            if (Page.IsValid)
            {
                try
                {
                    if (action == "add")
                    {
                        xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
                        elem_item = xmldoc.CreateElement("item");
                        SetElemValue(elem_item, "id", System.Guid.NewGuid().ToString().Replace("-", "_"));
                        SetElemValue(elem_item, "classid", _Class_ID);
                    }
                    else
                        if (action == "edit")
                        {
                            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
                            elem_item = (XmlElement)xmldoc.SelectSingleNode(String.Format(node_path, _ID));
                        }
                        else
                        {
                            return;
                        }
                    #region 赋值区域
                    SetElemValue(elem_item, "id", elem_item.GetAttribute("id").Replace("-", "_"));

                    SetElemValue(elem_item, "name", this.name);
                    SetElemValue(elem_item, "address", this.address);
                    SetElemValue(elem_item, "web", this.web);
                    SetElemValue(elem_item, "phone", this.phone);
                    SetElemValue(elem_item, "spic", this.spic);
                    SetElemValue(elem_item, "logo", this.logo);
                    SetElemValue(elem_item, "sintro", this.sintro);
                    SetElemValue(elem_item, "intro", this.intro);
                    SetElemValue(elem_item, "openurl", this.openurl);
                    SetElemValue(elem_item, "hint", this.hint);
                    SetElemValue(elem_item, "visible", this.visible);

                    SetElemValue(elem_item, "lng", this.lng);
                    SetElemValue(elem_item, "lat", this.lat);
                    SetElemValue(elem_item, "zoom", this.zoom);

                    SetElemValue(elem_item, "url", this.url);
                    SetElemValue(elem_item, "style", this.style);
                    SetElemValue(elem_item, "width", this.width);
                    SetElemValue(elem_item, "height", this.height);
                    SetElemValue(elem_item, "x", this.x);
                    SetElemValue(elem_item, "y", this.y);

                    SetElemValue(elem_item, "click", this.click);
                    SetElemValue(elem_item, "mouseover", this.mouseover);
                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/MapMarker/Markers");
                            xmldoc.SelectSingleNode("/Project/MapMarker/Markers").AppendChild(elem_item);
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(2));
                        }
                        catch (Exception)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                        }
                    }
                    if (action == "edit")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(2));
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>"); }
                    }


                }
                catch (Exception)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                }
            }
        }
    }
}
