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.Object
{
    public partial class ADD_Object : MyApp.Web.UI.UserPage
    {
        public string _Tour_ID = "";
        public string _Object_ID = "";
        string _ObjectTemplates_Dir = "/Data/Templates/Object/";

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

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


      
        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");
            this.ID.Focus();
        }

        override protected void pl_del()
        {
            try
            {
                Directory.Delete(Server.MapPath(String.Format("/Project/{0}/Object/{1}", _Tour_ID,_Object_ID)), true);
                Response.Redirect(Request.UrlReferrer.ToString());
            }
            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_Project;

            if (Page.IsValid)
            {
                _Object_ID = Convert.ToInt64(this.ID.Text.Trim()).ToString();
                try
                {
                    if (action == "add")
                    {
                        if (Directory.Exists(Server.MapPath(String.Format("/Project/{0}/Object/{1}", _Tour_ID,_Object_ID))))
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('ID Existed!!!');</script>");
                            return;
                        }

                        xmldoc = MyApp.Components.XML.XmlDocument.Load(_ObjectTemplates_Dir + this.ddl_Template.SelectedValue.Trim());
                    }

                    #region 赋值区域

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

                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            Directory.CreateDirectory(MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID));

                            MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Object.GetObjectServerFilePath(_Tour_ID,_Object_ID), xmldoc.OuterXml);
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format(_ObjectTemplates_Dir + this.ddl_Template.SelectedItem.Text)), Server.MapPath(String.Format("/Project/{0}/Object/{1}", _Tour_ID, _Object_ID)));

                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>try{top.reloadmainBEFrame();}catch(exception){top.reloadmainFrame();};</script>");
                        }
                        catch (Exception )
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');</script>");
                        }
                    }
                }
                catch (Exception )
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');</script>");
                }
            }
        }
    }
}
