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.CustomMAP.Project
{
    public partial class AD_Project : MyApp.Web.UI.UserPage
    {
        public string _Project_ID = "";
        
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Project_ID = Request.QueryString["Project_ID"].Trim();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }

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


        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_del()
        {
            pl_edit();

            try
            {
                if (this.LockTF.Checked)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert(\"This Project is Lock! You can't delete it!\");history.back()</script>");
                }
                else
                {
                    Directory.Delete(Server.MapPath(String.Format("/map/Project/{0}", _Project_ID)), true);
                    Response.Redirect(MyApp.Components.NavUrl.GetUrl(1));
                }
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }
        override protected void pl_add()
        {
            this.Date.Text = System.DateTime.Now.ToShortDateString();
            this.ID.Focus();
        }
        override protected void pl_edit()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);

            try
            {
                this.ID.Text = _Project_ID;
                #region General
                XmlElement elem_Project = (XmlElement)xmldoc.GetElementsByTagName("Project", "")[0];
                GetElemValue(elem_Project, "Name", this.Name, true);
                GetElemValue(elem_Project, "LockTF", this.LockTF, true);
                GetElemValue(elem_Project, "Date", this.Date, true);
                //GetElemValue(elem_Project, "Intro", this.Intro, true);
                #endregion

                #region MAPSet
                XmlElement elem_MAPSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/MAPSet");
                GetElemValue(elem_MAPSet, "MinZoom", this.MinZoom, true);
                GetElemValue(elem_MAPSet, "MaxZoom", this.MaxZoom, true);
                GetElemValue(elem_MAPSet, "TitleWidth", this.TitleWidth, true);
                GetElemValue(elem_MAPSet, "TitleHeight", this.TitleHeight, true);
                GetElemValue(elem_MAPSet, "DefZoom", this.DefZoom, true);
                GetElemValue(elem_MAPSet, "DefLat", this.DefLat, true);
                GetElemValue(elem_MAPSet, "DefLng", this.DefLng, true);
                GetElemValue(elem_MAPSet, "ImgURL", this.ImgURL, true);
                #endregion

                #region Intro
                XmlElement elem_Intro = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Intro");
                GetElemValue(elem_Intro, "content", this.Intro, true);
                #endregion

                #region Google
                XmlElement elem_Google = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Google");
                GetElemValue(elem_Google, "lng", this.google_lng, true);
                GetElemValue(elem_Google, "lat", this.google_lat, true);
                GetElemValue(elem_Google, "zoom", this.google_zoom, true);

                GetElemValue(elem_Google, "url", this.google_icon_url, true);
                GetElemValue(elem_Google, "style", this.google_icon_style, true);
                GetElemValue(elem_Google, "width", this.google_icon_width, true);
                GetElemValue(elem_Google, "height", this.google_icon_height, true);
                GetElemValue(elem_Google, "x", this.google_icon_x, true);
                GetElemValue(elem_Google, "y", this.google_icon_y, true);
                #endregion

                #region Announce
                XmlElement elem_Announce = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Announce");
                GetElemValue(elem_Announce, "content", this.Announce_content, true);
                #endregion

                #region Contacts
                XmlElement elem_Contacts = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Contacts");
                GetElemValue(elem_Contacts, "content", this.Contacts_content, true);
                #endregion

                #region SiteSet
                XmlElement elem_SiteSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/SiteSet");
                GetElemValue(elem_SiteSet, "logo", this.site_logo, true);
                GetElemValue(elem_SiteSet, "bbs", this.site_bbs, true);
                GetElemValue(elem_SiteSet, "status", this.site_status, true);
                #endregion

                #region TourSet
                XmlElement elem_TourSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/TourSet");
                GetElemValue(elem_TourSet, "TourID", this.TourID, true);
                GetElemValue(elem_TourSet, "MarkerType", this.MarkerType, true);
                #endregion
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            this.ID.Enabled = false;
        }

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

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

                        xmldoc = LoadXML("", String.Format("<Project/>"));
                    }
                    if (action == "edit")
                    {
                        xmldoc = MyApp.Components.CustomMap.Project.GetProjectXmlDoc(_Project_ID);
                    }
                    #region 赋值区域

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

                    #region MAPSet
                    XmlElement elem_MAPSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/MAPSet");
                    SetElemValue(elem_MAPSet, "MinZoom", this.MinZoom);
                    SetElemValue(elem_MAPSet, "MaxZoom", this.MaxZoom);
                    SetElemValue(elem_MAPSet, "TitleWidth", this.TitleWidth);
                    SetElemValue(elem_MAPSet, "TitleHeight", this.TitleHeight);
                    SetElemValue(elem_MAPSet, "DefZoom", this.DefZoom);
                    SetElemValue(elem_MAPSet, "DefLat", this.DefLat);
                    SetElemValue(elem_MAPSet, "DefLng", this.DefLng);
                    SetElemValue(elem_MAPSet, "ImgURL", this.ImgURL);
                    #endregion

                    #region Intro
                    XmlElement elem_Intro = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Intro");
                    SetElemValue(elem_Intro, "content", this.Intro);
                    #endregion

                    #region Google
                    XmlElement elem_Google = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Google");
                    SetElemValue(elem_Google, "lng", this.google_lng);
                    SetElemValue(elem_Google, "lat", this.google_lat);
                    SetElemValue(elem_Google, "zoom", this.google_zoom);

                    SetElemValue(elem_Google, "url", this.google_icon_url);
                    SetElemValue(elem_Google, "style", this.google_icon_style);
                    SetElemValue(elem_Google, "width", this.google_icon_width);
                    SetElemValue(elem_Google, "height", this.google_icon_height);
                    SetElemValue(elem_Google, "x", this.google_icon_x);
                    SetElemValue(elem_Google, "y", this.google_icon_y);
                    #endregion

                    #region Announce
                    XmlElement elem_Announce = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Announce");
                    SetElemValue(elem_Announce, "content", this.Announce_content);
                    #endregion

                    #region Contacts
                    XmlElement elem_Contacts = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/Contacts");
                    SetElemValue(elem_Contacts, "content", this.Contacts_content);
                    #endregion

                    #region SiteSet
                    XmlElement elem_SiteSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/SiteSet");
                    SetElemValue(elem_SiteSet, "logo", this.site_logo);
                    SetElemValue(elem_SiteSet, "bbs", this.site_bbs);
                    SetElemValue(elem_SiteSet, "status", this.site_status);
                    #endregion

                    #region TourSet
                    XmlElement elem_TourSet = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/Project/TourSet");
                    SetElemValue(elem_TourSet, "TourID", this.TourID);
                    SetElemValue(elem_TourSet, "MarkerType", this.MarkerType);
                    #endregion

                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            Directory.CreateDirectory(Server.MapPath(String.Format("/map/Project/{0}", _Project_ID)));
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(1));
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>"); }
                    }
                    if (action == "edit")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(MyApp.Components.CustomMap.Project._filename, _Project_ID)), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(1));
                        }
                        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>");
                }
            }
        }
    }
}
