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.house
{
    public partial class AD_HouseType : MyApp.Web.UI.UserPage
    {
        public string _Project_ID = "";
        public int _ROW = 0;
        public int _term = 0;
        public string _ID = "";
        public string _Class_ID = "";
        public string node_path = "";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
                _Class_ID = Request.QueryString["Class_ID"];
                _term = Convert.ToInt32(Request["term"]);
                _ROW = Convert.ToInt32(Request.QueryString["ROW"].Trim());
                _ID = Request.QueryString["id"];
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }

            node_path = String.Format("/Project/TypeSet/Class/item[@id={0}]/housetype[@id='{1}']",_term,_ID);
            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(String.Format("/Project/TypeSet/Class/item[@id={0}]",_term));
            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);
                GetElemValue(elem_item, "url", this.url);
                GetElemValue(elem_item, "type", this.type);
                GetElemValue(elem_item, "size", this.size);
                GetElemValue(elem_item, "usersize", this.usersize);
                GetElemValue(elem_item, "count", this.count);
                GetElemValue(elem_item, "pano", this.pano);
                GetElemValue(elem_item, "visible", this.visible);
                GetElemValue(elem_item, "locktf", this.locktf);
                GetElemValue(elem_item, "intro", this.intro);
                GetElemValue(elem_item, "memo", this.memo);

            }
            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("housetype");
                        SetElemValue(elem_item, "id", System.Guid.NewGuid().ToString().Replace("-", "_"));
                    }
                    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, "name", this.name);
                    SetElemValue(elem_item, "url", this.url);
                    SetElemValue(elem_item, "type", this.type);
                    SetElemValue(elem_item, "size", this.size);
                    SetElemValue(elem_item, "usersize", this.usersize);
                    SetElemValue(elem_item, "count", this.count);
                    SetElemValue(elem_item, "pano", this.pano);
                    SetElemValue(elem_item, "visible", this.visible);
                    SetElemValue(elem_item, "locktf", this.locktf);
                    SetElemValue(elem_item, "intro", this.intro);
                    SetElemValue(elem_item, "memo", this.memo);
                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, String.Format("/Project/TypeSet/Class/item[@id={0}]", _term));
                            xmldoc.SelectSingleNode(String.Format("/Project/TypeSet/Class/item[@id={0}]", _term)).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 ex)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('" + ex.ToString() + "!!!');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 ex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('"+ex.ToString()+"!!!');history.back()</script>");
                }
            }
        }
    }
}
