using System;
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;
using System.Xml;

namespace JuSNS.Web.system.sys
{
    public partial class EmailSys : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "sys";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /// <summary>
                /// 加载页面初始信息
                /// </summary>
                try
                {
                    string type = Request.QueryString["type"];

                    if (type == "0")
                    {
                        this.txtContent.Value = JuSNS.Config.EmailConfig.register;
                        this.selType.SelectedIndex = int.Parse(type);
                    }
                    else if (type == "1")
                    {
                        this.txtContent.Value = JuSNS.Config.EmailConfig.invite;
                        this.selType.SelectedIndex = int.Parse(type);
                    }
                    else if (type == "2")
                    {
                        this.txtContent.Value = JuSNS.Config.EmailConfig.retrieve;
                        this.selType.SelectedIndex = int.Parse(type);
                    }
                    else
                    {
                        this.txtContent.Value = JuSNS.Config.EmailConfig.register;
                    }
                }
                catch
                {

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string configpath = "~/config/email/email.config";
            string filepath = HttpContext.Current.Server.MapPath(configpath);
            string strNode = "";

            if (this.selType.Value == "0")
            {
                strNode = "register";
            }
            else if (this.selType.Value == "1")
            {
                strNode = "invite";
            }
            else if (this.selType.Value == "2")
            {
                strNode = "retrievepwd";
            }

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filepath);
            XmlNodeList xmlNodeList = xmlDocument.DocumentElement.ChildNodes;

            for (int i = 0; i < xmlNodeList.Count; i++)//begin for
            {
                //只更改appSettings节点下的子点节的值
                if (xmlNodeList.Item(i).Name == "matter")//begin if
                {
                    XmlNodeList tmpXmlNodeList = xmlNodeList.Item(i).ChildNodes;

                    for (int j = 0; j < tmpXmlNodeList.Count; j++)//begin for
                    {
                        if (tmpXmlNodeList.Item(j).Name == strNode)//begin if
                        {
                            if (tmpXmlNodeList[j].NodeType != XmlNodeType.Comment)
                            {
                                XmlElement xmlElement = (XmlElement)tmpXmlNodeList.Item(j);

                                //删除register节点
                                xmlElement.RemoveChild(tmpXmlNodeList.Item(j).FirstChild);
                                XmlNode newNode;
                                newNode = xmlDocument.CreateNode("cdatasection", strNode, "");//创建节点
                                newNode.InnerText = this.txtContent.Value;//给节点赋值
                                xmlElement.AppendChild(newNode);//把节点添加到doc

                                xmlDocument.Save(filepath);
                                JuSNS.Config.EmailConfig.Reload();
                                PageRight("发送邮件内容编辑成功!", "EmailSys.aspx");
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}
