using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace MYAPP.Manage.Project.Design
{
    [AjaxPro.AjaxNamespace("AjaxDemo")]
    public partial class List_Tour : MyApp.Web.UI.UserPage
    {
        public int PageRecord = 15, NowPage, RecordCount, hRecordCount, MaxPage;
        public DataTable list_dt;
        public string _Area_ID = "";
        protected override void AppInit()
        {
            try
            {
                _Area_ID = Request.QueryString["Area_ID"].Trim();
            }
            catch (Exception) { _Area_ID = ""; }
            base.AppInit();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            MyApp.Components.NavUrl.SetUrl(1);
            try
            {
                Page.DataBind();
            }
            catch (Exception) { }

        }

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

        protected override void DataInit()
        {
            if (_Area_ID == "-1")
            {
                list_dt = MyApp.Components.KRPano.Project.GetDirectorys("", "", PageRecord, ref NowPage, ref RecordCount, ref hRecordCount, ref MaxPage);
            }
            else
            {
                list_dt = MyApp.Components.KRPano.Project.GetDirectorys("", String.Format("Area_ID='{0}'", _Area_ID), PageRecord, ref NowPage, ref RecordCount, ref hRecordCount, ref MaxPage);
                //Response.Write(list_dt.Rows.Count.ToString());
            }

            #region
            DataSet myds = new DataSet();
            DataTable dt_type = MyApp.Components.Area.Get_TypeTreeTable();
            myds.Tables.Add(dt_type);
            DataRow dr = dt_type.NewRow(); dr["id"] = "0"; dt_type.Rows.Add(dr);
            myds.Relations.Add("NodeRelation", dt_type.Columns["id"], dt_type.Columns["upid"]);
            PopulateSubTree(dr, 1, 0, "", true);
            #endregion
            this.Area_ID.SelectedValue = _Area_ID;
            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++;
            }
        }

        public string GetElemValueByPath(object tour_id, string attribute)
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(tour_id))))
            {
                Directory.Delete(HttpContext.Current.Server.MapPath(MyApp.Components.KRPano.Project.Get_Pprject_Dir(tour_id)), true);
            }

            string _File_Dir = Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(tour_id));
            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 "　";
            }
        }

        public string GetElemValueByPath(object tour_id)
        {
            string attribute = "Version";
            string _File_Dir = Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(tour_id));
            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;
            double _version = 0.0;

            try
            {
                _version = Convert.ToDouble(xmlelem.GetAttribute(attribute).Trim());
            }
            catch {
                _version = 0.0;
            }

            if (xmlelem.GetAttribute(attribute).Trim() == "")
            {
                return "value='升迁'";
            }
            else if (xmlelem.GetAttribute(attribute).Trim() == "3.3")
            {
                return "value='V" + xmlelem.GetAttribute(attribute).Trim() + "'   disabled='disabled'";
            }
            else
            {
                return "value='V" + xmlelem.GetAttribute(attribute).Trim() + "'";
            }
        }



        [AjaxPro.AjaxMethod]
        public bool MoveTour(string _Tour_ID, string _ToArea_ID)
        {
            var list_dt = _Tour_ID.Split(',');
            try
            {
                foreach (string tour_id in list_dt)
                {
                    XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(tour_id));
                    XmlElement elem_krpano = (XmlElement)xmldoc.GetElementsByTagName("Project", "")[0];
                    SetElemValue(elem_krpano, "Area_ID", _ToArea_ID);

                    MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(tour_id)), xmldoc.OuterXml);
                }
                return true;
            }
            catch (Exception) { return false; }
        }

        public string GetDateYMD(string _time)
        {
            string _stime = "";

            string[] strs = _time.Split('/');
            if (strs.Length > 1)
            {
                for (int i = strs.Length-1; i >= 0; i--)
                {
                    _stime +=strs[i] + "-";
                    //_stime += Convert.ToInt32(strs[i]).ToString("00") + "-";
                }
                _stime = _stime.TrimEnd('-');
            }
            else
            {
               _stime = _time;
            }

            return _stime;
        }
    }
}
