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
{
    public partial class AD_GeneralIntro : MyApp.Web.UI.UserPage
    {
        public string _Project_ID = "";
        public int _ROW = 0;
        
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
                _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;
            }
            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/GeneralIntro");
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/Project/GeneralIntro/Field");
            try
            {
                if (_ROW == -1)
                {
                    foreach (XmlElement elem_item in xmlnodelist)
                    {
                        xmlnode.RemoveChild(elem_item);
                    }
                }
                else
                {
                    if (xmlnodelist.Count >= _ROW)
                    {
                        xmlnode.RemoveChild(xmlnodelist[_ROW - 1]);
                    }
                }
                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                Response.Redirect(MyApp.Components.NavUrl.GetUrl(11));
            }
            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.SelectNodes("/Project/GeneralIntro/Field")[_ROW - 1];
                GetElemValue(elem_item, "name", this.name, true);
                GetElemValue(elem_item, "value", this.value, true);
                GetElemValue(elem_item, "class", this.class_, true);

                GetElemValue(elem_item, "click", this.click, true);
                GetElemValue(elem_item, "mouseover", this.mouseover, true);
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');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("Field");
                    }
                    else
                        if (action == "edit")
                        {
                            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
                            elem_item = (XmlElement)xmldoc.SelectNodes("/Project/GeneralIntro/Field")[_ROW - 1];
                        }
                        else
                        {
                            return;
                        }
                    #region 赋值区域
                    SetElemValue(elem_item, "name", this.name);
                    SetElemValue(elem_item, "value", this.value);
                    SetElemValue(elem_item, "class", this.class_);

                    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/GeneralIntro");
                            xmldoc.SelectSingleNode("/Project/GeneralIntro").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(11));
                        }
                        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(11));
                        }
                        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>");
                }
            }
        }
    }
}
