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 System.Collections.Generic;

public partial class Admin_AddHtmlList : System.Web.UI.Page
{
    int listID=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
            listID = int.Parse(Request.QueryString["ID"]);
        if (!this.IsPostBack)
            BindList();
    }

    private void BindList()
    {
        if (listID > 0)
        {
            List<UserList> list = UserLists.GetUserListByAccount(Account);
            foreach (UserList l in list)
            {
                if (l.ID == listID)
                {
                    txtTitle.Text = l.Title;
                    txtHtmlCode.Text = l.Description;
                    this.buttonUpdate.Text = "更 新";
                    break;
                }
            }
        
        }
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text;

        UserList ul = new UserList();
        ul.Title = title;
        ul.SourceUrl = string.Empty;
        ul.ListType = UserListTyle.Html;
        ul.Parameter = string.Empty;
        ul.Account = Account;
        ul.Description = txtHtmlCode.Text;
       // Response.Write(ul.Description);
        if (listID > 0)
        {
            ul.ID = listID;
            UserLists.UpdateUserList(ul);
            List<UserList> list = UserLists.GetUserListByAccount(Account);
            for (int i=0; i < list.Count; i++)
            {
                if (list[i].ID == listID)
                {
                    list[i] = ul;
                }
            }
        
        }
        else
        {
            int partID=UserLists.CreateUserList(ul);
            if (listID <1)//新增HTML块
            {
                UserConfig uc = Users.GetUserConfig(Account);
                if (uc.Layout == null)
                {
                    ThemeHelp.SetDefaultTheme(uc);
                }
                uc.Layout.Rows[0].Add(new ThemeCell(string.Format("htmlpart:{0}", partID), ""));
                Users.UpdateUserConfig(uc);
            }
            UserLists.ClearUserList(Account);
        }
 
        Message.Text = Msg.GetMsg("操作已成功!");  
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
}
