using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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.Model;
using JuSNS.Common;

namespace JuSNS.Web.user.blog
{
    public partial class article : JuSNS.UI.Page.BasePage
    {
        protected int blid;
        protected int uid;
        protected string unm;
        protected bool isFoot = false;
        public string uName = string.Empty;
        public bool isSelf = false;
        protected bool isblogHead = true;
        protected string gtitle = string.Empty;
        protected string gcontent = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["r"]))
            {
                blid = int.Parse(Request.QueryString["r"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                blid = int.Parse(Request.QueryString["id"]);
            }
            string isHeadPic = this.GetHeadPath(this.GetUserID());
            if (string.IsNullOrEmpty(isHeadPic))
            {
                if (JuSNS.Config.UiConfig.isUpHeadPic == "1" && !JuSNS.Common.Public.GetHeadLimite(3))
                {
                    isblogHead = false;
                }
            }

            if (this.GetUserID() == 0)
            {
                this.c1.InnerHtml = "<a href=\""+rooDir+"/home/blog/\" onclick=\"javascript:{unlogin();return false}\">好友的日志</a>";
                this.c2.InnerHtml = "<a href=\"" + rooDir + "/home/blog/?r=my\" onclick=\"javascript:{unlogin();return false}\">我的日志</a>";
            }
            else
            {
                this.c1.InnerHtml = "<a href=\"" + rooDir + "/home/blog/\">好友的日志</a>";
                this.c2.InnerHtml = "<a href=\"" + rooDir + "/home/blog/?r=my\">我的日志</a>";
            }

            JuSNS.Home.Blog bl = new JuSNS.Home.Blog();
            #region Ajax
            if (Request.Form["Option"] == "DelComment")
            {
                try
                {
                    int id = int.Parse(Request.Form["ID"]);
                    bl.DelComment(GetUserID(), id);
                    Response.Write("1");
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            else if (Request.Form["Option"] == "DelBlog")
            {
                try
                {
                    int id = int.Parse(Request.Form["ID"]);
                    bl.DelBlog(this.GetUserID(), id);
                    Response.Write("1");
                }
                catch
                {
                    Response.Write("0");
                }
                Response.End();
            }
            #endregion
            if (Request.Form["Comment"] == "Submit")
            {
                if (this.GetUserID() == 0)
                {
                    PageError("需要登录才能发表回复。", rooDir+"/blog/b" + blid+".aspx");
                }
                Comment(blid);
                Response.Redirect(rooDir + "/blog/b" + blid + ".aspx");
            }

            BlogInfo info;
            List<BlogCommentInfo> lst;
            bool addreads = false;
            string cookieid = "BlogReadA_" + blid + "_" + GetUserID();
            if (Request.Cookies[cookieid] == null || Request.Cookies["BlogReadA_" + blid + "_" + GetUserID()].Value != "1")
            {
                HttpCookie cooknum = new HttpCookie(cookieid);
                Response.Cookies[cookieid].Value = "1";
                Response.Cookies[cookieid].Expires = DateTime.Now.AddYears(10);
                addreads = true;
            }
            int n = bl.GetAllInfo(this.GetUserID(), blid, addreads, out info, out lst);
            uid = info.UserID;
            unm = info.UserName;
            IDMoreB(info.UserID,blid);
            if (info.UserID == this.GetUserID())
            {
                unm = "我";
                this.HypList.Text = "返回我的日志列表";
                this.HypList.NavigateUrl = "../blog/?r=my";
                uName = "我的日志";
                isSelf = true;
            }
            else
            {
                //更新足迹
                if (this.GetUserID() > 0)
                {
                    bl.UpdateBlogFoot(blid, this.GetUserID());
                }
                unm = info.UserName;
                this.HypList.Text = "返回" + info.UserName + "的日志列表";
                this.HypList.NavigateUrl = "../blog/?r=" + info.UserID;
                uName = info.UserName + "的日志";
            }
            this.LtrNum.Text = n.ToString();
            if (info.Reads > 0)
            {
                this.LtrReads.Text = info.Reads + " 次阅读";
            }
            else
            {
                this.LtrReads.Text = "没有阅读";
            }
            if (info.Comments > 0)
            {
                this.LtrComments.Text = info.Comments + " 个评论";
            }
            else
            {
                this.LtrComments.Text = "没有评论";
            }
            if (info.Title.Trim() == string.Empty)
            {
                this.LtrTitle.Text = "无标题";
            }
            else
            {
                this.LtrTitle.Text = Input.HtmlEncode(info.Title);
                gtitle = Input.HtmlEncode(info.Title);
            }
            this.ptitle.InnerText = info.Title;
            this.LtrContent.Text = info.Content;
            gcontent = JuSNS.Common.Input.GetSubString(JuSNS.Common.Input.LostHTML(JuSNS.Common.Input.ForTXT(info.Content)), 100);
            this.LtrTime.Text = info.PostTime.ToString("yyyy-MM-dd hh:mm");

            this.RptComment.DataSource = lst;
            this.RptComment.DataBind();
            DataBind();
            FootIDdiv(blid);
           
        }

        protected void FootIDdiv(int sblid)
        {
            string list = "<ul>";
            JuSNS.Home.JUser us = new JuSNS.Home.JUser();
            IDataReader dr = us.blogFootID(sblid, 8, this.GetUserID());
            while (dr.Read())
            {
                isFoot = true;
                UserInfo ui = us.GetUserInfo(Convert.ToInt32(dr["UserID"].ToString()));
                list += "<li><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dr["UserID"].ToString()) + "\"><img src=\""+GetHeadImage(ui.UserID,1)+"\" style=\"width:48px;height:48px;\" /> <br />" + ui.Username + "</a></li>\r\n";
            }
            dr.Close();
            if (isFoot) { this.isFootUser.Visible = true; }
            list += "</ul>";
            FootID.InnerHtml = list;
        }

        protected void IDMoreB(int UserId,int sblid)
        {
            string list = "<ul>";
            JuSNS.Home.JUser us = new JuSNS.Home.JUser();
            IDataReader dr = us.BlogMoreID(UserId, sblid, 10);
            while (dr.Read())
            {
                list += "<li><a href=\""+rooDir+"/blog/b" + dr["ID"].ToString() + ".aspx\">" + dr["title"].ToString() + "</a> <span class=\"time\">" + JuSNS.Common.Public.getTimeSpan(DateTime.Parse(dr["PostTime"].ToString())) + "</span></li>\r\n";
            }
            dr.Close();
            list += "</ul>";
            IDMoreBlog.InnerHtml = list;
        }

        private void Comment(int blogid)
        {
            string s = Request.Form["TxtComment"];
            #region 过滤关键字
            string res = JuSNS.Common.Public.CheckKey(s, true);
            if (res != string.Empty)
            {
                s = s.Replace(res, "***");
            }
            #endregion
            JuSNS.Home.Blog bl = new JuSNS.Home.Blog();
            bl.CommentsBlog(blogid, this.GetUserID(), s);
        }
        /// <summary>
        /// 是否显示删除的功能
        /// </summary>
        /// <param name="commuserid"></param>
        /// <param name="bloguserid"></param>
        /// <param name="commid"></param>
        /// <returns></returns>
        protected string ShowDelComm(object commuserid, object bloguserid, object commid)
        {
            if (this.GetUserID() == (int)bloguserid || this.GetUserID() == (int)commuserid)
                return "<span class=\"delete\"><a href=\"javascript:DeleteComment(" + commid + ");\">删除</a></span>";
            else
                return string.Empty;
        }
    }
}
