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.Tour.Map
{
    public partial class AD_Map_Scene : MyApp.Web.UI.UserPage
    {
        int _Index_ID = 0;
        public string _Tour_ID = "";
        public int _MapIndex_ID = 0;

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Index_ID = Convert.ToInt32(Request["Index_ID"]);
                _MapIndex_ID = Convert.ToInt32(Request["MapIndex_ID"]);
                _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();
        }

        override protected void DataInit()//数据初始化
        {
            #region //填充场景列表
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            DataRow[] list_dr = MyApp.Components.KRPano.Project.GetDirectorys(String.Format("{0}/Scene", _Tour_ID),"");
            List<ListItem> li = new List<ListItem>();
            foreach (DataRow dr in list_dr)
            {
                XmlNodeList xmlnodelist = xmldoc.SelectNodes(String.Format("/Project/Maps/Map[position()=" + _MapIndex_ID + "]/point[@Scene_ID={0}]", dr["Name"].ToString()));
                if (xmlnodelist.Count == 0)
                {
                    li.Add(new ListItem(dr["Name"].ToString() + ":" + GetElemValueByPath(_Tour_ID, dr["Name"], "Name"), dr["Name"].ToString()));
                }
            }
            this.ddl_Scene_ID.Items.AddRange(li.ToArray());
            #endregion
        }

        public string GetElemValueByPath(object _Tour_ID, object scene_id, string attribute)
        {
            string _File_Dir = Server.MapPath(String.Format("/Project/{0}/Scene/{1}/scene.xml", _Tour_ID, scene_id.ToString()));
            StreamReader sr = System.IO.File.OpenText(_File_Dir);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(sr.ReadToEnd());
            sr.Close();
            XmlNode xmlnode = xmldoc.ChildNodes[0];
            XmlElement xmlelem = (XmlElement)xmlnode;
            if (xmlelem.GetAttribute(attribute).Trim() != "")
            {
                return xmlelem.GetAttribute(attribute).Trim();
            }
            else
            {
                return "　";
            }
        }

        protected override void pl_register_script()
        {
            this.ddl_Scene_ID.Attributes.Add("onchange", "javascrpt:{$('Scene_ID').value=this.value;}");
            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.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            XmlNode xmlnode = xmldoc.SelectSingleNode("/Project/Maps/Map[position()=" + _MapIndex_ID + "]");
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/Project/Maps/Map[position()=" + _MapIndex_ID + "]/point");
            try
            {
                if (_Index_ID == -1)
                {
                    foreach (XmlElement xmlelem in xmlnodelist)
                    {
                        xmlnode.RemoveChild(xmlelem);
                    }
                }
                else
                {
                    if (xmlnodelist.Count >= _Index_ID)
                    {
                        xmlnode.RemoveChild(xmlnodelist[_Index_ID - 1]);
                    }
                }
                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID)), xmldoc.OuterXml);
                Response.Redirect(MyApp.Components.NavUrl.GetUrl(4));
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }
        override protected void pl_add()
        {
            this.x.Text = Request["x"];
            this.y.Text = Request["y"];
        }

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

            try
            {
                XmlElement xmlelem = (XmlElement)xmldoc.SelectNodes("/Project/Maps/Map[position()=" + _MapIndex_ID + "]/point")[_Index_ID - 1];

                GetElemValue(xmlelem, "Scene_ID", this.Scene_ID);
                GetElemValue(xmlelem, "x", this.x);
                GetElemValue(xmlelem, "y", this.y);
                GetElemValue(xmlelem, "hint", this.hint);

                #region ICON
                GetElemValue(xmlelem, "icon_init", this.icon_init, true);
                GetElemValue(xmlelem, "icon_visit", this.icon_visit, true);
                GetElemValue(xmlelem, "icon_onhover", this.icon_onhover, true);
                GetElemValue(xmlelem, "icon_active", this.icon_active, true);
                #endregion

                #region 事件
                GetElemValue(xmlelem, "onover", this.onover, true);
                GetElemValue(xmlelem, "onhover", this.onhover, true);
                GetElemValue(xmlelem, "onout", this.onout, true);
                GetElemValue(xmlelem, "onclick", this.onclick, true);
                GetElemValue(xmlelem, "ondown", this.ondown, true);
                GetElemValue(xmlelem, "onup", this.onup, true);
                GetElemValue(xmlelem, "onloaded", this.onloaded, true);
                #endregion

                this.ddl_Scene_ID.SelectedValue = this.Scene_ID.Text;
            }
            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 xmlelem;
            if (Page.IsValid)
            {
                try
                {
                    if (action == "add")
                    {
                        xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
                        xmlelem = xmldoc.CreateElement("point");
                    }
                    else
                        if (action == "edit")
                        {
                            xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
                            xmlelem = (XmlElement)xmldoc.SelectNodes("/Project/Maps/Map[position()=" + _MapIndex_ID + "]/point")[_Index_ID - 1];
                        }
                        else
                        {
                            return;
                        }
                    #region 赋值区域


                    try
                    {
                        this.x.Text = Convert.ToInt32(this.x.Text).ToString();
                        this.y.Text = Convert.ToInt32(this.y.Text).ToString();
                    }
                    catch (Exception)
                    {
                        this.x.Text = "0";
                        this.y.Text = "0";
                    }

                    if (this.hint.Text.Trim() == "")
                    {
                        this.hint.Text = GetElemValueByPath(_Tour_ID, this.Scene_ID.Text.Trim(), "Name");
                    }

                    SetElemValue(xmlelem, "Scene_ID", this.Scene_ID);
                    SetElemValue(xmlelem, "x", this.x);
                    SetElemValue(xmlelem, "y", this.y);
                    SetElemValue(xmlelem, "hint", this.hint);


                    #region ICON
                    SetElemValue(xmlelem, "icon_init", this.icon_init);
                    SetElemValue(xmlelem, "icon_visit", this.icon_visit);
                    SetElemValue(xmlelem, "icon_onhover", this.icon_onhover);
                    SetElemValue(xmlelem, "icon_active", this.icon_active);
                    #endregion

                    #region 事件
                    SetElemValue(xmlelem, "onover", this.onover);
                    SetElemValue(xmlelem, "onhover", this.onhover);
                    SetElemValue(xmlelem, "onout", this.onout);
                    SetElemValue(xmlelem, "onclick", this.onclick);
                    SetElemValue(xmlelem, "ondown", this.ondown);
                    SetElemValue(xmlelem, "onup", this.onup);
                    SetElemValue(xmlelem, "onloaded", this.onloaded);
                    #endregion

                    #endregion

                    if (action == "add")
                    {
                        try
                        {
                            xmldoc.SelectSingleNode("/Project/Maps/Map[position()=" + _MapIndex_ID + "]").AppendChild(xmlelem);
                            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.xreload();top.close_dialog();</script>");
                        }
                        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(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID)), xmldoc.OuterXml);
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.xreload();top.close_dialog();</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>");
                }
            }
        }
    }
}
