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 Spaces;
using LanguagePack;
using System.Xml;
using System.IO;
using Spaces.Controls;

public partial class Manage_SystemConfig : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        Message.Text = "";
        if (!IsPostBack)
        {
            BindConfig();
        }
    }

    private void BindConfig()
    {
        Config config = Config.GetConfig();
        string defaultAlbum = config.DefaultAlbum;
        string defaultMusic = config.DefaultMusic;
        if (config.DefaultAlbum.StartsWith("/"))
        {
            defaultAlbum = config.DefaultAlbum.Remove(0,1);
        }
        if (config.DefaultMusic.StartsWith("/"))
        {
            defaultMusic = config.DefaultMusic.Remove(0, 1);
        }
        txtBlogAlbumPath.Text = config.BlogAlbumPath;
        txtBlogAlbumPath.Enabled = false;
        txtBlogAlbumUrl.Text = config.BlogAlbumUrl;
        txtDefaultAlbum.Text = defaultAlbum;
        txtDefaultAlbum.Enabled = false;
        txtDefaultDomain.Text = config.DefaultDomain;
        txtDefaultMusic.Text = defaultMusic;
        txtDefaultMusic.Enabled = false;
        txtDiskPath.Text = config.UploadFilesPath;
        txtDiskPath.Enabled = false;
        txtDomainFix.Text = config.DomainFix;
        txtPhotosPath.Text = config.PhotosPath;
        txtPhotosPath.Enabled = false;
        txtPhotosUrl.Text = config.PhotosUrl;
        string defaultLanguage=config.DefaultLanguage;
        string db = config.DefaultProvider;
        dropLanguages.Items.Clear();
        foreach (string s in Resources.SupportLanguages.Keys)
        {
            ListItem li = new ListItem(Resources.SupportLanguages[s], s);
            if (li.Value == defaultLanguage)
            {
                li.Selected = true;
            }
            dropLanguages.Items.Add(li);
        }
        
        foreach (ListItem l in dropDatabase.Items)
        {

            l.Selected = false;
            if (l.Value == db)
            {
                l.Selected = true;

            }
        }
        dropDatabase.Enabled = false;
        
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        string rootPath = Server.MapPath("~/");
        if (!rootPath.EndsWith("\\"))
            rootPath += "\\";
        string diskPath = txtDiskPath.Text;
        string domain = txtDefaultDomain.Text;
        string music = txtDefaultMusic.Text;
        string album = txtDefaultAlbum.Text;
        string albumPath = txtBlogAlbumPath.Text;
        string albumUrl = txtBlogAlbumUrl.Text;
        string language = dropLanguages.SelectedValue;
        string domainFix = txtDomainFix.Text;
        try
        {

            XmlDocument doc = new XmlDocument();

            FileInfo xmlFile = new FileInfo(string.Format("{0}Web.Config", rootPath));
            if (!xmlFile.Exists)
            {
                throw new ApplicationException("没有找到配置文件");
            }
            doc.Load(xmlFile.FullName);
            XmlNode node = doc["configuration"]["disk"]["disk"];
            string userPath = diskPath;
            if (!Directory.Exists(userPath))
                Directory.CreateDirectory(userPath);
            node.Attributes["uploadFilesPath"].Value = userPath;
            node.Attributes["defaultDomain"].Value = domain;

            string blogimageFolder = albumPath;
            if (!Directory.Exists(blogimageFolder))
                Directory.CreateDirectory(blogimageFolder);

            node.Attributes["blogAlbumPath"].Value = blogimageFolder;

            node.Attributes["blogAlbumUrl"].Value = albumUrl;

            node.Attributes["defaultLanguage"].Value = language;

            node.Attributes["defaultMusic"].Value = music;
            node.Attributes["defaultAlbum"].Value = album;
            node.Attributes["domainFix"].Value = domainFix;
            doc.Save(xmlFile.FullName);
            Message.Text = Msg.GetMsg("设置完成！");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
