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 article : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["my"] != null&&Request.QueryString["my"].ToString()!="")
            {
                showContent.Style.Add("display", "none");//不显示内容
                comment.Style.Add("display", "none");   //不显示内容
                replay.Style.Add("display", "none");    //不显示内容
                h2.InnerHtml = "<b>" + Session["username"].ToString() + "</b>发表的帖子";
                string sql = "select article.id,replaynum,readnum,title,username,classname,time from article,[user] where authorid='" + 
                    Request.QueryString["my"].ToString() + "' and userid=authorid order by article.id desc";
                DataSet ds = DbManager.getDataSet(sql, "myArticle");
                this.articlelist.DataSource = ds.Tables["myArticle"];
                this.articlelist.DataBind();
                DbManager.closeConnection();
            }
            if (Request.QueryString["class"] != null && Request.QueryString["class"].ToString() != "")
            {
                showContent.Style.Add("display", "none");//不显示内容
                comment.Style.Add("display", "none");   //不显示内容
                replay.Style.Add("display", "none");    //不显示内容
                h2.InnerHtml = "类别：" +Server.UrlDecode(Request.QueryString["class"].ToString());
                string sql = "select article.id,replaynum,readnum,title,username,classname,time from article,[user] " +
                    "where classname='" + Server.UrlDecode(Request.QueryString["class"].ToString()) + "' and userid=authorid order by article.id desc";
                DataSet ds = DbManager.getDataSet(sql, "classArticle");
                this.articlelist.DataSource = ds.Tables["classArticle"];
                this.articlelist.DataBind();
                DbManager.closeConnection();
            }
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                int id = Convert.ToInt32(Request.QueryString["id"].ToString());
                idValue.Value = id.ToString();
                table.Style.Add("display", "none"); //不显示GridView控件
                string sql = "select article.id,classname,title,authorid,time,context,userid,username,email from article,[user]"+
                    " where userid=authorid and article.id=" + id;
                SqlDataReader reader = DbManager.getReader(sql);
                if (reader.HasRows)
                {
                    reader.Read();
                    string strContent = "<a href='article.aspx?id=" + id + "'><h2>" + reader["title"].ToString() + "</h2></a>";
                    strContent += "<div class='content'>" + reader["context"].ToString().Replace("\n", "<br>") + "</div>";
                    string author = reader["authorid"].ToString();
                    string classname = reader["classname"].ToString();
                    string userid = reader["userid"].ToString();
                    System.IO.StringWriter urlClass = new System.IO.StringWriter();
                    Server.UrlEncode(classname, urlClass);
                    strContent += "<div class='infor'>" + reader["time"].ToString() + "由 <b>" +
                        reader["username"].ToString() + "</b> 撰写 类别：<a href='article.aspx?class=" +
                        urlClass.ToString() + "'>" + classname + "</a></div>";
                    strContent += "<div class='tool'><a href='#'>评论</a>&nbsp;";
                    if (Session["userid"] != null && Session["userid"].ToString() == userid)
                    {
                        strContent += "<a href='articleEdit.aspx?id=" + id + "&action=edit'>编辑</a>&nbsp;";
                        strContent += "<a href='articleEdit.aspx?id=" + id + "&action=delete'>删除</a>&nbsp;";
                    }
                    strContent += "<a href='mailto:" + reader["email"].ToString() + "'>Email</a></div><hr/>";
                    showContent.InnerHtml = strContent;
                    if (!reader.IsClosed)
                        reader.Close();
                    //更新阅读次数=======================
                    string upsql = "update article set readnum=readnum+1 where id=" + id;
                    DbManager.getNonQuery(upsql);
                    //回复===============================
                    string replaySql = "select * from replay where articleid="+id;
                    reader = DbManager.getReader(replaySql);
                    if (reader.HasRows)
                    {
                        string strGuest = "";
                        int i = 1;
                        while (reader.Read())
                        {
                            strGuest += "<fieldset><legend><img src='images/tx2.jpg' width='30' height='30'/><b>" +
                                    reader["guest"].ToString() + "</b> 于 " +
                                    reader["time"].ToString() + " 跟帖 <font color=red>" + i + "楼</font></legend>" +
                                    reader["message"].ToString().Replace("\n", "<br>") + "</fieldset>";
                            i++;
                        }
                        comment.InnerHtml = strGuest;
                        reader.Close();
                    }
                    else
                    {
                        comment.InnerHtml = "<b>暂时没有回帖</b>";
                    }
                }
                else
                {
                    showContent.InnerHtml = "<div class='content'>文章不存在</div>";
                }
                if (!reader.IsClosed)
                    reader.Close();
                DbManager.closeConnection();

                //如果用户登录将其用户名写入留言文本框
                if (Session["username"] == null || string.IsNullOrEmpty(Session["username"].ToString()))
                {
                    // 未登录时隐藏整个评论区域
                    replay.Style["display"] = "none";
                    // 同时隐藏或禁用提交按钮（可选）
                    addMessage.Visible = false; // 假设按钮ID为 addMessage
                }
                else
                {
                    // 已登录时显示评论区域
                    replay.Style["display"] = "block";
                    addMessage.Visible = true;
                    authorName.Text = Session["username"].ToString();
                }
            }
        }
    }
    protected void addMessage_Click(object sender, EventArgs e)
    {
        // 没有登陆点击回复弹出登录框
        if (Session["username"] == null)
        {
            Response.Redirect("Login.aspx");
            return;
        }

        // 原有代码
        int articleid = Convert.ToInt32(idValue.Value);
        string replaySql = "insert into replay(articleid,guest,message,time) values(" + articleid + ",'" +
                          Session["username"].ToString() + "','" + content.Text + "','" + DateTime.Now.ToString() + "')";

        int flag = DbManager.getNonQuery(replaySql);
        if (flag > 0)
        {
            string upsql = "update article set replaynum=replaynum+1 where id=" + articleid;
            if (DbManager.getNonQuery(upsql) > 0)
                Response.Redirect("replay.aspx?id=" + articleid);
        }
        else
        {
            Response.Redirect("replay.aspx?action=error");
        }
    }
    protected string GetAvatarPath(string username)
    {
        // 统一返回默认头像路径
        return "images/tx2.jpg";
    }
}
