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 System.Text.RegularExpressions;

public partial class article_comments : Ycbw.Data.OleDbClient
{
	public string title;
	
    private void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int id = Int32.Parse(Request.QueryString["id"]);

            this.Fill("Select top 1 * from [v_articles] a where id = " + id, "data_article");

            System.Data.DataRow dr = this.Data.Tables[0].Rows[0];

            this.title = dr["title"].ToString();
            this.content.Text  = Cutstring(Regex.Replace(dr["content"].ToString(), "<[^>]*>", "", RegexOptions.IgnoreCase), 300);
            this.source.Text   = dr["source"].ToString();		
            this.hits.Text   = dr["hits"].ToString();		
            this.addtime.Text  = dr["addtime"].ToString();

			Ycbw.Articles A = new Ycbw.Articles(this,dr["cid"].ToString());
		
            //SEO
            this.Head.Title = dr["title"].ToString();
			
			if( dr["tags"].ToString() != "" )
				this.Head.Keywords = dr["tags"].ToString();
			else
				this.Head.Keywords  = "宜昌" + A.ClassName + "," + this.Configuration["keywords"].ToString();
				
			this.Head.Describe = Ycbw.UI.Page.Cutstring(Regex.Replace(dr["content"].ToString().Replace("\r\n",""), "<[^>]*>", ""), 200);
			
			this.category.Text  = A.ClassName;
            this.path.Text = "您的位置：<a href=/>首页<a/> > " + A.Path + "正文";
			
			//相关推荐
            this.BindList("Select top 10 * from [v_articles] where cid = " + dr["cid"].ToString() + " and id <> " + id + " and state > 0 order by id", "recommend", data_articles);
			
			if( Tables["recommend"].Rows.Count == 0 )
				this.p_recommend.Visible = false;

			//文章评论
			this.BindList("select * from [b_article_comments] where aid = "+ id +" order by addtime desc", "data_comments", data_comments, 10, text_pages);

			//更新点击
            this.Execute("Update [b_articles] set hits = hits + 1 where id = " + id);
        }
    }
	
	public void button_submit_click(object sender, EventArgs e)
	{
        string error = "";

        this.Fill("select top 1 * from [b_article_comments] where ip = '"+ Request.ServerVariables["REMOTE_ADDR"]+"' order by addtime desc", "data_comments");

        if( this.Data.Tables["data_comments"].Rows.Count > 0 )
        {
            System.Data.DataRow dr = this.Data.Tables["data_comments"].Rows[0];

            DateTime lastpost = DateTime.Parse(dr["addtime"].ToString());

            TimeSpan ts = DateTime.Now - lastpost;

            if (ts.TotalSeconds < 60)
                error += "你发布评论的速度太快了，请稍候再发";  //ts.TotalSeconds.ToString();// 
        }

		if( this.c_content.Text == "文明上网，理性发言" || this.c_content.Text == "" )
			error += "·请输入留言内容！\\n";

		if( Session["vcode"] == null || Session["vcode"].ToString() != this.vcode.Text )
			error += "·验证码输入错误！\\n";

        if (error != "")
        {
            this.JWrite("alert('" + error + "');history.go(-1);");
        }
        else
        {
            string c_author = "网友";
            string c_content = this.c_content.Text;

            this.Execute("insert into [b_article_comments] (aid,author,content,ip,state,addtime) values (" + Request.QueryString["id"] + ",'" + c_author + "','" + c_content + "','" + Request.ServerVariables["REMOTE_ADDR"] + "',1, '"+ DateTime.Now +"')");

            this.JWrite("alert('评论发表成功，审核后方可显示，感谢您的参与！');history.go(-1);");
        }

		this.Dispose(true);
	}
}
