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 articleEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //处理安全性
            if (Session["login"] == null || Session["login"].ToString() != "true")
            {
                message.InnerHtml = function.redirect("Default.aspx", "您没有正常登录！");
            }
            else
            {
                if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() == "add")
                {
                    addEdit_OK.Text = "发表新帖";
                    idValue.Value = "-1";
                    //绑定类别
                    string classSql = "select * from class";
                    classname.DataSource = DbManager.getReader(classSql);
                    classname.DataTextField = "classname";
                    classname.DataValueField = "id";
                    classname.DataBind();
                    DbManager.closeConnection();
                }
                if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() == "edit")
                {
                    addEdit_OK.Text = "更新帖子";
                    idValue.Value = Request.QueryString["id"].ToString();
                    //绑定类别及原信息,更新的文章不能够改变其所属类别
                    string sql = "select * from article where id=" + Convert.ToInt32(Request.QueryString["id"].ToString());
                    SqlDataReader reader = DbManager.getReader(sql);
                    if (reader.HasRows)
                    {
                        reader.Read();
                        ListItem item = new ListItem (reader["classname"].ToString(),reader["classid"].ToString());
                        this.classname.Items.Add(item);
                        title.Text = reader["title"].ToString();
                        content.Text = reader["context"].ToString();
                        reader.Close();
                    }
                    else
                    {
                        addEdit.Style.Add("display", "none");
                        message.InnerHtml = function.redirect("Default.aspx", "更新出错");
                    }
                    DbManager.closeConnection();
                }
                if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() == "delete")
                {
                    //屏蔽table控件
                    addEdit.Style.Add("display", "none");
                    string delSql = "delete from article where id=" + Convert.ToInt32(Request.QueryString["id"].ToString());
                    if (DbManager.getNonQuery(delSql) > 0)
                    {
                        //更改作者发帖的总数
                        string deleteNum = "update [user] set blognum=blognum-1 where userid='" + Session["userid"].ToString() + "'";
                        DbManager.getNonQuery(deleteNum);
                        message.InnerHtml = function.redirect("Default.aspx", "删除成功！");
                    }
                    else
                        message.InnerHtml = function.redirect("Default.aspx", "删除失败！");
                }
            }
        }
    }
    protected void addEdit_OK_Click(object sender, EventArgs e)
    {
        //屏蔽table控件
        addEdit.Style.Add("display", "none");
        if (idValue.Value == "-1")
        {
            //处理添加新帖任务
            int classId = Convert.ToInt32(classname.SelectedItem.Value); //类别id
            string classType = classname.SelectedItem.Text;
            string authorid=Session["userid"].ToString();
            string sql = "insert into article(classid,classname,title,authorid,time,context,readnum,replaynum)" +
                " values(" + classId + ",'" + classType + "','" + title.Text + "','" + authorid + "','" + DateTime.Now.ToString() + "','" +
                content.Text + "',0,0)";
            if (DbManager.getNonQuery(sql) > 0)
            {
                //添加成功,使得authorid的blognum字段值加1
                string addSql = "update [user] set blognum=blognum+1 where userid='" + authorid + "'";
                if (DbManager.getNonQuery(addSql) > 0)
                {
                    System.IO.StringWriter urlClass = new System.IO.StringWriter();
                    Server.UrlEncode(classType, urlClass);
                    message.InnerHtml = function.redirect("article.aspx?class=" + urlClass.ToString(), "发帖成功！");
                }
            }
            else
            {
                message.InnerHtml = function.redirect("Default.aspx", "发帖失败");
            }
        }
        else
        {
            //处理更新
            int id = Convert.ToInt32(idValue.Value);
            string upSql = "update article set title='" + title.Text + "',context='" + content.Text + "' where id=" + id;
            if (DbManager.getNonQuery(upSql) > 0)
            {
                System.IO.StringWriter urlClass = new System.IO.StringWriter();
                Server.UrlEncode(classname.SelectedItem.Text, urlClass);
                message.InnerHtml = function.redirect("article.aspx?class=" + urlClass.ToString(), "更新成功！");
            }
            else
                message.InnerHtml = function.redirect("Default.aspx", "更新失败 ");
        }
        DbManager.closeConnection();
    }
}
