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_UserList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rptUserList.ItemCreated += new RepeaterItemEventHandler(rptUserList_ItemCreated);
        rptUserList.ItemCommand += new RepeaterCommandEventHandler(rptUserList_ItemCommand);
       // if (!IsPostBack)
            BindList();
    }

    void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
           
            int listID = Convert.ToInt32(e.CommandArgument);
            UserLists.DelUserList(listID);
            List<UserList> list=UserLists.GetUserListByAccount(Account);
            foreach (UserList u in list)
            {
                if (u.ID == listID)
                {
                    list.Remove(u);
                    break;
                }
            
            }
        
        }
        BindList();
    }

    void rptUserList_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
        {
            UserList ul = e.Item.DataItem as UserList;
            if (ul != null)
            {
                HyperLink listTitle = e.Item.FindControl("ListTitle") as HyperLink;
                Literal ListType = e.Item.FindControl("ListType") as Literal;
                HyperLink LnkEdit = e.Item.FindControl("LnkEdit") as HyperLink;
                LinkButton LbtnDel = e.Item.FindControl("LbtnDel") as LinkButton;
                listTitle.Text = ul.Title;
                if(ul.ListType==UserListTyle.Rss)
                {
                    listTitle.NavigateUrl = string.Format("EditRssList.aspx?id={0}",ul.ID);
                   
                    ListType.Text = "Rss源";
                }
                if (ul.ListType == UserListTyle.Html)
                {
                    listTitle.NavigateUrl = string.Format("EditHtmlList.aspx?id={0}", ul.ID);
                    ListType.Text = "HTML块";

                }
                LnkEdit.NavigateUrl = listTitle.NavigateUrl;
                LbtnDel.CommandName = "delete";
                LbtnDel.CommandArgument = ul.ID.ToString();
              //  Response.Write(ul.ID.ToString());
            }
        }
    }

    private void BindList()
    {
        List<UserList> list = UserLists.GetUserListByAccount(Account);
        rptUserList.DataSource = list;
        rptUserList.DataBind();
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
}
