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.Project.Design.MProject
{
    public partial class AD_MProject_Info : MyApp.Web.UI.UserPage
    {
        public string _Tour_ID = "";
        int _count = 0;
        DataTable dt;

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }
        protected override void pl_register_script()
        {
            base.pl_register_script();
        }
        protected void Page_Init(object sender, EventArgs e)
        {

            if (COE.Components.MyFun.GetNumber() > 10000)
            {

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本不是企业增强版!!!');</script>");
                Response.End();
            }

            dt = new DataTable();
            dt.Columns.Add("id", System.Type.GetType("System.Int32"));
            dt.Columns.Add("name", System.Type.GetType("System.String"));
            dt.Columns.Add("funname", System.Type.GetType("System.String"));
            dt.Columns.Add("xpath", System.Type.GetType("System.String"));
            dt.Columns.Add("keyrecursion", System.Type.GetType("System.String"));
            dt.Columns.Add("sort", System.Type.GetType("System.Int32"));

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (COE.Components.MyFun.GetNumber() > 10000)
            {

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本不是企业增强版!!!');</script>");
                Response.End();
            }

            if (!Page.IsPostBack)
            {


                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(MyApp.Components.KRPano.Project.Project_Name_Config(_Tour_ID))))
                {

                }
                else
                {
                    Response.Write("请先导入配置文件!!");
                    Response.End();

                }

                XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Project_Name_Config(_Tour_ID));

                XmlNodeList xmlnodelist = xmldoc.SelectNodes("/Project/MProject"); //xmldoc.ChildNodes;


                DataRow dr = null;
                for (int i = 0; i < xmlnodelist.Count; i++)
                {
                     if (xmlnodelist[i].NodeType == XmlNodeType.Element)
                    {

                        XmlElement xle = (XmlElement)xmlnodelist[i];
                        if (xle.GetAttribute("funenable") == "true")
                        {
                            _count++;
                            dr = dt.NewRow();
                            dr["id"] = _count;
                            dr["name"] = xle.GetAttribute("name");
                            dr["funname"] = xle.GetAttribute("funname");
                            dr["keyrecursion"] = xle.GetAttribute("keyrecursion");
                            dr["sort"] = xle.GetAttribute("funsort");
                            dr["xpath"] = xle.GetAttribute("xpath");
                            dt.Rows.Add(dr);
                        }
                        BindNodeView(xle);
                    }
                }
               DataView dw = dt.DefaultView;
               dw.Sort = "sort";
               this.rp_list.DataSource = dw;
               this.rp_list2.DataSource = dw;

                try
                {
                    Page.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }

        protected void BindNodeView(XmlElement xlle)
        {
            XmlNodeList xmlnodelist = xlle.ChildNodes;
            DataRow dr = null;

            for (int i = 0; i < xmlnodelist.Count; i++)
            {
                if (xmlnodelist[i].NodeType == XmlNodeType.Element)
                {
                    XmlElement xle = (XmlElement)xmlnodelist[i];

                    if (xle.GetAttribute("funenable") == "true")
                    {
                        _count++;
                        dr = dt.NewRow();
                        dr["id"] = _count;
                        dr["name"] = xle.GetAttribute("name");
                        dr["funname"] = xle.GetAttribute("funname");
                        dr["keyrecursion"] = xle.GetAttribute("keyrecursion");
                        dr["sort"] = xle.GetAttribute("funsort");
                        dr["xpath"] = xle.GetAttribute("xpath");
                        dt.Rows.Add(dr);
                    }
                    BindNodeView(xle);
               }
            }
        }

        override protected void pl_edit()
        {
            XmlDocument xmldoc;
            XmlElement elem_mproject;
            xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));

            try
            {
                elem_mproject = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/MProject");
                GetElemValue(elem_mproject, "Name", this.MP_Name, true);
                GetElemValue(elem_mproject, "Intro", this.Intro, 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_mproject;

            if (Page.IsValid)
            {
                try
                {
                    if (action == "edit")
                    {
                        xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
                    }

                    #region 赋值区域
                    elem_mproject = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/MProject");


                    if (elem_mproject != null)
                    {
                        SetElemValue(elem_mproject, "Name", this.MP_Name);
                        SetElemValue(elem_mproject, "Intro", this.Intro);
                    }
                    else
                    {
                        XmlNode xmlnodexml = xmldoc.SelectSingleNode("config");
                        elem_mproject = (XmlElement)xmldoc.CreateElement("MProject");

                        SetElemValue(elem_mproject, "Name", this.MP_Name);
                        SetElemValue(elem_mproject, "Intro", this.Intro);
                        xmlnodexml.AppendChild(elem_mproject);
                    }

                    #endregion
                    if (action == "edit")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID)), xmldoc.OuterXml);
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.updatetour();</script>");
                        }
                        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>");
                }
            }
        }
    }
}
