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

namespace JuSNS.Web.home.blog
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        public static string IDUserName = "<a href=\"../blog\">好友的日志</a>";
        public bool isMy = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string r = Request.QueryString["r"];
                if (r == "my") { isMy = true; } else { isMy = false; }
                BindData();
                DataBind();
                Foot();
            }
        }

        protected void Foot()
        {
            string list = "<ul>";
            JUser us = new JUser();
            Blog BL = new Blog();
            IDataReader dr = us.BlogFootList(GetUserID(), 20);
            while (dr.Read())
            {
                BlogInfo bi = BL.GetBlog(Convert.ToInt32(dr["blogID"].ToString()), false);
                UserInfo ui = us.GetUserInfo(bi.UserID);
                list += "<li><a href=\""+rooDir+"/blog/b" + dr["blogID"].ToString() + ".aspx\">" + bi.Title + "</a> <span><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", ui.UserID.ToString()) + "\">" + ui.Username + "</a><a href=\"../blog/?r=" + ui.UserID + "\"><img src=\"../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/site/blog_all_1.gif\"  align=\"absmiddle\" title=\"查看" + ui.Username + "所有日志\" /></a></span></li>";
            }
            dr.Close();
            list += "</ul>";
            myFoot.InnerHtml = list;
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        protected void BindData()
        {
            int ReCount = 0; 
            int PgCount = 0;
            int PageIndex = 1;
            string r = Request.QueryString["r"];
            string key=Request.QueryString["key"];
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            DataTable dt = null;
            if (!JuSNS.Common.Input.IsInteger(r))
            {
                switch (r)
                {
                    case "my":
                        this.ptitle.InnerText = "我的好友";
                        this.c1.Attributes.Add("class", "");
                        this.c2.Attributes.Add("class", "current");
                        this.c3.Attributes.Add("class", "");
                        this.c4.Attributes.Add("class", "");
                        this.c1.InnerHtml = "<a href=\"../blog\">好友的日志</a>";
                        SqlConditionInfo my_st = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                        dt = UtilPage.GetPage("blog_list_my", PageIndex, PAGESZ, out ReCount, out PgCount, my_st);
                        break;
                    case "all":
                        this.ptitle.InnerText = "所有日志";
                        this.ptitle.InnerText = "我的好友";
                        this.c1.Attributes.Add("class", "");
                        this.c2.Attributes.Add("class", "");
                        this.c3.Attributes.Add("class", "current");
                        this.c4.Attributes.Add("class", "");
                        this.c1.InnerHtml = "<a href=\"../blog\">好友的日志</a>";
                        if (key != string.Empty && key != null)
                        {
                            if (key.Length < 3)
                            {
                                Response.Redirect("../blog");
                            }
                            this.ptitle.InnerText = "所有" + key + "的日志";
                            this.keyvalue.InnerHtml = "找到关键字：<span class=\"reshow\">" + key + "</span>";
                            SqlConditionInfo all_st1 = new SqlConditionInfo("@key", key.Trim(), TypeCode.Int32);
                            all_st1.Blur = 3;
                            dt = UtilPage.GetPage("blog_list_allk", PageIndex, PAGESZ, out ReCount, out PgCount, all_st1);
                        }
                        else
                        {
                            SqlConditionInfo all_st = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                            dt = UtilPage.GetPage("blog_list_all", PageIndex, PAGESZ, out ReCount, out PgCount, all_st);
                        }
                        break;
                    case "about":
                        this.ptitle.InnerText = "提到我的日志";
                        this.ptitle.InnerText = "我的好友";
                        this.c1.Attributes.Add("class", "");
                        this.c2.Attributes.Add("class", "");
                        this.c3.Attributes.Add("class", "");
                        this.c4.Attributes.Add("class", "current");
                        this.c1.InnerHtml = "<a href=\"../blog\">好友的日志</a>";
                        SqlConditionInfo[] about_st = new SqlConditionInfo[2];
                        about_st[0] = new SqlConditionInfo("@UserName", UserName, TypeCode.String);
                        about_st[1] = new SqlConditionInfo("@UserID", "" + GetUserID().ToString() + "", TypeCode.String);
                        about_st[0].Blur = 3;
                        dt = UtilPage.GetPage("blog_aboutme_list", PageIndex, PAGESZ, out ReCount, out PgCount, about_st);
                        break;
                    default:
                        this.c1.InnerHtml = "<a href=\"../blog\">好友的日志</a>";
                        SqlConditionInfo friend_st = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                        dt = UtilPage.GetPage("blog_list_friend", PageIndex, PAGESZ, out ReCount, out PgCount, friend_st);
                        break;
                }
            }
            else
            {
                User us = new User();
                UserInfo muf = us.GetUserInfo(Convert.ToInt32(r));
                this.c1.InnerHtml = "<a href=\"../blog?r=" + r + "\">" + muf.Username + "的日志</a></li><li><a href=\"../blog\">好友的日记</a>";
                SqlConditionInfo id_st = new SqlConditionInfo("@UserID", Convert.ToInt32(r), TypeCode.Int32);
                dt = UtilPage.GetPage("blog_id_list", PageIndex, PAGESZ, out ReCount, out PgCount, id_st);
            }

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

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Content"] = dt.Rows[i]["Content"].ToString().Replace("\r\n", "<br/>");
            }
            this.RptData.DataSource = dt;
            this.RptData.DataBind();
            this.LtrNum.Text = ReCount.ToString();
            if (ReCount > PAGESZ)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, ""+rooDir+"/home/blog/?r=" + r + "&key="+key+"");
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }
    }
}
