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 SysKey : JuSNS.UI.Page.ManagePage
    {
        public static string type = "";
        public string title = "";
        protected string sort = "sys";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    type = Request.QueryString["type"];
                    if (string.IsNullOrEmpty(type))
                    {
                        type = "0";
                    }
                    if (type == "0")//关键字
                    {
                        this.txtKey.Value = JuSNS.Config.SysConfig.KeyWord;
                        title = "过滤关键字设置";
                    }
                    else if (type == "1")//姓名
                    {
                        this.txtName.Value = JuSNS.Config.SysConfig.UserName;
                        title = "会员注册禁用姓名设置";
                    }
                    else if (type == "2")//是否开启会员注册
                    {
                        this.radreg.SelectedValue = JuSNS.Common.Public.GetXMLValue("RegSwitch");
                        title = "是否开启会员注册设置";
                    }
                }
                catch
                {

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (type == "2")
            {
                setReg();
            }
            else
            {
                string configpath = "~/config/sys/system.config";
                string filepath = HttpContext.Current.Server.MapPath(configpath);
                string strNode = "";
                string StrNextNode = "";
                string strvalue = "";

                if (type == "0")//过滤关键字设置
                {
                    strNode = "filter";
                    StrNextNode = "keyword";
                    strvalue = this.txtKey.Value;

                }
                else if (type == "1")//禁止姓名注册设置
                {
                    strNode = "forbid";
                    StrNextNode = "username";
                    strvalue = this.txtName.Value;
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filepath);//你的xml文件
                XmlNodeList xmlList = xmlDoc.SelectSingleNode("system").ChildNodes;
                foreach (XmlNode xmlNo in xmlList)
                {
                    if (xmlNo.NodeType != XmlNodeType.Comment)
                    {
                        XmlElement xe = (XmlElement)xmlNo;
                        {
                            if (xe.Name == strNode)
                            {
                                xe.RemoveAll();
                                string[] value = strvalue.Split('|');

                                for (int k = 0; k < value.Length; k++)
                                {
                                    XmlNode newNode;
                                    newNode = xmlDoc.CreateNode("element", StrNextNode, "");//创建节点
                                    newNode.InnerText = value[k];//给节点赋值
                                    xe.AppendChild(newNode);//把节点添加到doc
                                }

                                xmlDoc.Save(filepath);

                                if (type == "0")
                                {
                                    JuSNS.Config.SysConfig.Reload(false);
                                }
                                else
                                {
                                    JuSNS.Config.SysConfig.Reload(true);
                                }
                                Response.Redirect("SysKey.aspx?type=" + type);
                                return;
                            }
                        }
                    }
                }
            }
        }


        public void setReg()//设置是否开启会员注册
        {
            string filepath = HttpContext.Current.Server.MapPath("~/config/sys/juSite.config");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);//你的xml文件
            XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "appsettings")
                {
                    XmlNodeList _node = element.ChildNodes;
                    for (int i = 0; i < _node.Count; i++)
                    {
                        if (_node[i].NodeType != XmlNodeType.Comment)
                        {
                            XmlElement el = (XmlElement)_node[i];
                            if (el.Attributes["key"].InnerXml == "RegSwitch")
                            {
                                el.SetAttribute("value", this.radreg.SelectedValue);
                                xmlDoc.Save(filepath);
                                PageRight("设置成功!", "SysKey.aspx?type=" + type);
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}
