using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Data;
using xabaraCOM;

public partial class WebXabara_frame_systemSet : System.Web.UI.Page
{
    admin a = new admin();
    javasrcipt js = new javasrcipt();

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check("|systemSet|");

        if (!IsPostBack)
        {
            cache.clear();

            regExpression reg = new regExpression();
            emailRegularExpressionValidator.ValidationExpression = reg.email;
            emailRegularExpressionValidator.ErrorMessage = reg.emailError;
            telRegularExpressionValidator.ValidationExpression = reg.phone;
            telRegularExpressionValidator.ErrorMessage = reg.phoneError;
            faxRegularExpressionValidator.ValidationExpression = reg.phone;
            faxRegularExpressionValidator.ErrorMessage = reg.phoneError;
            mobileRegularExpressionValidator.ValidationExpression = reg.mobile;
            mobileRegularExpressionValidator.ErrorMessage = reg.mobileError;
            urlRegExpress.ValidationExpression = reg.url;
            urlRegExpress.ErrorMessage = reg.urlError;

            string pathFlie = HttpContext.Current.Server.MapPath("/xabara.config");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFlie);

            webName.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='webName']").Attributes["value"].Value;
            webDomains.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='webDomains']").Attributes["value"].Value;
            webBak.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='miibeian']").Attributes["value"].Value;
            webQQ.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='qq']").Attributes["value"].Value;
            wangwang.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='taobao']").Attributes["value"].Value;
            webEmail.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='webEmail']").Attributes["value"].Value;
            webtel.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='webTel']").Attributes["value"].Value;
            webFax.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='webFax']").Attributes["value"].Value;
            webMobile.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='mobile']").Attributes["value"].Value;
            freePhone.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='freePhone']").Attributes["value"].Value;
            add.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='add']").Attributes["value"].Value;
            ips.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='ip']").Attributes["value"].Value;
            waterWords.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='waterMark']").Attributes["value"].Value;
            keyWords.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='KeyWord']").Attributes["value"].Value;
            mKeyWords.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='metaStr']").Attributes["value"].Value;
            titleMeta.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='titleMeta']").Attributes["value"].Value;
            countStr.Text = xmlDoc.DocumentElement.SelectSingleNode("//add[@key='countScript']").Attributes["value"].Value;

            xmlDoc = null;

            ipAddress i = new ipAddress();
            ipAdd.Text = i.address();
        }
    }

    protected void waterImgButton_Click(object sender, EventArgs e)
    {
        if (waterImgFileUpload.HasFile)    //本地上传
        {
            string uploadFloderImg = "/images/";
            if (!Directory.Exists(Server.MapPath(uploadFloderImg))) //判断上传目录是否存在
            {
                Directory.CreateDirectory(Server.MapPath(uploadFloderImg));
            }

            string fileName = waterImgFileUpload.PostedFile.FileName.ToLower();
            if (fileName == string.Empty || fileName == null)
            {
                js.div(false, "", "text", "请选择文件后再上传！", 300, 0, "", "");
            }

            string fileExe = fileName.Substring(fileName.LastIndexOf(".") + 1); //取后缀名
            long UploadSize = xabara.isNum(xml.read("xabara.config", "UploadImgSize", 180));  //上传大小
            if (!fileExe.Equals("png")) //判断该扩展名是否合法
            {
                js.div(false, "", "text", "请选择一张png格式的透明背景水印图！", 300, 0, "", "");
            }

            long newFileSize = waterImgFileUpload.PostedFile.ContentLength / 1048576;
            if (newFileSize > UploadSize) // 判断上传文件大小是否超过最大值
            {
                js.div(false, "", "text", "目前支持大小：" + UploadSize.ToString() + "MB<br />当前大小为：" + newFileSize.ToString() + "MB", 350, 0, "", "");
            }

            string UploadTemp = HttpContext.Current.Server.MapPath(@uploadFloderImg + "WaterMark.png");
            waterImgFileUpload.PostedFile.SaveAs(UploadTemp); //保存水印图片
            js.div(false, "", "text", "更换水印文件成功！", 300, 0, "", "");
        }
        else
        {
            js.div(false, "", "text", "请选择文件后再上传！", 300, 0, "", "");
        }
    }

    protected void sysSet_Click(object sender, EventArgs e)
    {
        string pathFlie = HttpContext.Current.Server.MapPath("/xabara.config");
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(pathFlie);

        XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes; //得到顶层节点列表
        foreach (XmlElement xElement in topM)
        {
            if (xElement.Name == "appSettings") //判断读取子层
            {
                XmlNodeList nodelist = xElement.ChildNodes; //得到该节点的子节点               
                if (nodelist.Count > 0)
                {
                    foreach (XmlNode xnode in nodelist)
                    {
                        XmlElement xe = (XmlElement)xnode;

                        string xeStr = xe.Attributes["value"].Value;
                        switch (xe.Attributes["key"].Value) //修改值
                        {
                            case "webName":
                                xeStr = webName.Text.Trim();
                                break;
                            case "webDomains":
                                xeStr = webDomains.Text.Trim();
                                break;
                            case "cookiesDomain":
                                string newDomain = string.Empty;
                                string[] domain = webDomains.Text.Trim().Split(new char[] { '.' });
                                int len = domain.Length;
                                for (int i = len - 1; i > 0; i--)
                                {
                                    newDomain += "." + domain[len - i];
                                }
                                xeStr = newDomain;
                                break;
                            case "miibeian":
                                xeStr = webBak.Text.Trim();
                                break;
                            case "qq":
                                xeStr = webQQ.Text.Trim();
                                break;
                            case "taobao":
                                xeStr = wangwang.Text.Trim();
                                break;
                            case "webEmail":
                                xeStr = webEmail.Text.Trim();
                                break;
                            case "webTel":
                                xeStr = webtel.Text.Trim();
                                break;
                            case "webFax":
                                xeStr = webFax.Text.Trim();
                                break;
                            case "mobile":
                                xeStr = webMobile.Text.Trim();
                                break;
                            case "freePhone":
                                xeStr = freePhone.Text.Trim();
                                break;
                            case "add":
                                xeStr = add.Text.Trim();
                                break;
                            case "ip":
                                xeStr = ips.Text.Trim();
                                break;
                            case "waterMark":
                                xeStr = waterWords.Text.Trim();
                                break;
                            case "KeyWord":
                                xeStr = keyWords.Text.Trim();
                                break;
                            case "metaStr":
                                xeStr = mKeyWords.Text.Trim();
                                break;
                            case "titleMeta":
                                xeStr = titleMeta.Text.Trim();
                                break;
                            case "countScript":
                                xeStr = countStr.Text.Trim();
                                break;
                        }
                        xe.Attributes["value"].Value = xeStr;
                    }
                }
            }
        }

        xmlDoc.Save(pathFlie);
        xmlDoc = null;
        Response.Redirect("systemSet.aspx", true);
    }

    protected void logoImgButton_Click(object sender, EventArgs e)
    {
        if (logoImgFile.HasFile)    //本地上传
        {
            string uploadFloderImg = "/images/";
            if (!Directory.Exists(Server.MapPath(uploadFloderImg))) //判断上传目录是否存在
            {
                Directory.CreateDirectory(Server.MapPath(uploadFloderImg));
            }

            string fileName = logoImgFile.PostedFile.FileName.ToLower();
            if (fileName == string.Empty || fileName == null)
            {
                js.div(false, "", "text", "请选择文件后再上传！", 300, 0, "", "");
            }

            string fileExe = fileName.Substring(fileName.LastIndexOf(".") + 1); //取后缀名
            long UploadSize = xabara.isNum(xml.read("xabara.config", "UploadImgSize", 180));  //上传大小
            if (!fileExe.Equals("jpg")) //判断该扩展名是否合法
            {
                js.div(false, "", "text", "请选择一张jpg格式的logo图片！", 300, 0, "", "");
            }

            long newFileSize = logoImgFile.PostedFile.ContentLength / 1048576;
            if (newFileSize > UploadSize) // 判断上传文件大小是否超过最大值
            {
                js.div(false, "", "text", "目前支持大小：" + UploadSize.ToString() + "MB<br />当前大小为：" + newFileSize.ToString() + "MB", 350, 0, "", "");
            }

            string UploadTemp = HttpContext.Current.Server.MapPath(@uploadFloderImg + "logo.jpg");
            logoImgFile.PostedFile.SaveAs(UploadTemp); //保存logo图片
            js.div(false, "", "text", "更换网站LOGO文件成功！", 300, 0, "", "");
        }
        else
        {
            js.div(false, "", "text", "请选择文件后再上传！", 300, 0, "", "");
        }
    }
}
