using System;
using System.Data;
using System.Text;
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 JuSNS.Home;
using JuSNS.Model;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.news
{
    public partial class content : JuSNS.UI.Page.BasePage
    {
        protected string GetClassTitle = string.Empty;
        protected string allClass = string.Empty;
        protected bool isAll = false;
        protected int newsID;
        protected string nTitle = string.Empty;
        protected string nContent = string.Empty;
        protected int ClassID;
        protected string nName = string.Empty;
        protected string root = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            root = rooDir;
            if (this.GetUserID() == 0)
            {
                this.Form1.InnerHtml = "<a href=\""+rooDir+"/?urls="+Request.Url+"\">请登录后发表评论。</a>";
            }
            if (Request.QueryString["Option"] == "DelComment")
            {
                try
                {
                    News ns = new News();
                    int id = int.Parse(Request.QueryString["nid"]);
                    ns.DelNewsComment(this.GetUserID(), id);
                    Response.Write("1");
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            else
            {
                GetNewsContent();
                GetList();
                NewsCommentlist();
                if (!string.IsNullOrEmpty(nName))
                {
                    GetClassTitle = " - " + nName;
                    this.allNews.Attributes.Add("class", "");
                }
                else
                {
                    this.allNews.Attributes.Add("class", "current");
                    GetClassTitle = string.Empty;
                }

            }
        }

        protected void GetList()
        {
            string list = string.Empty;
            News ns = new News();
            int i = 1;
            IDataReader dr = ns.GetNewsClass(0);
            while (dr.Read())
            {
                string cssStr = string.Empty;
                if (ClassID.ToString() == dr["id"].ToString())
                {
                    cssStr = " class=\"current\"";
                }
                if (i > 9)
                {
                    isAll = true;
                    list += "<li><a href=\"" + root + "/home/news/index.aspx?ClassID=" + dr["id"].ToString() + "&n=" + JuSNS.Common.Input.URLEncode(dr["ClassName"].ToString()) + "\">" + dr["ClassName"].ToString() + "</a></li>\r\n";
                }
                else
                {
                    list += "<li><a href=\"" + root + "/home/news/index.aspx?ClassID=" + dr["id"].ToString() + "&n=" + JuSNS.Common.Input.URLEncode(dr["ClassName"].ToString()) + "\">" + dr["ClassName"].ToString() + "</a></li>\r\n";
                    allClass += "<li" + cssStr + "><a href=\"" + root + "/home/news/index.aspx?ClassID=" + dr["id"].ToString() + "&n=" + JuSNS.Common.Input.URLEncode(dr["ClassName"].ToString()) + "\">" + dr["ClassName"].ToString() + "</a></li>\r\n";
                }
                i++;
            }
            dr.Close();
            this.newsclass_div.InnerHtml = "<ul>" + list + "</ul>";
        }

        protected void GetNewsContent()
        {
            string nID = Request.QueryString["nid"];
            if (!string.IsNullOrEmpty(nID) && JuSNS.Common.Input.IsInteger(nID))
            {
                News ns = new News();
                NewsInfo nInfo = new NewsInfo();
                nInfo = ns.GetNewsId(Convert.ToInt32(nID), 1);
                this.ptitle.InnerText = nInfo.Title;
                this.NewsTitle.InnerText = nInfo.Title;
                this.NewsTime.InnerHtml = nInfo.CreatTime.ToString("yyyy-MM-dd hh:mm");
                string PicSTR = string.Empty;
                if (nInfo.Pic.Length>10)
                {
                    PicSTR = "<div class=\"center\"><img src=\"" + rooDir + "/" + nInfo.Pic + "\" style=\"width:70px;\" align=\"absmiddle\" /></div>";
                }
                this.NewsContent.InnerHtml = PicSTR + nInfo.Content;
                newsID = nInfo.ID;
                nTitle = nInfo.Title;
                nContent = Common.Input.GetSubString(JuSNS.Common.Input.ForTXT(JuSNS.Common.Input.LostHTML(nInfo.Content)), 100);
                ClassID = nInfo.ClassID;
                nName = nInfo.ClassName;
            }
            else
            {
                PageError("错误的参数", "");
            }
        }

        protected void NewsCommentlist()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            DataTable dt = UtilPage.GetPage("NewsComment_aspx", PageIndex, 30, out ReCount, out PgCount, new SqlConditionInfo("@NewsID", Convert.ToInt32(Request.QueryString["nID"]), TypeCode.Int32));
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                sb.Append("<li id=\"li_" + dt.Rows[i]["ID"].ToString() + "\">");
                sb.Append("<div class=\"body-2m-left nl image center\"><a href=\"" + root + "/user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\"><img src=\"" + GetHeadImage(Convert.ToInt32(dt.Rows[i]["UserID"].ToString()), 1) + "\" style=\"width:48px;height:48px;\" /><div>" + dt.Rows[i]["UserName"].ToString() + "</div></a></div>\n");
                sb.Append("<div class=\"body-2m-right nl\"><div class=\"content hdtitle nl s95\"><span class=\"nl\">&nbsp;<a href=\"" + root + "/user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\">" + dt.Rows[i]["UserName"].ToString() + "</a> <span class=\"time\">" + JuSNS.Common.Public.getTimeEXTSpan(DateTime.Parse(dt.Rows[i]["CreatTime"].ToString())) + "</span></span>" + ShowDelComm(dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["ID"].ToString()) + "</div>\n");
                sb.Append("<div class=\"pstr\">" + JuSNS.Common.Public.CheckSma(JuSNS.Common.Input.ForTXT(dt.Rows[i]["Content"].ToString())) + "</div></div>\n");
                sb.Append("</li>\r\n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.NewsCommentList.DataSource = data;
            this.NewsCommentList.DataBind();
            this.LtrNum.Text = ReCount.ToString();
            dt.Clear(); data.Clear();
        }

        protected string ShowDelComm(string contentuserid, string commid)
        {
            if (GetUserID() == Convert.ToInt32(contentuserid))
                return "<span class=\"delete\"><a href=\"javascript:void(0);\" onclick=\"javascript:DeleteComment(" + commid + ");\">删除</a></span>";
            else
                return string.Empty;
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            GetIsNotEmail();
            string newsID=Request.QueryString["nID"];
            string CommentContent = this.CommentContent.Text;
            if (CommentContent.Length > 200 || CommentContent.Length < 2)
            {
                this.ShowError.Visible = true;
                this.ShowError.InnerHtml = "评论长度为2-200个字符";
            }
            else
            {
                this.ShowError.Visible = false;
                News ns = new News();
                if (ns.InsertNewsComment(GetUserID(), Convert.ToInt32(newsID), CommentContent) > 0)
                {
                    Response.Redirect("" + root + "/news/n" + newsID + ".aspx#Form1");
                }
            }
        }
    }
}
