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 EmailSTMP : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "sys";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getInfo();
            }
        }

        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void getInfo()
        {
            this.txtHost.Text = JuSNS.Config.EmailConfig.host;
            this.txtPort.Text = JuSNS.Config.EmailConfig.port.ToString();
            this.txtFrom.Text = JuSNS.Config.EmailConfig.from;
            this.txtUserName.Text = JuSNS.Config.EmailConfig.username;
            this.txtPwd.Text = JuSNS.Config.EmailConfig.password;
            this.txtSSL.Text = JuSNS.Config.EmailConfig.enablessl.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string configpath = "~/config/email/email.config";
            string filepath = HttpContext.Current.Server.MapPath(configpath);
            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 == "setting")//begin if
                {
                    XmlNodeList tmpXmlNodeList = xmlNodeList.Item(i).ChildNodes;

                    for (int j = 0; j < tmpXmlNodeList.Count; j++)//begin for
                    {
                        if (tmpXmlNodeList.Item(j).Name == "parameter")//begin if
                        {
                            if (tmpXmlNodeList[j].NodeType != XmlNodeType.Comment)
                            {
                                XmlElement xmlElement = (XmlElement)tmpXmlNodeList.Item(j);
                                // xmlElement.SetAttribute
                                //更改parameter节点的值
                                xmlElement.SetAttribute("host", this.txtHost.Text);
                                xmlElement.SetAttribute("port", this.txtPort.Text);
                                xmlElement.SetAttribute("from", this.txtFrom.Text);
                                xmlElement.SetAttribute("username", this.txtUserName.Text);
                                if (this.txtPwd.Text != "")
                                {
                                    xmlElement.SetAttribute("password", this.txtPwd.Text);
                                }
                                if (this.txtSSL.Text.ToLower() == "true")
                                    xmlElement.SetAttribute("enablessl", "true");
                                else
                                    xmlElement.SetAttribute("enablessl", "false");

                                xmlDocument.Save(filepath);
                                JuSNS.Config.EmailConfig.Reload();
                                Response.Redirect("emailStmp.aspx");
                                
                            }
                        }
                    }
                }
            }
        }
    }
}
