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.Project.Design.Scene
{
    public partial class ADD_Scene : MyApp.Web.UI.UserPage
    {
        public string _Scene_ID = "", _Tour_ID = "", _TypeID = "";
        public string _filename = "/Project/{0}/scene/{1}/scene.xml";
        string _TourTemplates_Dir = "/Data/Templates/Scene";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _TypeID = Request.QueryString["TypeID"].Trim();
            }
            catch (Exception) { _TypeID = ""; }

            if (_TypeID == "-1") { _TypeID = ""; }
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
                _Scene_ID = Request.QueryString["Scene_ID"].Trim();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }

            if (MyApp.Components.KRPano.Project.GetTourSceneCount(String.Format("{0}/Scene", _Tour_ID)) >= COE.Components.MyFun.GetNumber())
            {
                ButPost.Enabled = false;
                if (COE.Components.MyFun.GTX()!="localhost")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本(KRPanoGUI" + COE.Components.MyFun.bb[COE.Components.MyFun.GetNumber().ToString()] + ")最多只能添加" + COE.Components.MyFun.GetNumber().ToString() + "个场景!!!');</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本(KRPanoGUI测试版)最多只能添加" + COE.Components.MyFun.GetNumber().ToString() + "个场景!!!');</script>");
                }
                return;
            }
            base.AppInit();
        }

        override protected void DataInit()//数据初始化
        {
            #region //填充模板列表
            MyApp.Components.Accessories acc = new MyApp.Components.Accessories();
            DataTable dt = acc.GetFiles(Server.MapPath(_TourTemplates_Dir), "*.xml");
            foreach (DataRow dr in dt.Rows)
            {
                this.ddl_Template.Items.Add(new ListItem(dr["File_Name"].ToString(), dr["File_Path"].ToString()));
            }
            #endregion
        }

        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_add()
        {
            this.Date.Text = System.DateTime.Now.ToString("yyyy-MM-dd"); //ToShortDateString();
            this.ID.Focus();
        }
        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            if (Page.IsValid)
            {
                _Scene_ID = Convert.ToInt64(this.ID.Text.Trim()).ToString();
                try
                {
                    if (action == "add")
                    {
                        if (Directory.Exists(Server.MapPath(String.Format("/Project/{0}/Scene/{1}", _Tour_ID, _Scene_ID))))
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('ID Existed!!!');</script>");
                            return;
                        }
                        xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/Templates/Scene/" + this.ddl_Template.SelectedValue.Trim());
                    }

                    #region 赋值区域

                    #region General
                    XmlElement elem_Project = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                    SetElemValue(elem_Project, "TypeID", _TypeID);
                    SetElemValue(elem_Project, "Name", this.Name);
                    SetElemValue(elem_Project, "Date", this.Date);
                    #endregion

                    #region SSet
                    XmlElement elem_Sset = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/SSet");
                    SetElemValue(elem_Sset, "Scene_ID", _Scene_ID);
                    SetElemValue(elem_Sset, "Scene_Name", this.Name); 
                    SetElemValue(elem_Sset, "TypeID", _TypeID);
                    #endregion

                    #endregion

                    if (action == "add")
                    {
                        try
                        {
                            Directory.CreateDirectory(Server.MapPath(String.Format("/Project/{0}/scene/{1}", _Tour_ID, _Scene_ID)));
                            MyApp.Components.KRPano.Scene.UploadScenePic("b", _Tour_ID, _Scene_ID);
                            MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);

                            //MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/scene/{1}/include_scene.xml", _Tour_ID, _Scene_ID)), xmldoc.ChildNodes[0].InnerXml);
                            //ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.updatescenetree();top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + MyApp.Components.KRPano.Scene.GetValue(MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID), "/krpano", "TypeID") + "';top.close_dialog();</script>");
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.addscene(" + _Scene_ID + ");top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + MyApp.Components.KRPano.Scene.GetValue(MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID), "/krpano", "TypeID") + "';top.close_dialog();</script>");
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!);</script>"); }
                    }
                }
                catch (Exception)
                {
                    ButPost.Enabled = false;
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');</script>");
                    return;
                }
            }
        }
    }
}
