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
{
    public partial class ADD_Tour : MyApp.Web.UI.UserPage
    {
        public string _Tour_ID = "";
        string _TourTemplates_Dir = "/Data/Templates/Tour";
        public string _Area_ID = "";

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Area_ID = Request.QueryString["Area_ID"].Trim();
            }
            catch (Exception) { _Area_ID = ""; }
            try
            {
                _Tour_ID = Request.QueryString["Tour_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(_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

            #region
            DataSet myds = new DataSet();
            DataTable dt_type = MyApp.Components.Area.Get_TypeTreeTable();
            myds.Tables.Add(dt_type);
            DataRow drx = dt_type.NewRow(); drx["id"] = "0"; dt_type.Rows.Add(drx);
            myds.Relations.Add("NodeRelation", dt_type.Columns["id"], dt_type.Columns["upid"]);
            PopulateSubTree(drx, 1, 0, "", true);
            #endregion
            base.DataInit();
        }


        private void PopulateSubTree(DataRow dbRow, int level, int _UpID, string ne, bool _enable_tf)
        {
            int len = dbRow.GetChildRows("NodeRelation").Length;
            int i = 0;
            string dr_head = "";
            foreach (DataRow dr in dbRow.GetChildRows("NodeRelation"))
            {
                if (level > -1)
                {
                    if (i == (len - 1))
                    {
                        dr_head = ne + "└";
                    }
                    else
                    {
                        dr_head = ne + "├";
                    }
                }

                if (_enable_tf)
                {
                    this.Area_ID.Items.Add(new ListItem(dr_head + dr["name"].ToString(), dr["id"].ToString()));
                }
                else
                {
                    this.Area_ID.Items.Add(new ListItem(dr_head + dr["name"].ToString(), "-" + dr["id"].ToString()));
                }
                if (i == len - 1)
                {
                    PopulateSubTree(dr, level + 1, Convert.ToInt32(dr["id"]), ne + "　", _enable_tf);
                }
                else
                {
                    PopulateSubTree(dr, level + 1, Convert.ToInt32(dr["id"]), ne + "│", _enable_tf);
                }
                i++;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    this.Area_ID.SelectedValue = _Area_ID.Trim();
                }
                catch
                { }
            }
        }
   
        override protected void pl_add()
        {
            this.Date.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            this.ID.Focus();
        }

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

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

                        xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/Templates/Tour/" + this.ddl_Template.SelectedValue.Trim());
                    }
                  
                    #region 赋值区域

                    #region General
                    elem_Project = (XmlElement)xmldoc.GetElementsByTagName("Project", "")[0];
                    SetElemValue(elem_Project, "Name", this.TB_Name);
                    SetElemValue(elem_Project, "Date", this.Date);
                    SetElemValue(elem_Project, "Area_ID",this._Area_ID);
                    SetElemValue(elem_Project, "Version", "3.3");

                    #endregion

                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            Directory.CreateDirectory(Server.MapPath(String.Format("/Project/{0}", _Tour_ID)));
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/pano/COPY")), Server.MapPath(String.Format("/Project/{0}", _Tour_ID)));
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID)), xmldoc.OuterXml);
                            COE.Components.MyFun.DirectoryCopy(Server.MapPath(String.Format("/Data/Templates/Tour/" + this.ddl_Template.SelectedItem.Text)), Server.MapPath(String.Format("/Project/{0}", _Tour_ID)));

                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>try{top.reloadmainBEFrame();}catch(exception){top.reloadmainFrame();};oWindow.close_dialog();</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>");
                }
            }
        }
    }
}
