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;

public partial class comment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //ajax提交新评论
        if (Request.Form["type"] != null)
        {
            string type=Request.Form["type"].ToString();
            Response.ContentType = "text/html";
            if ( type== "0")
            {
                if (Request.Form["username"] != null && Request.Form["content"] != null)
                {
                    Response.Write(CommentAdd(Request.Form["username"].ToString(), Request.Form["content"].ToString()));
                }
                else
                {
                    Response.Write("参数传递失败!");
                }
            }
            else
            {
                Response.Write(GetCommentCount(int.Parse(Request.Form["docid"].ToString())));
            }
          
            Response.Flush();
            Response.Close();
        }
    
        if (Request.Form["comid"] != null&Request.Form["sa"]!=null)
        {
            Response.ContentType = "text/html";
            int comid = int.Parse(Request.Form["comid"].ToString());
            string sa=Request.Form["sa"].ToString();
            Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
            if (sa == "s")
            {
                ps.Queryps("update Fm_comment set support=support+1 where comid=" + comid + "");
                Response.Write(ps.Getps("Fm_comment", "support", "comid=" + comid + "")[0]["support"].ToString());
            }
            else
            {
                ps.Queryps("update Fm_comment set against=against+1 where comid=" + comid + "");
                Response.Write(ps.Getps("Fm_comment", "against", "comid=" + comid + "")[0]["against"].ToString());
            }

            Response.Flush();
            Response.Close();

        }
        
        if (!IsPostBack)
        {
            if (Request.QueryString["docid"] != null)
            {
                Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
                int docid=int.Parse(Request.QueryString["docid"].ToString());
                DataView dv = ps.Getps("Fm_model_article_view", "title,path,html", "docid=" + docid + "");
                if (ReturnComment(docid))
                {
                    Hiddendocid.Value = docid.ToString();

                    cont_title.Text = dv[0]["title"].ToString();
                    cont_title.Target = "_blank";
                    if (dv[0]["html"].ToString().ToLower() == "true")
                    {
                        cont_title.NavigateUrl = dv[0]["path"].ToString();

                    }
                    else
                    {
                        cont_title.NavigateUrl = "/article.aspx?docid=" + docid;
                    }
                    Title = "评论--" + dv[0]["title"].ToString();
                    
                }
                else
                {
                    Response.Write("<script>alert('该文章评论已被关闭!');</script>");
                    form1.Visible = false;
                    Title = "该文章评论已被关闭!";
                }
               
            }
        }
        if (Request.QueryString["docid"] != null)
        {
            //绑定分页
            FormPaging_Rep1.TableName = "Fm_comment";
            FormPaging_Rep1.ColName = "lou";
            FormPaging_Rep1.Rep = Rep_Comment;
            FormPaging_Rep1.Where = "docid="+int.Parse(Request.QueryString["docid"].ToString())+"";
            FormPaging_Rep1.Order = 0;
        }
    }
    string GetCommentCount(int docid)
    {
      
         //DataView dv = ps.Getps("Fm_model_article_view", "comment", "docid=" + docid + "");
         if (ReturnComment(docid))
         {
             Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
             return ps.Getps("Fm_comment", "count(*) as num", "docid=" + docid + "")[0]["num"].ToString();
         }
         else
         {
             return "该文章评论已被关闭";
         }
    }
    string CommentAdd(string username, string cont)
    {
    
        int docid = 0;
        if (Request.Form["docid"] != null)
        {
            docid = int.Parse(Request.Form["docid"].ToString());
        }
        else
        {
            if (Request.QueryString["docid"] != null)
            {
                docid = int.Parse(Request.QueryString["docid"].ToString());
            }
        }
        
        // DataView dv = ps.Getps("Fm_model_article_view", "comment", "docid=" + docid + "");
         if (ReturnComment(docid))
         {
             Fm.Bussiness.Comments comment = new Fm.Bussiness.Comments();
             Fm.EntityClass.Comments com = new Fm.EntityClass.Comments();
             com.docid = docid;
             com.username = username;
             com.content = cont;
             com.writetime = DateTime.Now;
             com.ip = Request.UserHostAddress;
             com.lou = comment.GetLou(com);
             com.support = 0;
             com.against = 0;
             if (comment.CommentAdd(com))
             {
                 return "您的评论发表成功!";
             }
             else
             {
                 return "您的评论发表失败!";
             }
         }
         else
         {
             return "该文章评论已被关闭";
         }
    }
    bool ReturnComment(int docid)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
         DataView dv = ps.Getps("Fm_model_article_view", "comment", "docid=" + docid + "");
         if (dv[0]["comment"].ToString().ToLower() == "true")
         {
             return true;
         }
         else
         {
             return false;
         }
    }
  
}
