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.ExportTemplate
{
    [AjaxPro.AjaxNamespace("AjaxDemo")]
    public partial class index : MyApp.Web.UI.UserPage
    {
        public string _Tour_ID = "";

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

        override protected void DataInit()//数据初始化
        {

        }

        protected override void pl_register_script()
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(index));
            base.pl_register_script();
        }

        override protected void pl_add()
        {
            this.Date.Text = System.DateTime.Now.ToShortDateString();
            this.Name.Focus();
        }

        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();

            if (Page.IsValid)
            {
                string _template_name = String.Format("/Data/Templates/Tour/{0}", this.Name.Text.Trim());
                try
                {
                    if (action == "edit")
                    {
                        if (File.Exists(Server.MapPath(String.Format("/Data/Templates/Tour/{0}.xml", this.Name.Text.Trim()))))
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('模板名称重复!!!');</script>");
                            action = "exit";
                        }
                    }
                    #region 
                   
                    #endregion
                    if (action == "edit")
                    {
                        try
                        {
                            File.Copy(Server.MapPath(String.Format("/Project/{0}/project.xml", _Tour_ID)), Server.MapPath(String.Format("/Data/Templates/Tour/{0}.xml", this.Name.Text.Trim())), true);
                            Directory.CreateDirectory(Server.MapPath(_template_name));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/Accessories", _Tour_ID)),Server.MapPath(_template_name+"/Accessories"));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/images", _Tour_ID)), Server.MapPath(_template_name + "/images"));
                            //COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/js", _Tour_ID)), Server.MapPath(_template_name + "/js"));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/Plugin", _Tour_ID)), Server.MapPath(_template_name + "/Plugin"));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/Skin", _Tour_ID)), Server.MapPath(_template_name + "/Skin"));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Project/{0}/templates", _Tour_ID)), Server.MapPath(_template_name + "/templates"));

                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('模板导出成功!!!');</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>");
                }
            }
        }
    }
}