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_BlogCategory : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //Cacher.ClearCacheByFix("45");
        categoryView.RowDeleting += new GridViewDeleteEventHandler(categoryView_RowDeleting);
        categoryView.RowEditing += new GridViewEditEventHandler(categoryView_RowEditing);
        categoryView.RowCancelingEdit += new GridViewCancelEditEventHandler(categoryView_RowCancelingEdit);
        categoryView.RowUpdating += new GridViewUpdateEventHandler(categoryView_RowUpdating);
        if (!this.IsPostBack)
            BindView();
    }

    void categoryView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        HiddenField hf = (HiddenField)categoryView.Rows[e.RowIndex].FindControl("CategoryID");
        int categoryID = Convert.ToInt32(hf.Value);
        BlogCategories.DelBlogCategory(categoryID);
        BindView();
    }

    void categoryView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        HiddenField hf = (HiddenField)categoryView.Rows[e.RowIndex].FindControl("CategoryID");
        TextBox titleTB = (TextBox)categoryView.Rows[e.RowIndex].FindControl("Title");
        string title = titleTB.Text;
        int categoryID = Convert.ToInt32(hf.Value);
        BlogCategory bc = BlogCategories.GetBlogCategory(categoryID);
        bc.Title = title;
        BlogCategories.UpdateBlogCategory(bc);
        categoryView.EditIndex = -1;
        BindView();
    }

    void categoryView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        categoryView.EditIndex = -1;
        BindView();
    }

    void categoryView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        categoryView.EditIndex = e.NewEditIndex;
        BindView();
    }
    void BindView()
    {

        string account = Page.User.Identity.Name;
        UserConfig uc = Cacher.GetUserConfig(account);
        List<BlogCategory> list = BlogCategories.GetBlogCategoryByBlogID(uc.BlogID);

        categoryView.DataSource = list;
        categoryView.DataBind();
        // categoryView.

    }
    protected void AddBtn_Click(object sender, EventArgs e)
    {
        string account = Page.User.Identity.Name;
        string categoryName = CategoryName.Text;

        UserConfig uc = Cacher.GetUserConfig(account);
        BlogCategory bc = new BlogCategory();
        bc.BlogID = uc.BlogID;
        bc.ParentID = 0;
        bc.Title = categoryName;
        bc.IsActive = true;
        bc.Description = string.Empty;
        bc.CategoryType = BlogCategoryType.PostCollection;
        BlogCategories.CreateBlogCategory(bc);
        BindView();
    }
}
