using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class Manager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //非法url链接
            if (Request.QueryString["class"] == null)
            {
                table.Style.Add("display", "none");
                message.InnerHtml = function.redirect("Default.aspx", "请确认要管理的社区！");
            }
            else
            {
                string classname = Server.UrlDecode(Request.QueryString["class"].ToString());
                classValue.Value = classname;
                if (Session["userid"] != null && Session["userid"].ToString() != "")
                {
                    //确认该用户是否为版主
                    int i = Convert.ToInt32(Session["banzhu"].ToString());
                    bool flag = false;
                    for (int j = 0; j < i; j++)
                    {
                        string banzhu = "banzhu" + j;
                        if (Session[banzhu]!=null&&Session[banzhu].ToString() == classname)
                        {
                            flag = true;
                            break;
                        }
                    }
                    //不是版主，退出到主页
                    if (!flag)
                    {
                        table.Style.Add("display", "none");
                        message.InnerHtml = function.redirect("Default.aspx", "您不是该版的版主！");
                    }
                    //是版主，载入该类信息
                    h2.InnerHtml = "管理 <b>" + classname + "</b> 信息";
                    string sql = "select article.id,replaynum,readnum,title,username,classname,time from article,[user] " +
                    "where classname='" + classname + "' and userid=authorid order by article.id desc";
                    DataSet ds = DbManager.getDataSet(sql, "manArticle");
                    this.articlelist.DataSource = ds.Tables["manArticle"];
                    this.articlelist.DataKeyNames = new string[] { "id" };  //绑定主键
                    this.articlelist.DataBind();
                    DbManager.closeConnection();
                }
                else
                {
                    table.Style.Add("display", "none");
                    message.InnerHtml = function.redirect("Default.aspx", "身份有误！");
                }
            }
        }
    }
    protected void articlelist_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = this.articlelist.DataKeys[e.RowIndex].Value.ToString();   //获取article主键id
        //首先查询出作者id即authorid，并使得该作者的帖子数目减一
        string selectSql = "select authorid from article where id=" + id;
        SqlDataReader reader = DbManager.getReader(selectSql);
        if (reader.HasRows)
        {
            reader.Read();
            string userid = reader["authorid"].ToString();
            reader.Close();
            //删除一个帖子
            string sql = "delete from article where id=" + id;
            if (DbManager.getNonQuery(sql) > 0)
            {
                string subBlog = "update [user] set blognum=blognum-1 where userid='" + userid + "'";
                DbManager.getNonQuery(subBlog);
                table.Style.Add("display", "none");
                message.InnerHtml = function.redirect("Manager.aspx?class=" + Server.UrlEncode(classValue.Value).ToString(), "删除成功！");
            }
            else
            {
                table.Style.Add("display", "none");
                message.InnerHtml = function.redirect("Default.aspx", "删除过程出错！");
            }
        }
        else
        {
            //屏蔽table
            table.Style.Add("display", "none");
            message.InnerHtml = function.redirect("Default.aspx", "删除过程出错！");
        }
    }
}
