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_AddRssList : 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()
    { 
        string format=Globals.Text["RssRecordCount"];
        for (int i = 1; i < 11; i++)
        {
            dropShowCount.Items.Add(new ListItem(string.Format(format,i), i.ToString()));
        }
       
        if (listID > 0)
        {
            List<UserList> list = UserLists.GetUserListByAccount(Account);
            foreach (UserList l in list)
            {
                if (l.ID == listID)
                {
                    txtTitle.Text = l.Title;
                    txtRssUrlAddress.Text = l.SourceUrl;
                    this.buttonUpdate.Text = Globals.Text["btn_Edit"];
                    if (l.Parameter == string.Empty)
                        dropShowCount.SelectedValue = l.Parameter;
                    else
                    dropShowCount.SelectedValue = l.Parameter;
                    break;
                }
            }

        }else
        dropShowCount.SelectedValue = "5";
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text;
        string rss = txtRssUrlAddress.Text;
        int count = int.Parse(dropShowCount.SelectedValue);
        UserList ul = new UserList();
        ul.Title = title;
        ul.SourceUrl = rss;
        ul.ListType = UserListTyle.Rss;
        ul.Parameter = dropShowCount.SelectedValue;
        ul.Account = Account;
        ul.Description=string.Empty;
        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)//新增Rss块
            {
                UserConfig uc = Users.GetUserConfig(Account);
                if (uc.Layout == null)
                {
                    ThemeHelp.SetDefaultTheme(uc);
                }
                uc.Layout.Rows[0].Add(new ThemeCell(string.Format("rsspart:{0}", partID), ""));
                Users.UpdateUserConfig(uc);
            }
           UserLists.ClearUserList(Account);
        }
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script type='text/javascript' language='javascript'>
var d = new dialog();d.init();
d.set('src','2');
 
d.event('操作成功！', ' ','',' ');</script>");
        Message.Text = sb.ToString();

    }

    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
}
