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

public partial class Admin_PostList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rprSelectionList.ItemCommand += new RepeaterCommandEventHandler(rprSelectionList_ItemCommand);
        if (!this.IsPostBack)
        {
            BindList();
        }
    }
    void rprSelectionList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName.ToLower())
        {
            case "delete":
                int id = Convert.ToInt32(e.CommandArgument);
                Blogs.DelPost(id);

                break;
            default:
                break;
        }
        BindList();
    }
    void BindList()
    {
        UserConfig uc = Tools.GetUserConfigBySession();
        PagedPost p = new PagedPost(uc.BlogID, SpacesContext.Current.CategoryID, PostPaged.CurrentPageIndex, PostPaged.PageSize, PostType.BlogPost);
        p.Hiding = false;
        int count;
        rprSelectionList.DataSource = Blogs.GetPagedPost(p, out count);
        PostPaged.RecordCount = count;
        rprSelectionList.DataBind();

        string selectValue = dropPostCategory.SelectedValue;
        dropPostCategory.Items.Clear();
        List<BlogCategory> bs= BlogCategories.GetBlogCategoryByAccount(uc.Account);
       // List<ListItem> lis = new List<ListItem>();
        ListItem defaultLI = new ListItem();
        defaultLI.Text = "所有文章";
        defaultLI.Value = "-1";
        if(selectValue==defaultLI.Value)
        {
         defaultLI.Selected=true;
        }
         dropPostCategory.Items.Add(defaultLI);
        foreach (BlogCategory bc in bs)
        {
            ListItem li = new ListItem();
            li.Text = bc.Title;
            li.Value = bc.CategoryID.ToString();
            if(li.Value==selectValue)
            {
                li.Selected = true;
            }
            dropPostCategory.Items.Add(li);
        }
       

    }

  public string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
    protected void PostPaged_PageChanged1(object src, Xiaoxiao.WebUI.PageChangedEventArgs e)
    {
        this.PostPaged.CurrentPageIndex = e.NewPageIndex;
        BindList();
    }

    protected void dropPostCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        int categoryID = int.Parse(dropPostCategory.SelectedValue);
      //  Response.Write(categoryID+"");
       // return;
        if (categoryID == -1)
        {
            BindList();
            return;
        }
        UserConfig uc = Tools.GetUserConfigBySession();
        PagedPost p = new PagedPost(uc.BlogID, categoryID, PostPaged.CurrentPageIndex, PostPaged.PageSize, PostType.BlogPost);
        p.Hiding = false;
        int count;
        rprSelectionList.DataSource = Blogs.GetPagedPost(p, out count);
        PostPaged.RecordCount = count;
        rprSelectionList.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string title = txtPostTitle.Text;


        PagedBlogSearch pt = new PagedBlogSearch(title.TrimStart(' ').TrimEnd(' '), 1,100);
        pt.SearchType = SearchType.Title;
        pt.BlogID = Tools.GetUserConfigBySession().BlogID;
   
       int count;
       DataView dv = Blogs.Blog_SearchTopic(pt, out count);
 
 

       rprSelectionList.DataSource = dv;
      // PostPaged.RecordCount = list.Count;
       rprSelectionList.DataBind();
    }
}
